Compiling reveal.js-based R markdown presentation without RStudio - slideshow

Fairly recently, RStudio has added support for beautiful reveal.js-based HTML presentations generated from RMarkdown (with some extensions). These are different from earlier HTML presentation formats provided by the rmarkdown R package, which relied on ioslides or Slidy.
Is it possible to compile such a presentation to HTML without having recourse to RStudio? I.e. is there a pure R function which will, given an R presentation source file, generate the same result as the IDE?
P.S. I suppose the underlying R package doing the conversion is revealjs by JJ Allaire, but on its own, it doesn't recognize some of the syntax extensions (e.g. those for customizing appearance by putting css: custom.css under the title of the first slide), which makes me think there must be an additional wrapper around it.

You can use the standard rmarkdown::render() function with the revealjs::revealjs_presentation format in the YAML header. Resources like custom.css are referenced relative to the Rmd location so there is no need to specify these within the render() step.
User notes on specific implementation diffs between native RStudio & scripted version
It's not a drop-in replacement though. The revealjs package, as it's available from CRAN, ships with a different (newer) version of the reveal.js library than the one used internally by RStudio (3.2 vs 2.4 as of March 8th, 2016). The default settings (e.g. transitions) are also different, so they need tweaking.
Circumspection is also warranted with the version of pandoc used as the workhorse for the Markdown → HTML conversion, as RStudio may internally be using an older one with different templates. All of this means that you may need to rework your customizations (e.g. css tweaks).
There are also syntax differences -- the metadata can't go under the first heading of the presentation (i.e. the title):
Presentation title
==================
author: Foo Bar
css: custom.css
Instead, they have to be put in a traditional RMarkdown YAML header:
---
author: Foo Bar
css: custom.css
---
I'm not sure whether per-slide special settings like incremental: true (which are put under the respective slide's heading in the RPresentation format) are recognized at all.

I use a script like this that I source to render my reveal.js markdown slides:
library(rmarkdown)
library(revealjs)
file.name <- "index"
path.to.file <- "."
rmarkdown::render(file.path(path.to.file, paste0(file.name, ".Rmd")),
revealjs_presentation(theme="white", highlight="tango", slideNumber = TRUE),
encoding = "UTF-8")
# Open the generated HTML file in the browser
browseURL( file.path(path.to.file, paste0(file.name, ".html")))

Related

papaja: Changing font sizes and faces for code listings and R output

Based on the answer to this question, I was able to get 2-column papaja with listings wrapping (rather than overflowing column width). But the listings package turns off various features that help code listings and R output stand out relative to the main text.
A simple solution would be if I could globally change the font faces and/or sizes selectively for code and R output. Is there a way to do that in papaja? I haven't been able to figure this out from papaja or Rmarkdown documentation. Thank you!
When you use the listings package in a papaja (or bookdown) document, what is technically happening is that all code is wrapped into an lstlisting LaTeX environment that comes with its own capabilities of customizing code appearance. Hence, you don't see the syntax highlighting that you would otherwise see if you would not use the listings package. The documentation of the listings package with instructions how to style your code can be found here.
To make use of this, you can extend the YAML header of your papaja document like this:
documentclass : "apa6"
classoption : "jou"
output :
papaja::apa6_pdf:
pandoc_args: --listings
header-includes:
- \lstset{breaklines=true,language=R,basicstyle=\tiny\ttfamily,frame=trB,commentstyle=\color{darkgray}\textit}
Here, I first specify the code's language, and use a tiny monospace font. With frame, I add a frame around the code block, and with commentstyle I set comments in italic and gray.

Adding preamble.tex and/or modifying css

Thanks for making papaja. It's really terrific!
I just submitted my first journal article using it and ran into problems. The layout staff don't know what to do with the code chunks and listings that are fine in single-column, full page format, but not in their 2-column format. I'm trying use the class 'jou' option to make 2 columns, but I can't figure out how to control the size of code and listing fonts (possibly by modifying the css, as recommended here), or how to using the latex package 'listings' to set listings to wrap (as recommended here).
I'd be grateful for any advice, and my apologies if I've missed how one might do this in the documentation.
If it's only about getting the listings package to work, you can modify the YAML header that it looks similar to the following:
documentclass : "apa6"
classoption : "jou"
output :
papaja::apa6_pdf:
pandoc_args: --listings
header-includes:
- \lstset{breaklines=true}
However, note that using automatic line breaks will most likely break the code at some points. Therefore, it is worthwhile to consider alternatives: For instance, you could try to use a code style that uses more line breaks. The styler package and add-in might be helpful accomplishing this: https://styler.r-lib.org/

Rmarkdown rticles - MDPI template. How to print your own abbreviations

I am using the MDPI template (from the rticles package), and keen to also use the glossaries packages so I don't have to manually feed all the abbreviation on the appropriate YAML field.
For such, I have loaded the LaTex package glossaries using the header-includes:
header-includes:
\usepackage{inputenc}
\usepackage[acronym, section=section]{glossaries}
\setacronymstyle{long-short}
\makeglossaries
\makeindex
\input{glossary}
after creating several acronyms within the Rmarkdown body, I would be willing to either input the latex commands and have it printed in within the "Abbreviations" section of the template.
Currently, I am able to hack it through the following steps (I am sure there is a better way):
1- keep all the aux files chunk with :
options(tinytex.clean = FALSE)
2- cmd makeglossaries "filename"
3- Raw Latex on Rmarkdown file:
\begin{abbreviations}
\setabbreviationstyle[acronym]{long-short}
\printglossary[type=\acronymtype,title={}]
\end{abbreviations}
However, I would be keen to know if I could insert something on the YAML and use the MDPI formatting.
Alternatively, I could edit the rticles MDPI template (but I am not sure how).
Any ideas?
Cheers,
Include in YAML:
include-after: glossary.tex
I tried without success, but it seems reasonable that this approach might work with a few modifications

