Shantanu
Shantanu

Reputation: 867

How to use Docker to convert Rmarkdown to HTML?

How to convert the following test.rmd using Docker to a give an output as an .html document?

I am not able to find a short and simple example out here without requiring RStudio to be setup in a docker container.

test.rmd

---
title: "Test"
author: "John Doe"
date: "5/15/2019"
output:
  html_document:
    theme: lumen
---

## R Markdown

Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.

## Scatter Plot

```{r}
library(ggplot2)

ggplot(mpg, aes(displ, hwy, colour = class)) + 
  geom_point()
``

Upvotes: 2

Views: 1593

Answers (1)

Dirk is no longer here
Dirk is no longer here

Reputation: 368409

Here is an answer for you (with two corrections/changes) based on a Rocker container I made for Rmd and which I had locally. It is fairly minimal: R plus rmarkdown plus a full LaTeX stack (that aspect mattered to me, it may not to you) and little else, but ggplot2. You can download it with a standard docker pull rocker/r-rmd from the Rocker account at the Docker Hub.

I had to make two changes to your Rmd file. First, I added the missing third backtick on the last line. Then, I also had to remove the lumen theme. That may come from another package -- I do not recall. After that, the following command does the trick (and your example is save as file.Rmd):

docker run --rm -ti -v${PWD}:/work -w/work \
    rocker/r-rmd Rscript -e 'rmarkdown::render("file.Rmd")'

Output log on my box

edd@rob:~/git/stackoverflow/56157292(master)$ docker run --rm -ti -v${PWD}:/work -w/work rocker/r-rmd Rscript -e 'rmarkdown::render("file.Rmd")'


processing file: file.Rmd
  |................................                                 |  50%
  ordinary text without R code

  |.................................................................| 100%
label: unnamed-chunk-1

output file: file.knit.md

/usr/bin/pandoc +RTS -K512m -RTS file.utf8.md --to html4 --from markdown+autolink_bare_uris+ascii_identifiers+tex_math_single_backslash+smart --output file.html --email-obfuscation none --self-contained --standalone --section-divs --template /usr/lib/R/site-library/rmarkdown/rmd/h/default.html --no-highlight --variable highlightjs=1 --variable 'theme:bootstrap' --include-in-header /tmp/Rtmp6683qu/rmarkdown-str1417442cc.html --mathjax --variable 'mathjax-url:https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML'

Output created: file.html
edd@rob:~/git/stackoverflow/56157292(master)$

Rendered Output

enter image description here

Upvotes: 10

Related Questions