首页 > 解决方案 > 在 R markdown 中的 YAML 标头之前包含 R 块

问题描述

我的 Rmd 文件与它所需的文件位于不同的文件夹中(例如style.css,、、logo.pngheader.html

我试图将文件夹设置为工作目录,但是在我设置它之前调用了这些文件,因此编织不起作用,因为它找不到文件。

因为它是:

---
title: foo title
author: foo
output: 
  html_document:
    includes:
      in_header: header.html
    css: style.css

---
```{r setup, include=FALSE, echo=FALSE}
  library(knitr)
opts_knit$set(root.dir = "~/path/to/folder/")
```

正如我所希望的那样:

```{r setup, include=FALSE, echo=FALSE}
  library(knitr)
opts_knit$set(root.dir = "~/path/to/folder/")
```
---
title: foo title
author: foo
output: 
  html_document:
    includes:
      in_header: header.html
    css: style.css

---

从我想渲染此报告的 R 脚本中,我使用以下方法调用它:

rmarkdown::render(input = report.rmarkdown.rmd.fpath,
                  output_file = output.html.report.fpath,
                  knit_root_dir = report.working.dpath)

但我可以想象在设置 knit_root_dir 的那一刻发生同样的情况。

如果无法在 YAML 标头之前放置一个块,知道如何找到它header.html吗?style.css

我可以“手动”编写文件路径,但我的 header.html 看起来像:

<div><img src="logo.png" width="200px" align="right"></div>

而且logo.png无论如何都找不到,对吧?

非常感谢!

标签: rr-markdown

解决方案


推荐阅读