R devtools::build_vignettes() - can generate both html and pdf files?

41 Views Asked by At

I want to generate both an .html file and a .pdf file when I call devtools::build_vignettes()

This is the content of my .Rmd file:

---
title: "Function xyz"
output:
  html_document: default
  pdf_document: default
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Introduction

This document gives a brief example on how to use the function...

If I call devtools::build_vignettes(), it only generates the first output in the list, so it only generates a .html file.

However, if I swap the order of the outputs like this:

---
title: "Function xyz"
output:
  pdf_document: default
  html_document: default
vignette: >
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

# Introduction

This document gives a brief example on how to use the function...

... a call to devtools::build_vignettes() will generate only a .pdf file instead.

I want the call to devtools::build_vignettes() to generate both an .html file and a .pdf file.

How can this be achieved?

1

There are 1 best solutions below

0
user2554330 On

Vignettes have source code, R code, and a display format which is either PDF or HTML. They don't have two ways to display them, so using vignette based functions isn't going to do what you want.

However, your vignettes are also R Markdown files, and those are more flexible. So what you could do is use the same method that devtools::build_vignettes() uses to find all of your vignettes, then treat (some of) them as R Markdown files, and render those in multiple formats.

For example, here's a snippet:

vigns <- tools::pkgVignettes(dir = ".")$docs
for (v in vigns) 
  rmarkdown::render(v, output_format = "all")