31

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).

enter image description here

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 <a@b.com>
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")
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
  • Why the need for the build to create the HTML? Why not use knit HTML button instead if you're using R Studio? It makes sense not to have the HTML as part of the build as it should be minimal and the vignette can then be created when installed. – Tyler Rinker Oct 15 '13 at 13:46
  • 2
    @TylerRinker I take your point. Perhaps I need to think more about workflow. My initial motivations was: (a) to use the vignettes as an additional package test, (b) ensure the files are accessible on the github copy of the repository (e.g., use http://htmlpreview.github.com/ on the resulting html vigenette, which I can then link to from the `readme.md` file); (c) ensure that any copy of the vignette distributed as a raw file would be up to date. – Jeromy Anglim Oct 15 '13 at 23:06
  • I have similar needs and use a workflow that (1) use knit HTML (2) copy the file to a dropbox public folder with `file.copy` (3) use a link from the GitHub README to the dropbox file. This works pretty well for me. PS didn't know about the htmlpreview.github.com. pretty cool. – Tyler Rinker Oct 15 '13 at 23:22
  • I'm replacing step (2) with the htmlpreview. Thanks. Still leaves the question of why you'd want the build to make the Vignette. You can even [style the vignette in RStudio](http://www.rstudio.com/ide/docs/authoring/markdown_custom_rendering) if that's the reason and it's way faster than a complete build. I think this workflow still accomplishes your 3 goals. – Tyler Rinker Oct 15 '13 at 23:26
  • 1
    It is entirely possible to extend [`servr`](https://github.com/yihui/servr) a little bit so that it can serve package vignettes in the browser, and all you need to do is to refresh the page if you want to rebuild the vignette. I have written the code in my mind, and just need to find some time to type it out :) – Yihui Xie Oct 20 '13 at 04:42

1 Answers1

17

Update: Thinking laterally, there are at least three options.

1. Use build_vignettes()

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:

  1. the Rmd source
  2. the knitted or weaved HTML vignette
  3. and the R code in the code blocks

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.

2. Use Knit HTML in Rstudio

As @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.

3. Build source package and extract from compressed file

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

  1. Build Source
  2. Unzip the tar.gz file
  3. Browse and open the resulting file
Jeromy Anglim
  • 33,939
  • 30
  • 115
  • 173
  • 3
    You can use `devtools::build_vignettes()` – hadley Oct 15 '13 at 13:21
  • 1
    The only key is `R CMD build`; that is the official approach to getting compiled vignettes into the vignette index in the HTML help. Currently RStudio only does `R CMD INSTALL`, and I do not think that is always a good idea, e.g. it does not build vignettes. I'll mention it in the support forum if no one has done so. – Yihui Xie Oct 20 '13 at 01:33
  • Would you mind providing a sample make file that uses `build_vignettes()`? – StevieP May 12 '14 at 16:52
  • Also @hadley, you have an ambiguous pronoun in the help documentation for `build_vignettes()`: which files are to be included in `.Rbuildignore`, the files in `vignettes/` or the files in `inst/doc/`? – StevieP May 12 '14 at 16:53
  • 2
    So, I'm using RStudio version 0.98.836 and none of these options work. In particular, `build_vignettes()` returns `NULL` despite having a bonafide .Rmd in `vignettes/` and `knit HTML` doesn't save an .md or .html file in `vignettes/`... Any reason as to why this would be? – StevieP May 15 '14 at 07:18