note: much of the information in the problem statement is irrelevant, and the problem has been resolved with no apparent reason through the debugging steps that are listed in the resolution section
I apologize for asking a question about an error that I can not reproduce.
Problem
I have a test function 'foo.R' in my package 'PKG'; foo.R consists of:
foo <- function (filename, n) {
text <- scan(file = filename, what = "character")
if (n==1) text <- gsub("\\#GGG", '\\#', text)
if (n>1) text <- gsub("\\#GGG", '', text)
writeLines(text, con = 'newfn.R')
}
The intent of foo is to either uncomment lines by replacing "#GGG" with "" when n>1 or to leave the line commented if n == 1
The minimal code required to produce this error is:
foo <- function (string) {
gsub("\\#GGG", '', string)
}
However, when I run R CMD INSTALL PKG
I get the following error:
Error in parse(outFile) :
/tmp/RtmpLbFQF0/R.INSTALL2edd9a07/PKG/R/foo.R:3:1: unexpected '}'
2: gsub("\\#GGG", '\\#', string)
3: }
^
UPDATES
Based on the answers, '\#' is not the culprit.
I have found that the problem is during install (not build), and I have changed the question to reflect this. I have also fixed the curly braces from my test case.
Also, I have found that not only does the function work, e.g. when loaded with source(),
When I make a new project (e.g. 'newproject') and move the function there, it installs without error, i.e. this works:
- move foo.R to newproject/R/foo.R
- make a newproject/DESCRIPTION
- R CMD build newproject
- R CMD INSTALL newproject
This gives the error above:
- R CMD build PKG
- R CMD INSTALL PKG
In response to the request for the error output log, the output from
R CMD check PKG
is the same as the error from theR CMD INSTALL PKG
,
- installing source package ‘PKG’ ... ** R Error in parse(outFile) : /home/user/PKG/R/foo.R:24:0: unexpected end of input 22: writeLines(model.text, con = outfile) 23: } ^ ERROR: unable to collate files for package ‘PKG’
- removing ‘/home/user/PKG.Rcheck/PKG’
Resolution
The resolution of the error is inexplicable, but I reproduced the same seemingly magical method on two functions with the same error.
mv /PKG/R/foo.R PKG/foo.R
- Visit new
PKG/R/foo.R
in emacs - for (i in misc bits of function
- cut-and-paste i
R CMD check PKG
- if
PKG
can install - return to
1
- else remove bits and return to
3
diff PKG/foo.R PKG/R/foo.R
- if no difference except that function开发者_开发百科 now works
- update question on SO
- consider previous error a fluke
- continue as if nothing happened
UPDATE 2: culprit found!
The problem was an errant 'e' stuck to the left of the comment symbols ## coming before a function
NO, R is complaining because there's no opening {
foo <- function(string){
gsub(blah)
}
open curly, close curly.
If R CMD INSTALL
is still throwing an error, then it will generate a message along the line of "check the file /path/to/00install.log" or similar
immediately at the end of the information printed to the console. Go and have a look at that log file as it will show exactly where the error occurred and a transcript of the preceding R calls leading to the error.
If R CMD check
is throwing an error, there will be a similar note to look in the file /path/to/check/directory/00check.log
, which again will contain useful information.
Does looking at those two files, whichever is appropriate, help? If not, post the content of the relevant log file and we might be able to give you more concrete advice.
At the moment, you assumption re the \\#
does not appear to be a problem for R at all --- it parses that function just fine.
I couldn't re-create this issue via the steps below:
- Copied
foo
into my global environment and ranpackage.skeleton()
- Deleted lines 34-35 in
anRpackage/man/anRpackage-package.Rd
- Added a non-empty title to
anRpackage/man/foo.Rd
- Ran
R CMD build anRpackage/
- Ran
R CMD INSTALL anRpackage_1.0.tar.gz
- Ran
R CMD check anRpackage_1.0.tar.gz
Everything worked, except check
, which failed due to bad examples in anRpackage-Ex.R
. For reference, here's the foo
I used.
foo <- function (filename, n) {
text <- scan(file = filename, what = "character")
if (n==1) text <- gsub("\\#GGG", '\\#', text)
if (n>1) text <- gsub("\\#GGG", '', text)
writeLines(text, con = 'newfn.R')
}
精彩评论