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
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.
Related
I cannot compile to a pdf-beamer from a very simple minimal example (below), with no packages.
I click - Knit - Knit to pdf
(Note this is not a duplicate of previous posts, because those all involved using Kable; I am not using it; fresh restart not calling any packages here)
I tried with and without classoption: table
Knit to html works just fine.
Restarted Rstudio; no improvement
---
title: "Untitled"
author: "Test"
date: "20 novembre 2018"
classoption: table
output:
beamer_presentation:
keep_tex: yes
---
# Test nothing
blah
```
Throws error: ! LaTeX Error: Option clash for package xcolor.
I want it to compile a beamer pdf slide show, of course.
I've put the log file here
My default.beamer (stored at /Users/yosemite/.pandoc/templates/default.beamer I think) is linked HERE
I'm confused as to why my R package's vignette is in HTML on CRAN and not a pdf.
I followed the advice at Enforce PDF package vignette with knitr and used
%\VignetteEngine{knitr::rmarkdown}
to generate the vignette, but there it is, a pdf. The full header of one of the files is
---
title: "wCorr Formulas"
author: "Paul Bailey, Ahmad Emad, Ting Zhang, Qingshu Xie"
date: '`r Sys.Date()`'
output:
pdf_document: default
vignette: |
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{wCorr Formulas}
\usepackage[utf8]{inputenc}
\usepackage{amssymb}
---
you can see the whole package at https://github.com/cran/wCorr or, before it gets built at https://github.com/ahmademad/wCorr
You are including copies of the HTML output (presumably from an earlier version) in inst/doc.
This might not have been intentional; I believe devtools::build_vignettes might do it to work around a bug in R: https://bugs.r-project.org/bugzilla3/show_bug.cgi?id=17416. But there are better workarounds, e.g. described here: https://github.com/yihui/knitr/issues/1540 .
I'm trying to run inline R code in the YAML front matter before getting rmarkdown to run the file. However it isn't working for me. Here's an example:
---
title: "**Title**"
classoption: xcolor=dvipsnames
output:
beamer_presentation:
slide_level: 2
pandoc_args: [
"--bibliography", "`r paste('path/to/bib')`"
]
---
<!-- slide 1 -->
## Intro ##
Which throws an error:
pandoc-citeproc: could not find `r paste('path/to/bib')`
This is a simple example, but highlights my main problem. How do I get rmarkdown to run the inline R code in the YAML front matter?
It is a similar problem to these questions:
Manipulate RMarkdown metadata from within R code chunks
YAML current date in rmarkdown
This is how I solved this. I knit from RStudio. Curiously, I had to use one solution for the date and csl fields and a different solution for the bibliography field. !expr did not work in the date or csl lines (for me). And quoted r code didn't work in the bibliography line (for me). I have the bibliography and csl files in a package (inst/docs folder). rmarkdown files, which are not part of that package, use those.
---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: !expr system.file("docs", "my.bib", package = "MyPackage")
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---
# Introduction
Yada yada [#MyRef04].
# References
my.bib is the BibTex file with MyRef04. csl is the style file
This is a situation where one person maintains a package which has data, code, bibliography, etc. Others, potentially unknown to the package writer, install that package from GitHub and write or run rmarkdown files that use the package. The users almost certainly do not use Git or GitHub and I don't want them to have to download any extra files after installing the package from GitHub.
Update: After posting the above, I happened to install markdown from GitHub because I needed something in the development version. With version ‘1.7.5’ of rmarkdown on GitHub you can use r code in the bibliography line:
---
title: "Title"
date: '`r format(Sys.time(), "%d %B, %Y")`'
output: html_document
bibliography: '`r system.file("docs", "my.bib", package = "MyPackage")`'
csl: '`r system.file("docs", "my.csl", package = "MyPackage")`'
---
To install rmarkdown from GitHub
library(devtools)
install_github("rstudio/rmarkdown")
So I found a round about way of getting what I wanted. Rmarkdown I don't think allows R expressions/commands in the YAML, probably for a good reason. What I ended up doing was putting all the output yaml commands in a file called _output.Ryaml like so:
beamer_presentation:
slide_level: 2
includes:
in_header: "src/preamble.tex"
pandoc_args: [
"--bibliography", "`r paste('path/to/bib')`",
"--variable", "classoption:xcolor=dvipsnames",
"--variable", "fontsize:9pt"
]
Then in the main slides.Rmd file, was something like:
---
title: "**Title**"
author: Luke
---
<!-- slide 1 -->
## Intro ##
Then, I can generate the slides using the R code (which I put into a Makefile):
knitr::knit('_output.Ryaml', '_output.yaml')
rmarkdown::render('slides.Rmd')
unlink('_output.yaml')
Seems to be working well enough. If anyone's got a better idea, let me know!
I am new to writing R packages. I'm trying to learn how to make a vignette for my package. I have created a vignettes folder with a file "getting-started.Rmd"
---
title: "WaterML Tutorial"
author: "Jiri Kadlec"
date: "`r Sys.Date()`"
output: rmarkdown::html_vignette
vignette: >
%\VignetteIndexEntry{Introduction to the WaterML R package}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
## Quick Start
This simple example shows how to get started with the <my R package>.
To build the vignette I use the command:
devtools::build_vignettes()
Then I run Rcmd.exe INSTALL my_package, and to view my vignette I run:
browseVignettes("my_package")
However I only see the vignettes in the html and source format:
As you see in the screenshot, there's no "pdf" option. How do I configure my .Rmd file to create my vignette in the pdf format?
In your header, you are telling R to output only an html vignette in line:
output: rmarkdown::html_vignette
If you want pdf, try:
output: pdf_document
According to R packages:
Output: this tells rmarkdown which output formatter to use. There are many options that are useful for regular reports (including html, pdf, slideshows, …) but rmarkdown::html_vignette has been specifically designed to work well inside packages. See ?rmarkdown::html_vignette for more details.
So you might have a few small problems using a raw pdf.
At this time, rmarkdown does not have a output: rmarkdown::pdf_vignette option
I am using knitr, R markdown, and a custom LaTeX file to write a manuscript. The abstract is set as part of the YAML frontmatter. This works great, except that the results that I would like to include in the abstract are generated later in the document.
Having the abstract in the YAML makes some sense to me and I would prefer to keep it this way. Any thoughts on a way to have the abstract take advantage of calculations that happen later in the document?
A brief example:
---
title: 'How do I interpret R objects in YAML that are created in the document?'
author: Jeff Hollister
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
html_document: null
word_document: null
fontsize: 11pt
documentclass: article
abstract: This is a test. Is this `r mean(x)` working?
---
This is a test of outputing a pdf, with R code interpretted in the YAML front mater. Use case
for this would be a manuscript, with abstract as part of YAML, and some of the results in the
abstract being calculated in the body of the Rmd. For instance:
```{r}
x<-rnorm(100)
mn<-mean(x)
mn
```
It's not exactly YAML-related, and limited to latex, but maybe this helps. Sorry, the thread is in German, but maybe the code is clear enought.
You can pick items for the abstract as you create your report; the items are written out to a temporary file similar to method use in bibtex and biblatex, and merged to the start and end (for appendix) in a second step.
As a package, you can download it from here .
Requirements
Install R, knitr, and yaml:
sudo su -
apt-get install pandoc
apt-get install r
r
url <- "http://cran.us.r-project.org"
install.packages('knitr', repos=url)
install.packages('yaml', repos=url)
Create Variables
Separate the variables into a new file (e.g., v.yaml):
title: 'How do I interpret R objects in YAML that are created in the document?'
author: Jeff Hollister
output:
pdf_document:
fig_caption: yes
keep_tex: yes
number_sections: yes
html_document: null
word_document: null
fontsize: 11pt
documentclass: article
abstract: This is a test. Is this `r mean(x)` working?
Create Script
Create a script to load knitr, yaml, and the variables (e.g., knitr.sh), when given a file in Markdown format:
#!/bin/bash
R -e \
"library(knitr); library(yaml); v <- yaml.load_file('v.yaml'); knit('$1')"
Reference Variables
Use R statements (inline or otherwise) to reference the source document variables (document.md):
# `r v$title`
Author: `r v$author`
Run Script
Generate a knitr-processed Markdown file by running the script:
./knitr.sh document.md
View Output
The file document.txt (renaming is a problem left for the reader) should contain the following Markdown:
# How do I interpret R objects in YAML that are created in the document?
Jeff Hollister
Addendum
It is fairly trivial with shell script magick to insert the variables at the top of the generated file, should they be needed for processing by pandoc. If using YAML variables for pandoc's templates, I'd recommend skipping that route and using R variables instead.