r - R中for循环中具有渲染功能的多个参数
问题描述
我想用 R 创建多个 .html,方法是使用包含 rmarkdown::render 函数的 for 循环并包含多个带条件的参数(params)。在这种情况下如何设置多个参数?
我尝试了以下不起作用的代码。
Rmd 模板示例:
---
title: "The ranking of `r params$word` companies"
output: distill::distill_article
params:
country: "USA"
word: "American"
pays: "the United States"
---
Figure 1. Number of businesses in `r params$pays`
```{r}
dataGraph1 <- filter(dataGraph7, country==params$country)
plot(dataGraph1)
```
It is interesting to observe the progress of those `r params$word` businesses.
创建用于从具有不同参数的 Rmd 模板生成 html 的 for 循环:
# Directory containing input (and output) files
directory <- "~/thelink"
for (country in c("Canada", "USA", "France", "Germany", "UK", "Japan")) {
if (country == "Canada"){
word <- "Canadian"
pays <- "Canada"
} else if (country == "USA"){
word <- "American"
pays <- "the United States"
} else if (country == "Germany") {
word <- "German"
pays <- "Germany"
} else if (country == "France") {
word <- "French"
pays <- "France"
} else if (country == "UK") {
word <- "British"
pays <- "The United Kingdom"
} else (country == "Japan") {
word <- "Japanese"
pays <- "Japan"
}
input <- paste0(directory, "/", "iri2015template", ".Rmd")
output <- paste0(directory, "/","iri2015", country, ".html")
try(rmarkdown::render(input, params = list(country = country, word = word, pays = pays), output_file = output))
}
错误:
Error: unexpected '}' in " }"
> input <- paste0(directory, "/", "iri2015template", ".Rmd")
> output <- paste0(directory, "/","iri2015", country, ".html")
Error in paste0(directory, "/", "iri2015", country, ".html") :
object 'country' not found
> try(rmarkdown::render(input, params = list(country = country, word = word, pays = pays), output_file = output))
Error in rmarkdown::render(input, params = list(country = country, word = word, :
object 'output' not found
> }
Error: unexpected '}' in "}"
>
我希望for循环产生的是一个名为iri2015USA.html的html,当国家==美国时,其他参数为单词==美国并支付==美国。
它应该生成一个名为 iri2015Canada.html 的 html,当国家 == 加拿大时,其他参数为单词 == 加拿大并支付 == 加拿大。
等等
非常感谢。
解决方案
考虑 even Map
(wrapper to mapply
),它是通过等长向量(即数据帧的列)的元素循环,无需调整switch
或if
编码即可缩放到数据。
country_df <- data.frame(
country = c("Germany", "France", "UK", "USA", "Japan", "Canada"),
nationality = c("German", "French", "British", "American", "Japanese", "Canadian"),
names = c("Germany", "France", "the United Kingdom",
"the United States", "Japan", "Canada")
)
# USER-DEFINED FUNCTION OF MULTIPLE ARGS
html_build <- function(country, nationality, names) {
input <- paste0(directory, "/", "iri2015template", ".Rmd")
output <- paste0(directory, "/","iri2015", country, ".html")
rmarkdown::render(input,
params = list(country = country,
nationality = nationality,
name = names),
output_file = output)
}
# ELEMENT WISE (LOOP-HIDING) CALL
Map(html_build, country_df$country, country_df$nationality, country_df$names)
# with(country_df, Map(html_build, country, nationality, names)) # LESS WORDY
推荐阅读
- javascript - 针对不同浏览器,特别是移动浏览器修复我的网站中的文本大小不一致(尝试了 reset.css 和多种字体格式)
- flutter - Flutter如何显示FutureBuilder的snapshot.data?
- android - 与flutter中更改通知提供程序相关的问题
- mysql - 列出所有数据库中的表
- regex - 使用 GREP 和 XARGS 删除 .html URL
- javascript - Three.js Zoom-to-Fit with offset
- android - 我可以在没有@Inject 的任何类上使用@Singleton 或自定义范围吗
- python - 将多个参数限制为每个特定的字符串集
- c# - 创建新的 dotnet 项目时如何修复“分段错误(核心转储)”?
- firebase - 如何在 Arduino IDE 中使用 nodeMCU ESP8266 制作结构嵌套的 Firebase