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