9

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)
```

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

我知道两种绕过 CRAN 检查的方法:

  1. 使用 knitr 选项:eval = FALSE.

  2. 在 R.rsp包的帮助下制作静态小插图。

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

4

2 回答 2

2

最简单的方法就是将数据包含在您的包中。虚拟数据集位于:

  • data目录。这将允许用户轻松访问它。
  • 或在inst/extdata. 用户可以访问此文件,但它比较隐蔽。您会使用system.file(package="my_pkg")

在小插曲中你会有一些东西

```{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()
```
于 2016-09-18T09:51:11.810 回答
1

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.

于 2017-11-25T08:52:14.877 回答