RMarkdown - Change Inline Code Color *without changing workflow*

I teach an introductory statistics course using R Markdown in RStudio (Server). We have students knit to html_notebooks, and we often have them use inline code to report various elements of their statistical analyses. It'd be really helpful for grading purposes if we could have the result of inline code output in a different color -- that way we could easily see if they were indeed using inline code or if they copy-pasted a number from their output into raw text.
There's a couple of ideas for solutions posted here, but these won't super work in my case. These are introductory students who are generally kinda afraid of RStudio to begin with, so asking them to do anything complicated with text_spec or sprintf will likely cause mild riots. I really need something that won't change students' workflow at all.
I wonder if there's any way to configure things either on the backend in RStudio Server (maybe by messing with knitr?), or through some kind of <style> tag wizardry in the preamble, so that inline code will print its results in a different color.
Thanks!
EDIT: Arthur Berg below has provided something that's almost exactly what I need. Here's a MWE:
---
title: "test knit_hook"
output: html_document
---
```{r, setup, include="FALSE"}
knitr::knit_hooks$set(inline=function(x){paste0("<span style=\"color: #0000FF;\">",
x,"</span>")})
```
`r pi`
The only issue with this is that it doesn't work if I change to html_notebook in the YAML header and thus use the "Preview" button in the RStudio IDE. For external reasons, it's important for us to have the output type as html_notebook. Anyone know how we might modify this to get it to work with html_notebook?
A way to achieve this without changing the workflow too much is to create your own format (e.g. html_notebook2) that is derived from the original but modifies the inline hook of knitr.
To get started you can check out this document.
Basic steps include
Create a new R package
Within this project run usethis::use_rmarkdown_template(). This creates the folder structure for your new format.
Edit skeleton.rmd and template.yaml
Define your format in a R file which has the same name html_notebook2.R(kind of a convention).
The content of the html_notebook2.R file could be
#'#import knitr
set_hooks <- function() {
default_hooks <- knit_hooks$get()
list(
inline = function(x) {
paste0("<span style=\"color: #FF0000;\">", x,"</span>")
})
}
#' #importFrom rmarkdown output_format knitr_options pandoc_options html_notebook
#' #export
html_notebook2 = function() {
output_format(
knitr = knitr_options(knit_hooks = set_hooks()),
pandoc = pandoc_options(to = "html"),
clean_supporting = FALSE,
base_format = html_notebook()
)
}
In the first part we define a new inline hook which only changes the font color.
The second part is the definition of the new format.
After building and installing the package you can create a new rmarkdown document and use output: packagename::html_notebook2 as the output format. All inline code output will be colored red using my code. Here is an example:
---
title: "Inline"
output: cformat::html_notebook2
---
## R Markdown
`r pi`
I created such a package and you can find it on GitHub. Feel free to copy it and rename it (cformat is a pretty lame working title ;) ).
Notice though that your students could change the color manually using HTML/CSS anyways. A way around could be some kind of key generation using a certain rule (unknown to the students obviously). For each inline chunk a key is generated and embedded using
paste0("<span code=", key," style=\"color: #FF0000;\">", x,"</span>")
If a valid key is embedded, the output was generated using R and not simply copied.
For those looking for a fix to a simple R markdown document, adding this line changes the inline output to blue.
knitr::knit_hooks$set(inline=function(x){paste0("<span style=\"color: #0000FF;\">", x,"</span>")})

Use additional Latex packages for math expressions in RMarkdown `output = "html_document"`

I`m aware how to use additional Latex-packages for pdf-format output from .Rmd files using
---
header-includes:
- \usepackage{mathtools}
---
in the YAML header.
However, this does (of course) not work if one specifies output: html_document.
---
output: html_document
header-includes:
- \usepackage{mathtools}
---
Using additional Latex-Packages could be of interest for output: html_document, too - especially in math expressions (MWE below)
---
title: "MWE"
output: html_document
header-includes:
- \usepackage{mathtools}
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
MathJax offer a number of extensions, and there are third-party extensions as well. If your desired package is not available in this way, then things get difficult.
Simple commands, such as \coloneqq, can be recreated using \newcommand. The simplest way is to add these via the include-before option. Using your MWE with a solution from Mathematics Meta SE, one gets:
---
title: "MWE"
output:
html_document: default
include-before:
- '$\newcommand{\coloneqq}{\mathrel{=}}$'
---
## Use "Defined by" Symbol
$$sin(x) \coloneqq \frac{opposite}{hypothenuse}$$
Output:
Background
RMarkdown is build around pandoc, which performs most of the format conversions. Pandoc creates PDF via LaTeX (by default), and will simply include any raw LaTeX commands which are given in the source. When seeing \usepackage{mathtools}, the package is not parsed, but the command is simply added verbatim to the intermediate LaTeX. However, when exporting to HTML, it wouldn't make sense to pass through LaTeX commands, so any such command will simply be omitted from the output, so any \usepackage in your document won't effect the HTML output.
Alternative solution
If you are using very complex LaTeX-packages, then you could consider setting up a complex pipeline to still use it: E.g, one could use a pandoc filter to extract all equations, compile each equation as a separate document, and then convert the resulting PDF to SVG. Finally, that SVG can then be included in the HTML output. This is non-trivial and probably not worth the effort. A similar approach is recommended to include TikZ pictures.