If I use devtools::use_vignette("my-vignette")
in my package, running devtools::build_vignette()
works fine.
However, once I add a call to anything from my package, it stops working, with error could not find function "myfunc"
. If I add a library(mypackage)
call, I get the error there is no package called 'mypackage'
.
(I should note that my package checks, builds & installs perfectly cleanly [with no vignettes], and running devtools::load_all()
also works fine for interactive sessions.)
I know that if I build & install my package, I can then get the vignettes built. This seems like a really inefficient and dangerous way to develop; essentially forcing me to re-build and re-install the entire package on every commit, to test that the vignette isn't breaking.
Is there another way to get the vignette to recognize the package-in-progress?
If you are using RStudio IDE (which is very helpful for package developpement), you can render your Rmd document created by devtools::use_vignette
, by clicking on the Knit button. It will create a preview version of your vignette.
By the way, RStudio IDE provides you with helpful shortkeys and buttons to execute your Rmd document chunk by chunk to test if it's working.
If you are not using RStudio IDE, you could render your document without building the package by using the function rmarkdown::render
.
However, in order to be working, your vignette requires your package to be loaded. So, as you said, you'll have to call library(mypackage)
and so your package have to be installed.
You can install your package without the vignette in the command line with devtools::install(build_vignette = FALSE)
. In the RStudio IDE, the button Build & Reload
is enougth to intall your package.`
Another solution for non user of Rstudio IDE is to use devtools::load_all(path to your package)
in your vignette in order to simulate the installation of your package in the vignette environment. Then you can build your vignette with devtools::build vignette
whithout needing to install your package before.
I should underline that vignette is build automatically when your build your package. So, when development are finish, replace in the vignette devtools::load_all
by library
because your package is loaded before building the vignette when you build a package.
If you look up Hadley Wickham's packages in github, you will see he includes a library(xyz) at the top of his vignette, e.g. https://github.com/tidyverse/dplyr/blob/master/vignettes/dplyr.Rmd
Then his recommended way to build vignettes works:
You can build all vignettes from the console with
*devtools::build_vignettes()*
, but this is rarely useful. Instead usedevtools::build()
to create a package bundle with the vignettes included.
I believe this is what you will need to submit a package to CRAN.
It is a slow development cycle, though, so for active coding, you can insert a line with devtools::load_all()
to use the knit in RStudio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With