I am trying to write R Package vignettes using R Markdown. I am using R Studio's package authoring tools.
I have R greater than version 3.0.
I have an .Rmd file in the vignettes
folder that contains the following text at the top:
<!-- %\VignetteEngine{knitr::knitr} %\VignetteIndexEntry{An Introduction to the bootcorrelations package} -->
I have the following in my DESCRIPTION
file:
VignetteBuilder: knitr Suggests: knitr
When I clean and build or build and reload the package in RStudio, the vignette source is displayed, but not the HTML (i.e., there's no HTML file in inst/man
).
How can I get RStudio to automatically create the HTML from an R Markdown Vignette?
I've read through Yihui's post on R Package Vignettes with Markdown and it suggests using a makefile, but this more recent documentation about knitr vignettes suggests that the makefile is no longer required.
I also realise that I could manually create the HTML vignette with a command like this:
library(knitr) knit(input='vignettes/foo.Rmd', output='inst/doc/foo.md') library(markdown) markdownToHTML('inst/doc/foo.md', 'inst/doc/foo.html')
A reproducible example:
Vectorize(dir.create)(c("test", "test/R", "test/man", "test/vignettes")) cat( 'Package: test Title: Test pkg Description: Investigate how to auto-compile markdown vignettes Version: 0.0-1 Date: 2015-03-15 Author: Jeromy Anglim Maintainer: Jeromy Anglim <[email protected]> Suggests: knitr License: Unlimited VignetteBuilder: knitr', file = "test/DESCRIPTION" ) cat( '--- title: "Introduction" author: "Jeromy Anglim" date: "`r Sys.Date()`" output: html_document --- <!-- %\\VignetteEngine{knitr::rmarkdown} %\\VignetteIndexEntry{Introduction} --> # Introduction A sample vignette! ```{r} 1 + 1 ```', file = "test/vignettes/intro.Rmd" ) cat( "#' Nothing #' This function is only needed so that roxygen generates a NAMESPACE file. #' @export nothing <- function() 0", file = "test/R/nothing.R" ) library(roxygen2) library(devtools) roxygenise("test") build("test")
The usual way to compile an R Markdown document is to click the Knit button as shown in Figure 2.1, and the corresponding keyboard shortcut is Ctrl + Shift + K ( Cmd + Shift + K on macOS). Under the hood, RStudio calls the function rmarkdown::render() to render the document in a new R session.
The check workflow, Cmd + Shift + E , will run the code in all vignettes. This is a good way to verify that you've captured all the needed dependencies.
You need the rmarkdown package, but you don't need to explicitly install it or load it, as RStudio automatically does both when needed.
R Markdown still runs the code in the chunk, and the results can be used by other chunks.
Update: Thinking laterally, there are at least three options.
As @Hadley points out, running build_vignettes()
from the devtools
package will build vignettes and place them in the inst/man
directory of the package.
devtools::build_vignettes()
Building the vignette means you get three versions in inst/man
:
This is not tied to the build and reload command in RStudio, but it is a one line solution to the task. And it could easily be incorporated into a makefile
.
Knit HTML
in RstudioAs @TylerRinker notes you can just use Knit HTML in Rstudio. This will add both md and HTML knitted versions of the rmd vignette to the vignettes
directory.
This also is not tied to the build process but as @TylerRinker points out, often you don't want the vignettes tied to the main build process. This also has the benefit of giving you an md file which can be a nice option for displaying the vignette on github, although http://htmlpreview.github.com/ is an option for displaying HTML vignette on github.
Build - Build Source Package
in RStudio corresponds to R CMD build
. When run a compressed tar.gz
file is created which contains in the inst/doc
directory the Rmd, R and HTML files for the original rmd vignette file.
This is described in the official documentation on writing package vignettes which instructs you to use R CMD build
to generate PDF and HTML vignettes.
So it is possible to
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