出于安全原因,程序包插图中的R代码无法在CRAN上运行。如何管理这样的小插图?

时间:2021-11-24 11:28:01

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包使用私有用户名和密码与商业数据库通信以建立连接。在package_vignette.Rmd文件中有一大块代码:

```{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.

我无权向CRAN提供实际的用户名和密码,因此我无法在软件包中提供正版的“connection.config”文件。因此,当然,此代码片段会在CRAN检查期间导致错误。

I know two ways to get around CRAN check:

我知道两种方法来解决CRAN检查:

  1. Use knitr option: eval = FALSE.

    使用knitr选项:eval = FALSE。

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

    借助R.rsp包制作静态晕影。

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.

第一种方式太耗时,因为有很多块,我经常重写/重建小插图。第二种方式对我来说更好。但可能有更好的模式如何支持这样的小插曲?例如,在包的测试中,我使用testthat :: skip_on_cran()来避免CRAN检查。

2 个解决方案

#1


2  

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")
  • 或者在inst / extdata中。用户可以访问此文件,但它更隐蔽。您可以使用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()
```

#2


1  

testthat::skip_on_cran just checks a system variable

testthat :: skip_on_cran只检查一个系统变量

> 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

从我收集的内容来看,这是由testthat或devtools设置的。因此,你可以使用

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.

在chunk选项中并加载testthat或devtools在第一个块中的一个。否则,您可以在站点上使用类似的机制并分配类似的系统变量并检查它是否为“true”。例如,使用Sys.setenv(“IS_MY_COMP”,“true”))。如果您使用R studio或在R_HOME / Rprofile.site文件中,则在.Rprofile文件中放入Sys.setenv调用。有关后一选项的信息,请参阅帮助(“启动”)。

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

或者,您可以检查是否存在“/home/user001/connection.config”

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

in the chunk option.

在块选项中。

#1


2  

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")
  • 或者在inst / extdata中。用户可以访问此文件,但它更隐蔽。您可以使用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()
```

#2


1  

testthat::skip_on_cran just checks a system variable

testthat :: skip_on_cran只检查一个系统变量

> 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

从我收集的内容来看,这是由testthat或devtools设置的。因此,你可以使用

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.

在chunk选项中并加载testthat或devtools在第一个块中的一个。否则,您可以在站点上使用类似的机制并分配类似的系统变量并检查它是否为“true”。例如,使用Sys.setenv(“IS_MY_COMP”,“true”))。如果您使用R studio或在R_HOME / Rprofile.site文件中,则在.Rprofile文件中放入Sys.setenv调用。有关后一选项的信息,请参阅帮助(“启动”)。

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

或者,您可以检查是否存在“/home/user001/connection.config”

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

in the chunk option.

在块选项中。