rpackagedevtoolsroxygen

How to stop a R package from executing an .R script?


I am trying to prepare the package. In the development stage, I want to create an example script example.R that I can use to check different functions of my package. However, I do not want my example script to be executed when I am updating the package. Here is what I am facing the challenge:

Assume my example.R has following code:

print("Hello")

if, I update and save my package:

devtools::document()
# Actual Output: [1] "Hello"
# Desired Output: It should be blank. Code in Example.R script should not be executed  

I have tried various other options but unsuccessful so far:

  1. attempt with @examples
#' @examples
print("Hello")

if, I update and save my package:

devtools::document()
# Actual Output: [1] "Hello"
# Desired Output: It should be blank. Code in Example.R script should not be executed  
  1. attempt with \dontrun{}
#' \dontrun{
print("Hello")
#'}

if, I update and save my package:

devtools::document()
# Actual Output: [1] "Hello"
# Desired Output: It should be blank. Code in Example.R script should not be executed  

The only approach that seems to work is by commenting the whole code:

# print("Hello")

if, I update and save my package:

devtools::document()
# Actual Output:
# Desired Output: It should be blank. Code in Example.R script should not be executed  

Commenting is challenging as I have multiple lines of code that I will be changing and editing continuously during the development phase. Thus I need a more simple option.


Solution

  • If you look at the Writing R Extensions manual for packages, it offers three basic steps: R CMD build to create a tarball, R CMD INSTALL to install it (not your goal here) and R CMD check to check it during development. They all offer numerous switches to tweak the behaviour. Use those -- i.e. I often do R CMD check --no-manual --no-vignettes to skip pdf / latex part.

    And R CMD check has the very --no-examples flag you are looking for. I am not an active user of devtools but I would suspect it also offers you a pass-through of those options. And, worst case, if it doesn't, just use the standard tools. (In RStudio you will find a toggle, and you can set options to the R CMD ... calls as you would on the command-line.)

    (In the narrow sense of stopping examples, I keep forgetting what is current but you can try all of \dontrun{}, \donttest{}, ... as well as explicit conditioning on an environment variable you set. All of that will be visible in the code and may not be what you want to show in your documentation though.)