简体   繁体   English

在包装本身的装饰图案中加载R包

[英]Loading R package in vignette for the package itself

I am currently building an R package, call it mypackage . 我目前正在构建一个R包,称之为mypackage I generated a package vignette using devtools::use_vignette("my-vignette") , and filled in my-vignette with content. 我使用devtools::use_vignette("my-vignette")生成了一个包装小devtools::use_vignette("my-vignette") ,并用内容填充了my-vignette。 Of course, because it is a package vignette, I need to demonstrate the use of the functions and data contained in this package itself. 当然,因为它是一个包装插图,我需要演示此包中包含的函数和数据的使用。 However, I'm having issues loading the package and its contents when knitting the vignette. 但是,在编织小插图时,我在加载包及其内容时遇到问题。

All of the code chunks in the vignette run without error on my local computer, even after restarting R and clearing the workspace. 即使在重新启动R并清除工作区后,插图中的所有代码块也会在本地计算机上运行时没有错误。 However, when I attempt to build the package using devtools::build(), the build fails with: 但是,当我尝试使用devtools :: build()构建包时,构建失败并显示:

Error in library(mypackage): there is no package called `mypackage` Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> library Execution halted

The full output in the Console from devtools::build() is: 来自devtools::build()的控制台中的完整输出是:

* checking for file ‘/path/to/mypackage/DESCRIPTION’ ... OK
* preparing ‘mypackage’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Quitting from lines 39-44 (my-vignette.Rmd) 
Error: processing vignette 'my-vignette.Rmd' failed with diagnostics:
'my_function' is not an exported object from 'namespace:mypackage'
Execution halted

devtools::document() runs without error, but devtools::build_vignettes() aparently reaches a different error where it cannot use a specific function (but seems to be able to load the package itself): devtools :: document()运行没有错误,但devtools :: build_vignettes()显然达到了一个不同的错误,它不能使用特定的函数(但似乎能够加载包本身):

> devtools::build_vignettes()
Building mypackage vignettes
Moving basic-mypackage-vignette.html, basic-mypackage-vignette.R to inst/doc/
Copying basic-mypackage-vignette.Rmd to inst/doc/
> devtools::build()
'/Library/Frameworks/R.framework/Resources/bin/R' --no-site-file --no-environ --no-save --no-restore --quiet CMD build  \
'/path/to/mypackage' --no-resave-data --no-manual 

* checking for file ‘/path/to/mypackage/DESCRIPTION’ ... OK
* preparing ‘auctestr’:
* checking DESCRIPTION meta-information ... OK
* installing the package to build vignettes
* creating vignettes ... ERROR
Quitting from lines 39-44 (my-vignette.Rmd) 
Error: processing vignette 'my-vignette.Rmd' failed with 
diagnostics:
could not find function "my_function"
Execution halted
Error: Command failed (1)
> devtools::document()
Updating mypackage documentation
Loading mypackage

I assume this is because the package itself does not exist in my R library; 我假设这是因为我的R库中不存在包本身; I am just loading that package using devtools::load_all() when doing development. 我正在进行开发时使用devtools::load_all()加载该包。 How can I get around this and load my package in order to use its functions and data in the package vignette? 我如何绕过这个并加载我的包以便在包装插图中使用它的功能和数据? Specifically, how can I make the package in its functions available to whatever environment knitr is using to knit the vignette? 具体来说,我如何使其功能中的包可用于编织者用来编织小插图的任何环境?

I have seen other package vignettes load the package directly using library() without issue; 我已经看到其他包装晕影使用library()直接加载包而没有问题; for example, in the stringr vignette. 例如,在stringr中

I hope readers can evaluate this without a fully reproducible example; 我希望读者能够在没有完全可重复的例子的情况下对此进 reproducing the entire example is difficult because it requires a full R package directory structure that is difficult to replicate or share here. 再现整个例子很困难,因为它需要一个完整的R包目录结构,这里很难复制或共享。

I do load directly my package in my vignettes ( library(my_package) ). 我直接在我的插图中加载我的包( library(my_package) )。

But considering your error, it might not be the problem... 但考虑到你的错误,可能不是问题......

Can you make sure that my_function is indeed exported. 你能确保确实导出了my_function吗? You should add the following code ate the end of the documentation of your function. 您应该在函数文档的末尾添加以下代码。

#' @export

或者,您可以使用:::表示法,尤其是如果您不导出该函数,例如

mypackage:::my_function()

I added a chunk 我添加了一大块

```{r, include=FALSE}
devtools::load_all(".")
```

at the beginning of the vignette to work around the problem. 在小插图的开头解决问题。

However, this doesn't fix the actual problem, as this should not be necessary. 但是,这并不能解决实际问题,因为这不是必需的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM