Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Loading R package in vignette for the package itself

Tags:

I am currently building an R package, call it mypackage. I generated a package vignette using devtools::use_vignette("my-vignette"), and filled in my-vignette with content. 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. However, when I attempt to build the package using devtools::build(), the build fails with:

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:

* 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::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; I am just loading that package using devtools::load_all() when doing development. 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; for example, in the stringr vignette.

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.

like image 785
jpgard Avatar asked Nov 10 '17 12:11

jpgard


1 Answers

I do load directly my package in my vignettes (library(my_package)).

But considering your error, it might not be the problem...

Can you make sure that my_function is indeed exported. You should add the following code ate the end of the documentation of your function.

#' @export
like image 109
Emmanuel-Lin Avatar answered Sep 22 '22 12:09

Emmanuel-Lin