Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enforce PDF package vignette with knitr

I have a vignette for my package which compiles to a nice PDF when compiled with Rscript -e 'rmarkdown::render("tmod.rmd")'. However, when I compile and install the package, start R and type vignette("mypackagename"), a browser window opens with ugly looking HTML, without a title, without a table of contents and without references.

However, if I compile the vignette manually to pdf before creating the package, it shows up just fine.

What went wrong? Why is my PDF not generated automatically when I compile the package?

In the markdown file header, I have

---
title: "foo foo foo"
author: "Foofooary Finer"
date: "`r Sys.Date()`"
output:
  pdf_document:
vignette: >
  %\VignetteIndexEntry{FooFoo}
  %\VignetteKeyword{foo}
  %\VignetteEngine{knitr::knitr}
  %\SweaveUTF8
  \usepackage[utf8](inputenc)
abstract: |
  foo foo foo foo foo
toc: yes
bibliography: bibliography.bib
---

In the package DESCRIPTION file, I have

VignetteBuilder: knitr
Suggests: knitr
like image 899
January Avatar asked Jun 19 '15 11:06

January


People also ask

How do I add a vignette to a package in R?

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.

What is knitr package?

The R package knitr is a general-purpose literate programming engine, with lightweight API's designed to give users full control of the output without heavy coding work. It combines many features into one package with slight tweaks motivated from my everyday use of Sweave.

How does R knitr work?

When you run render , R Markdown feeds the . Rmd file to knitr, which executes all of the code chunks and creates a new markdown (. md) document which includes the code and its output. The markdown file generated by knitr is then processed by pandoc which is responsible for creating the finished format.

How do you use the knitr in RStudio?

If you are using RStudio, then the “Knit” button (Ctrl+Shift+K) will render the document and display a preview of it.


1 Answers

When I asked the same question on the knitr google group, Yihui Xie (author of knitr) replied:

Use the vignette engine knitr::rmarkdown instead of knitr::knitr.

I'm not entirely sure I understand why, but it works. Here is a link to discussion on the knitr google group.

like image 86
January Avatar answered Sep 28 '22 00:09

January