I wrote an R package for internal purposes and also added some vignettes. When I use devtools::install(build_vignettes = TRUE)
, all vignettes are installed properly on my machine. But in order to distribute the package to colleages, I would like to generate a windows binary.
But when I use Build binary package from the RStudio Build pane, the vignettes will not show up when the package is installed from the generated binary. I figured that I could move the vignettes from doc
to inst/doc
before building the binary package, but this needs to be done manually whenever a vignette changes.
From R CMD INSTALL --help
I could not figure if there is an option to include building the vignettes.
Is there any better option available than manually copying the files from doc
to inst/doc
?
I already tried devtools::build_vignettes()
. This is the output in the console:
> devtools::build_vignettes()
Building archivR vignettes
Moving vig1.html, vig2.html, vig1.R, vig2.R to doc/
Copying vig1.Rmd, vig2.Rmd to doc/
Building vignette index
It says that the files are copied to doc/
. They do not appear in inst/doc
.
To create a package vignette in R Markdown, the easiest way is through the RStudio menu File -> New File -> R Markdown -> From Template (see Figure 16.4). Then you select “Package Vignette” from the rmarkdown package, and you will get a vignette template.
To see the vignette for a specific package, use the argument, browseVignettes("packagename") . Each vignette provides three things: the original source file, a readable HTML page or PDF, and a file of R code. You can read a specific vignette with vignette(x) , and see its code with edit(vignette(x)) .
Currently, only PDF versions of vignettes can be viewed. The program specified by the pdfviewer option is used for this. If several vignettes have PDF versions with base name identical to topic , the first one found is used. If no topics are given, all available vignettes are listed.
A source package is just a directory of files with a specific structure. It includes particular components, such as a DESCRIPTION file, an R/ directory containing . R files, and so on. Most of the remaining chapters in this book are dedicated to detailing these components.
Just for reference:
The most robust way is to build a source package (.tar.gz file) and then use the command tools instead of RStudio to build the binary. If you're in the directory where the source package can be found, you can use the following command at the command line (provided R is in your PATH):
R CMD INSTALL --build pkgname_x.y.z.tar.gz
with pkgname_x.y.z.tar.gz
the name of the tar file containing the source package.
Note that you should create the source package first and build from the source package if you want to have the vignettes added correctly.
If you use
devtools::build()
devtools::build("../package_name.tar.gz", binary=TRUE)
then the vignettes will be build into the tar.gz file, first and then into the binary.
No need to move any files about
I have also been struggling with the same question.
devtools::build_vignettes()
put the results in inst/doc
(for example, as recommended in the last bullet point here). devtools::build_vignettes()
now puts the results in doc
(the specific change appears to be here). The reasons for this change are given in the issues linked to this commit. I can't find a way of accomplishing the previous workflow using only devtools
, so I used the following code. It will overwrite any files that are already in inst/doc
or inst/Meta
.
build_vignettes_to_inst <- function() {
devtools::build_vignettes() # Builds vignettes to 'doc' and 'Meta'. Updates '.gitignore'.
unlink(c("inst/doc", "inst/Meta"), recursive = TRUE) # Remove the directories if they exist
dir.create("inst/doc"); dir.create("inst/Meta") # Create empty directories
has_worked <- c( # Copy files to 'inst' subfolders
file.copy(list.files("doc", full.names = TRUE), to = "inst/doc")
, file.copy(list.files("Meta", full.names = TRUE), to = "inst/Meta")
)
unlink(c("doc", "Meta"), recursive = TRUE) # Optional: Remove unwanted directories
return(all(has_worked)) # Returns TRUE if everything worked OK
}
build_vignettes_to_inst() # Call the function
You can now call devtools::build()
with binary = TRUE
, and it will include the built (i.e. HTML) vignettes.
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