Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rmarkdown: different output folders, shared libs

I have an .R file wherein I for each unique value in a list, render a number of different .Rmd files. Something like this:

for (uddannelse in unique(c("Monkey","Horse"))) {
  rmarkdown::render("file1.Rmd", output_dir=file.path(getwd(), uddannelse) ,output_file=paste("file1", uddannelse,".html", sep="_"), encoding="UTF-8")
  rmarkdown::render("file2.Rmd", output_dir=file.path(getwd(), uddannelse), output_file=paste("file2", uddannelse,".html", sep="_"), encoding="UTF-8")
}

As evident by the render parameters, the html-output should go into a separate folder for each value in the list, in the above example: folder: "Monkey" and folder "Horse".

Each .Rmd file has the following front matter (the files go to a static html website and needs to have self_contained: false:

---
output:
  html_document:
    theme: readable
    self_contained: false
    lib_dir: pub/libs
    css: pub/libs/custom_css/custom.css
    date: "`r format(Sys.time(), '%d %B, %Y')`"
---

However: When I call the render functions I recieve this error:

Error in relativeTo(basepath, dir) :

The path C:/Users/ac/Dropbox/2014_07_WIP/pub/libs/jquery-1.11.0 does not appear to be a descendant of C:/Users/ac/Dropbox/2014_07_WIP/Monkey/

So I guess the rmarkdwown::render first creates the lib directories relative to the Rmd file, yet expect the files to be placed relative to the output files.

How can I get around this, so that I can have a set of common Rmd input files in one folder, and have output in different folders, yet share a common lib?

I tried to place something like this in the fronmatter.

---
output:
  html_document:
    theme: readable
    self_contained: false
    lib_dir: "`r file.path(uddannelse, "libs")`"
    css: "`r file.path(uddannelse, "libs", "custom_css", "custom.css")`"
    date: "`r format(Sys.time(), '%d %B, %Y')`"
---

And I got this error:

  Error in yaml::yaml.load(front_matter) : 

  Parser error: while parsing a block mapping at line 3, column 5did not find expected key at line 5, column 50
like image 216
Andreas Avatar asked Jul 07 '14 12:07

Andreas


1 Answers

I solved my immediate problem by passing some of the front matter in the render call:

rmarkdown::render("file1.Rmd", 
                   output_dir=file.path(uddannelse),
                   output_file=paste("file1", uddannelse,".html", sep="_"),
                   output_options=list(html_document = 
                     list(self_contained = FALSE, 
                          lib_dir = file.path(uddannelse, "lib"),
                          css = paste("lib", "custom_css", "custom.css",
                                      sep="/"),
                          include = list(
                            after_body = file.path(uddannelse,
                                                  "footer_w_index.html")))),
                   encoding="UTF-8")

Notice that lib_dir has to be relative to the Rmdfile and css has to be relative to the output-file.

For some reason - regardless of whether I use paste or file.path(fsep="/", ...) the css path in the output file is linked with windows separator ("\") - and thus not usable in e.g. Firefox.

like image 112
Andreas Avatar answered Sep 29 '22 19:09

Andreas