Include TeX header in R package for RMarkdown documents - r

I want to create an R package containing a latex header file which would then be available from an RMarkdown document to create a PDF with slides via TeX. When I include a reference to the latex file in the header of the RMarkdown document, I can create my slides. But I do not know how to package it. So my two related questions are:
How can I put a LaTeX-file in an R package so that it can be accessed later?
How can I include the LaTeX file in a new RMarkdown document to create slides after loading the package (like a template)?

Yes, you can. Do something like
---
output:
beamer_presentation:
includes:
in_header: my_header.tex
---
and the my_header.tex may be any (la)tex code, including package load and variable settings. You can also have before_body: and more.
If you just want packages loaded there is also a simpler way via yaml but I don't have that handy as I use the above mechanism more often.

Related

Knitr: adding a PDF as a vignette

My package has a PDF file which I'd like to include as a vignette. There are guides on the internet for doing that with Sweave and R.rsp, but I'd like to use knitr as the vignette engine because we are writing another vignette in Rmd.
My latest attempt has file.pdf and file.pdf.asis inside the vignettes/ folder, with the latter containing these two lines:
%\VignetteIndexEntry{title}
%\VignetteEngine{knitr::asis_output}
FWIW, if I use R.rsp as an engine, the second line above can simply contain %\VignetteEngine{R.rsp::asis} and that vignette builds fine, so one suspicion I have is that I am simply using the wrong knitr equivalent.
I've also tried some monstrosities involving a mix of a YAML header and LaTeX inclusion of the PDF, which didn't work either:
---
title: "title"
author: "yes"
output: pdf_document
vignette: >
%\VignetteIndexEntry{title}
%\VignetteEngine{knitr::rmarkdown}
%\VignetteEncoding{UTF-8}
---
\includepdf{file.pdf}
Thanks to the power of open-source software, I was able to run browseVignettes(), go through my installed packages containing a mix of HTML and PDF vignettes, and eventually check out the source code for the jsonlite package. This led me to a simple solution I thought about but immediately dismissed because I thought it wasn't possible: declare both knitr and R.rsp as vignette engines.
So my package's DESCRIPTION now includes:
VignetteBuilder: R.rsp, knitr
and I have an Rmd vignette containing %\VignetteEngine{knitr::rmarkdown} on its YAML header and a file.pdf.asis containing %\VignetteEngine{R.rsp::asis}.

Code / Process for running rmarkdown in base R

All my codes developed in base R and I don't want to use RStudio, however I want to use rmarkdown feature in base R which is available in Rstudio.
I have downloaded rmarkdown package in base r, but not able to derive a code to publish my work
All the output of my codes written in R should be view able through web browser.
First make sure you're using .Rmd as your file extension. If not, rename it to a .Rmd extension. Make sure you have Pandoc installed on your OS.
Next, add the following to the top of the file:
---
title: "Your notebook title"
output: html_document
---
output: could take any value. You can pass in the value of ioslides_presentation for example if you want but it looks like html_document fits the criteria of what you want pretty well.
Once you have that, write your code in any editor (or the R console if you prefer). Use the code chunks and markdown text formatting as you normally would:
```{r}
plot(1:10)
```
In my base R Console, this is how mynotebook.Rmd looks like:
Finally, use the render() function from rmarkdown. You can either attach it and run render():
library(rmarkdown)
render("mynotebook.Rmd")
Or, run rmarkdown::render("mynotebook.Rmd").
Notice that the use of RStudio is not required at all since Pandoc is the document converter performing this task. For the so inclined, this is what its documentation has to say:
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 it's output.
The markdown file generated by knitr is then processed by pandoc
which is responsible for creating the finished format.
This may sound complicated, but R Markdown makes it extremely simple
by encapsulating all of the above processing into a single render
function.

Is it possible to include a bibliography in a revealjs presentation using rmarkdown?

I am trying to include a bibliography into a revealjs presentation using rmarkdown. However, despite the apparent inclusion of the bibliography in the pandoc processing (the pandoc command generated by rmarkdown includes the bib file and the citeproc filter), the generated html does not include the references. Using a different slide presentation generator and rmarkdown, such as ioslides, correctly includes the references. I was not able to find any obvious statement abuot supporting bibliography processing with rmarkdown and revealjs. Is it possible?
As a workaround you can try the following:
create a separate Rmd-file and adapt the following:
---
output: html_document
bibliography: pathtomybibfile.bib
nocite: |
#cite, #all, #your, #references
---
generate html-version of this file (e.g. name it bib.html)
include html within your revealjs-presentation via:
# {data-background-iframe="bib.html"}

