在 bookdown 中的 knitr 块选项中设置图形路径

Set figure path in knitr chunk options in bookdown

我想为大型文档的所有图像设置文件路径。

这是我的 yaml 和 r 设置:

---
title: "General Introduction"
csl: harvard-cite-them-right.csl
header-includes:
- \usepackage{pdflscape}
- \usepackage{longtable}
- \newcommand{\blandscape}{\begin{landscape}}
- \newcommand{\elandscape}{\end{landscape}}
- \usepackage{lmodern}
- \usepackage{graphicx}

output:
  bookdown::pdf_book:
    keep_tex: yes
    latex_engine: xelatex
    toc: yes
    toc_depth: '2'
  html_document: default
  word_document:
    reference_docx: my-styles.docx
    toc: yes
    toc_depth: 2
mainfont: Arial
fontsize: 11pt
bibliography: library.bib
---

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE, fig.path = "figs&tabs/")

我正在尝试包含如下图片:

```{r ed, fig.cap = '(ref:roum)' , out.width='100%' }
knitr::include_graphics("ed.png")

但是找不到文件。如果我在块中包含文件路径,它会起作用,但我不想每次都以多个图像的形式执行此操作:

```{r ed, fig.cap = '(ref:roum)' , out.width='100%' }
knitr::include_graphics("figs&tabs/ed.png")

我做错了什么?

knitr::opts_chunk$set中的fig.path选项用于设置文件路径图保存到。

为了在 r 设置块中设置文件路径,我创建了一个包装函数:

fpath = function(x) {paste("figs_and_tabs/", x, sep = "")}

并插入图像:

```{r ed, fig.cap = '(ref:roum)' , out.width='100%' }
knitr::include_graphics(fpath("ed.png"))

更进一步:要设置文件路径 在我使用的函数中包含 knitr::include_grpahics 调用:

```{r setup, include=FALSE}
library(knitr)
knitr::opts_chunk$set(echo = FALSE)
fp = function(x) {knitr::include_graphics(paste("figs_and_tabs/", x, sep = ""))}

然后用

插入图片
```{r ed, fig.cap = '(ref:roum)' , out.width='100%' }
fp("ed.png")