R CMD INSTALL is stuck on a function that works… h

2019-08-14 03:44发布

问题:

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

  1. Based on the answers, '\#' is not the culprit.

  2. 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.

  3. 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:

      1. move foo.R to newproject/R/foo.R
      2. make a newproject/DESCRIPTION
      3. R CMD build newproject
      4. R CMD INSTALL newproject
    • This gives the error above:

      1. R CMD build PKG
      2. R CMD INSTALL PKG
  4. In response to the request for the error output log, the output from R CMD check PKG is the same as the error from the R 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.

  1. mv /PKG/R/foo.R PKG/foo.R
  2. Visit new PKG/R/foo.R in emacs
  3. for (i in misc bits of function
    1. cut-and-paste i
    2. R CMD check PKG
    3. if PKG can install
    4. return to 1
    5. else remove bits and return to 3
  4. diff PKG/foo.R PKG/R/foo.R
  5. if no difference except that function now works
    1. update question on SO
    2. consider previous error a fluke
    3. 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

回答1:

NO, R is complaining because there's no opening {

foo <- function(string){   
 gsub(blah)
}

open curly, close curly.



回答2:

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.



回答3:

I couldn't re-create this issue via the steps below:

  1. Copied foo into my global environment and ran package.skeleton()
  2. Deleted lines 34-35 in anRpackage/man/anRpackage-package.Rd
  3. Added a non-empty title to anRpackage/man/foo.Rd
  4. Ran R CMD build anRpackage/
  5. Ran R CMD INSTALL anRpackage_1.0.tar.gz
  6. 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')
}


标签: r install