%in% in @examples in R function documentation

Hey folks, I'm currently helping a collaborator update their function documentation in the R package 'IndependenceTests'. I've recently migrated the old documention to roxygen2 for better NAMESPACE management.

In one example for the dependogram function, the'%in%' operator is used.

#' n <- 100
#' W <- sample(x = 1:8, size = n, TRUE)
#' X1 <- W %in% c(1, 2, 3, 5)
#' X2 <- W %in% c(1, 2, 4, 6)
#' X3 <- W %in% c(1, 3, 4, 7)
#' X4 <- W %in% c(2, 3, 4, 8)
#' X <- cbind(X1, X2, X3, X4)
#' dependogram(X, vecd.or.p = c(1, 1, 1, 1), N = 10, B = 20, alpha = 0.05,
#'             display = TRUE, graphics = TRUE)

document() executes successfully and calling the .Rd of the function
Unfortunately, check() or R CMD CHECK complains giving me this ERROR

❯ checking for unstated dependencies in examples ... ERROR
  Warning: parse error in file 'lines':
  9: unexpected '\\'
  174: W <- sample(1:8, n, replace = TRUE)
  175: X1 <- W \
               ^

This is the corresponding section of the example in the dependogram.Rd

n <- 100
W <- sample(x = 1:8, size = n, TRUE)
X1 <- W \%in\% c(1, 2, 3, 5)
X2 <- W \%in\% c(1, 2, 4, 6)
X3 <- W \%in\% c(1, 3, 4, 7)
X4 <- W \%in\% c(2, 3, 4, 8)
X <- cbind(X1, X2, X3, X4)

I can see the % is being escaped but I couldn't find the '\' the error is referring to.

I tried to workaround the problem by using markdown syntax and R chunks following the R packages book, but with no success.

Adding @md in my function file and placing the problem code into ```{r} chunks, like so:

#' ```{r}
#' n <- 100
#' W <- sample(x = 1:8, size = n, TRUE)
#' X1 <- W %in% c(1, 2, 3, 5)
#' X2 <- W %in% c(1, 2, 4, 6)
#' X3 <- W %in% c(1, 3, 4, 7)
#' X4 <- W %in% c(2, 3, 4, 8)
#' X <- cbind(X1, X2, X3, X4)
#' dependogram(X, vecd.or.p = c(1, 1, 1, 1), N = 10, B = 20, alpha = 0.05,
#'             display = TRUE, graphics = TRUE)
#'```

resulted in another familiar RMD error:

❯ checking for unstated dependencies in examples ... ERROR
  Warning: parse error in file 'lines':
  attempt to use zero-length variable name

I also tried the less recommended option of placing the example in a ```R chunk

but I get the same attempt to use zero-length variable name error.

I even had a look at the dplyr repo's across.R to see if I have got the formatting of the chunks right!

I am perplexed and a bit lost! I am tempted to comment out the examples.

Would really appreciate your suggestions and advice :pray:

How can we reproduce this? I can't reproduce this on your package.

Hi @Gabor

Thanks for a speedy reply. Yes apologies I had to learn to how make a reprex for this issue

library(devtools)

usethis::create_package("reproduceerror")

usethis::use_r("example.R")

usethis::use_mit_license()

writeLines(
  paste0("
         #' #title Example to reproduce error
         #' @param hello element to be printed by function
         #' @examples n <- 100
         #' W <- sample(x = 1:8, size = n, TRUE)
         #' X1 <- W %in% c(1, 2, 3, 5)
         #' X2 <- W %in% c(1, 2, 4, 6)
         #' X3 <- W %in% c(1, 3, 4, 7)
         #' X4 <- W %in% c(2, 3, 4, 8)
         #' X <- cbind(X1, X2, X3, X4)

say_hey <- function(hello){
print(hello)
}"
), "R/example.R")

devtools::document()

devtools::check()

Interestingly when I ran this reprex - I am unable to reproduce the problem.
Let me try isolate the issue further and see what is going on

Hi @Gabor

This reprex below reproduces the error for the backticks R method:

library(devtools)

create_package("reproduceerror")

usethis::use_mit_license()

usethis::use_r("example.R")

writeLines(
  paste0("
         #' #title Example to reproduce error
         #' @param hello element to be printed by function
         #' @examples 
         #' # Example 4.3: Test of mutual independence between 4 dependent binary 
         #' # variables which are 2-independent (pairwise) and also 3-independent
         #' # (any 3 of the 4 variables are mutually independent).
         #' ```R
         #' n <- 100
         #' W <- sample(x = 1:8, size = n, TRUE)
         #' X1 <- W %in% c(1, 2, 3, 5)
         #' X2 <- W %in% c(1, 2, 4, 6)
         #' X3 <- W %in% c(1, 3, 4, 7)
         #' X4 <- W %in% c(2, 3, 4, 8)
         #' X <- cbind(X1, X2, X3, X4)
         #' say_hey(X)

         say_hey <- function(hello){
         print(hello)
         }"
         ), "R/example.R")

devtools::document()

devtools::check()
── R CMD check results ─────────────────────────────────────────────────── reproduceerror 0.0.0.9000 ────
Duration: 8.6s

❯ checking for unstated dependencies in examples ... ERROR
  Warning: parse error in file 'lines':
  attempt to use zero-length variable name

Replacing: the three backticks R to {r} also reproduces the error

Why are you using ```r in @examples?

@examples already contains R code that the user can run. I am not sure if there is a use case for using ```r within an @examples section, if you think you have a good use, consider opening an issue about it in the rogygen2 issue tracker.

For now, the workaround is to not use ```r in @examples.

Hi @Gabor,

I've solved my issue. It was an oversight of escaping % within in another .R script.
My collaborator had written `%in%in the examples, and afterroxygenise()the resulting code turned into\%` which threw the error!

Removing the escaping \ in front of % solved the issue!

This topic was automatically closed after 45 days. New replies are no longer allowed.


If you have a query related to it or one of the replies, start a new topic and refer back with a link.