r - 如何不运行会出错的块
问题描述
我正在编写一个带有每周更新的图像的 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 包从参数中指定的路径读取每个图像。
解决方案
您也可以将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
```
推荐阅读
- ms-access - 查询表达式中的语法错误(缺少运算符) - Access16
- sql - 根据 UTC 日期按天分组和聚合,调整为指定的时区偏移量
- matplotlib - 使用 Pylab&Pyplot 生成 mollweide 投影图。此外,点的注释值将显示
- c++ - std::is_base_of 和 std::is_convertible 之间的区别
- credentials - 无法删除 Google OAuth 客户端 ID
- ios - Unity Xcode Build“自动签名无法解决问题”
- css - First-of-type 选择器适用于父级的所有子级
- bayesian - Dirichlet 参数在 JAGS 中不更新
- c++ - 为什么 Rcpp::Function 可以用作 boost::function,并且可以在运行时进行自省?
- stripe-payments - 为什么我在 Stripe Connect 部分看不到我的关联账户