首页 > 解决方案 > 如何不运行会出错的块

问题描述

我正在编写一个带有每周更新的图像的 html 文件。我正在使用 Rmarkdown 文件来指定图像的文件路径,如下所示:

---
title: "test"
output:
 html_document:
   toc: true
   toc_depth: 2
   toc_float: true
   theme: flatly 
params: 
  user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
  user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---

我将每周生成这些报告,因此一周我们可能有用户 2 的图像,而一周我们可能没有用户 2 的图像。所以我知道我可以编织并获得一个运行设置 error = TRUE 的 html。但这仍然会在缺少图像的 html 输出中显示错误消息。有没有办法设置一个条件,如果文件不存在,该块将不会在我在文件中读取的位置运行?我使用 magick 包从参数中指定的路径读取每个图像。

标签: rimager-markdownrmagick

解决方案


您也可以将R语句传递给 knitr 选项,如下所示。

---
title: "test conditional chunks"
output: html_document
params: 
  user1: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts.png"
  user2: "C:/Users/blah/Desktop/Discrete-event-simulation-concepts5.png"
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```


```{r condition 1, echo=FALSE, eval=file.exists(params$user1)}
# plot image
```

```{r condition 1, echo=FALSE, eval=file.exists(params$user2)}
# plot image
```

推荐阅读