Skip to content
Prev 6802 / 12125 Next

[R-pkg-devel] Advice on identifying parsing failures with examples when trying to pkgdown my package

On 10/04/2021 3:28 p.m., Chris Evans wrote:
In Example code, % needs to be escaped, or it will be interpreted as a 
comment.  Sometimes that means the examples just compute the wrong 
thing, e.g.

A %*% b

is parsed as A when the rest of the line is ignored as a comment. 
Sometimes it makes the code illegal.  I don't know which you're seeing.
pkgdown tries to parse \dontrun examples, so this was a good thing, but 
I think you missed some:  e.g. in checkAllUnique.Rd, I see this around 
lines 55-60:

\examples{
\dontrun{
### letters gets us the 26 lower case letters of the English alphabet so 
should test OK
checkAllUnique(letters)
[1] TRUE
### good!

That line containing TRUE is not legal R code, so should be commented. 
You can find these one by one by copying the text from the Rd example 
section into a file, and trying to parse that file, e.g.

Put this in "test.R":

   ### letters gets us the 26 lower case letters of the English alphabet 
so should test OK
   checkAllUnique(letters)
   [1] TRUE
   ### good!

then run parse("test.R"):

Error in parse("test.R") : test.R:3:1: unexpected '['
2: checkAllUnique(letters)
3: [
    ^

You can do it for a file at a time by loading the test.R file into 
RStudio; it'll put markers beside each syntax error.  Unfortunately, it 
thinks

checkAllUnique(tmpTib \%>\% pull(1))

is a syntax error:  those escapes are required in Rd, but are illegal in R.

I don't know if there's a way to do it directly from the Roxygen source.

Duncan Murdoch