【问题标题】:Rmarkdown: different output folders, shared libsRmarkdown:不同的输出文件夹,共享库
【发布时间】:2014-08-27 22:39:42
【问题描述】:

我有一个.R 文件,其中我为列表中的每个唯一值呈现多个不同的.Rmd 文件。像这样的:

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")
}

从渲染参数可以看出,html-output 应该为列表中的每个值进入一个单独的文件夹,在上面的示例中:文件夹:“Monkey”和文件夹“Horse”。

每个.Rmd文件都有以下前题(文件去静态html网站,需要有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')`"
---

但是:当我调用渲染函数时,我收到了这个错误:

relativeTo(basepath, dir) 中的错误:

路径 C:/Users/ac/Dropbox/2014_07_WIP/pub/libs/jquery-1.11.0 似乎不是 C:/Users/ac/Dropbox/2014_07_WIP/Monkey/ 的后代

所以我猜rmarkdwown::render 首先创建相对于 Rmd 文件的 lib 目录,但希望这些文件相对于输出文件放置。

我该如何解决这个问题,以便我可以在一个文件夹中拥有一组通用 Rmd 输入文件,并在不同文件夹中输出,但共享一个通用库?

我试图在前面放置这样的东西。

---
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')`"
---

我得到了这个错误:

  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

【问题讨论】:

    标签: r yaml rstudio pandoc r-markdown


    【解决方案1】:

    我通过在渲染调用中传递一些前面的问题解决了我的直接问题:

    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")
    

    注意lib_dir 必须与Rmd文件相关,css 必须与输出文件相关。

    出于某种原因——无论我使用paste 还是file.path(fsep="/", ...),输出文件中的css 路径都与windows 分隔符(“\”)链接——因此不能用于例如火狐。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2021-02-27
      • 1970-01-01
      • 2016-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多