Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R code in package vignette cannot run on CRAN for security reasons. How to manage such vignette?

An R package communicates with a commercial data base using a private user_name and password to establish connection. In the package_vignette.Rmd file there is a chunk of code:

```{r, eval = TRUE}
# set user_name and password from user's configuration file
set_connection(file = "/home/user001/connection.config")

# ask data base for all metrics it has
my_data <- get_all_metrics()

# display names of fetched metrics
head(my_data$name)
```

I do not have the rights to provide actual user_name and password to CRAN, so I can not supply genuine 'connection.config' file with the package. So, of course, this code fragment leads to Error during CRAN checks.

I know two ways to get around CRAN check:

  1. Use knitr option: eval = FALSE.

  2. Make static vignette with help of the R.rsp package.

The first way is too time-consuming, because there are a lot of chunks, and I rewrite/rebuild the vignette often. The second way is better for me. But may be there is a better pattern how to support such vignette? For example, in the package's tests I use testthat::skip_on_cran() to avoid CRAN checks.

like image 871
Mikhail Zvagelsky Avatar asked Apr 14 '15 15:04

Mikhail Zvagelsky


2 Answers

The easiest way is just to include the data with your package. Either the dummy data set in:

  • the data directory. This would allow users to easily access it.
  • or in inst/extdata. Users can can access this file, but it's a bit more hidden. You would find the location using system.file(package="my_pkg")

In the vignette you would have something

```{r, echo=FALSE}
data(example_data, package="my_pkg")
my_data = example_data
```

```{r, eval = FALSE}
# set user_name and password from user's configuration file
set_connection(file = "/home/user001/connection.config")

# ask data base for all metrics it has
my_data <- get_all_metrics()
```
like image 84
csgillespie Avatar answered Nov 03 '22 15:11

csgillespie


testthat::skip_on_cran just checks a system variable

> testthat::skip_on_cran
function () 
{
    if (identical(Sys.getenv("NOT_CRAN"), "true")) {
        return(invisible(TRUE))
    }
    skip("On CRAN")
}
<environment: namespace:testthat>

From what I gather, this is set by testthat or devtools. Thus, you could use

eval = identical(Sys.getenv("NOT_CRAN"), "true")

in the chunk option and load testthat or devtools in one of the first chunks. Otherwise, you can use a similar mechanism on your site and assign a similar system variable and check if it is "true". E.g., use Sys.setenv("IS_MY_COMP", "true")). Then put a Sys.setenv call in your .Rprofile file if you use R studio or in your R_HOME/Rprofile.site file. See help("Startup") for information on the later option.

Alternatively, you can check if "/home/user001/connection.config" exists with

eval = file.exists("/home/user001/connection.config")

in the chunk option.

like image 37
Benjamin Christoffersen Avatar answered Nov 03 '22 16:11

Benjamin Christoffersen