using rmarkdown as a vignette engine

I've written a few vignettes in R Markdown, with the intention of having them
get built with RStudio's rmarkdown
package. I know that rmarkdown::render is the function we use to convert
.rmd's to .html (or whatever other format), however, when I place
<!--
%\VignetteEnginer{rmarkdown::render}
%\VignetteIndexEntry{some test title}
-->
in the preamble of my .rmd (and knitr and rmarkdown in my Suggest's field of
DESCRIPTION, as well as rmarkdown in the VignetteBuilder field) my vignette
does not compile.
Has anyone managed to get rmarkdown to act as a vignette builder?
Taking from #Ben's answer (and the comments below), knitr has registered a vignette engine that accesses rmarkdown (if it's installed) and
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Supplementary materials}
-->
is example of how we'd register it. However, in order to take full advantage of rmarkdown (that is, conversion of .Rmd's into .html's and preservation of any styling defined in the .Rmd) you must place the code snippet above BELOW the "rmarkdown preamble". As an example, the top of your .Rmd should look like
---
Title: "Supplementary Materials"
output:
html_document:
theme: flatly
---
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Supplementary Materials}
-->
Of course, you also need to make sure you have appropriately created your DESCRIPTION file to include rmarkdown and knitr. The simplest way to do this is with
Suggests: knitr, rmarkdown
VignetteBuilder: knitr
Why do you want to use rmarkdown rather than knitr? At first glance your question looks like a bit of confusion between rmarkdown and knitr. To clarify:
rmarkdown is an 'authoring format' that is 'based on knitr and pandoc'. When we run rmarkdown::render we are calling knitr and/or pandoc.
knitr is the engine that converts rmarkdown to html/PDF/docx. This is what is executing the R code to get output and plots and so on.
The knitr package author already mentioned that 'because the rmarkdown package is not on CRAN yet, you cannot use the vignette engine knitr::rmarkdown at the moment'. If you can't wait you could register your own engine but that looks rather complex.
I think what you want is:
This at the top of your Rmd doc:
<!--
%\VignetteEngine{knitr::rmarkdown}
%\VignetteIndexEntry{Supplementary materials}
-->
And this in your DESCRIPTION file:
VignetteBuilder: knitr
Suggests:
knitr
For a complete example, check out the tidyr package, here's the DESCRIPTION and here's the rmarkdown vignette (hat-tip to Andrie for pointing me to this).
If there's something specific you want from rmarkdown that you can't get from knitr (a custom style, etc.) then you should put that up in a new question.

knit HTML does not save html in vignettes/

So I have a vignette, vignettes/test-vignette3.Rmd:
---
title: "Sample Document"
output:
html_document:
highlight: kate
theme: spacelab
toc: yes
pdf_document:
toc: yes
---
Header
=========
When I hit the knit HTML button, I get the following:
processing file: test-vignette3.Rmd
output file: test-vignette3.knit.md
Output created: /tmp/RtmpKVpegL/preview-5ef42271c0d5.dir/test-vignette3.html
However, if I copy this file to inst/doc and hit the knit HTML button, I get:
processing file: test-vignette3.Rmd
output file: test-vignette3.knit.md
Output created: test-vignette3.html
My questions are:
How do I get RStudio to save the output from knit HTML on vignettes/test-vignette3.Rmw to the vignettes directory?
How do I get RStudio to not delete test-vignette3.knit.md during the knit HTML procedure? (I'd like to have the .md so people can read it on my github repo.)
I'm running RStudio version 0.98.836, rmarkdown version 0.1.98 and knitr version 1.5.
Actually you should not keep the .html output under vignettes/, because the vignette output is supposed to be generated by R CMD build. R may fail to recompile your vignettes if the HTML output files have already been there when you build the source package, which means you are likely to see old (and possibly wrong) results because the HTML file was not generated from the latest version of the .Rmd file. Therefore RStudio intentionally avoid writing the HTML files in the vignetttes directory.
If you choose to ignore the warning above, you can certainly run rmarkdown::render('your-vignette.Rmd') in the R console.
For the second question, I do not recommend you to do that, either, because Github renders the markdown to HTML differently (compared to the Pandoc conversion done through the rmarkdown package). Normally the package vignettes are shown on CRAN, see, for example, the knitr page on CRAN. However, because the rmarkdown package is not on CRAN yet, you cannot use the vignette engine knitr::rmarkdown at the moment (I guess we are not too far away from the CRAN release now). You can consider pushing the HTML files to Github pages, though.

Resources