首页 > 解决方案 > 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,当国家 == 加拿大时,其他参数为单词 == 加拿大并支付 == 加拿大。

等等

非常感谢。

标签: rfor-loopparameters

解决方案


考虑 even Map(wrapper to mapply),它是通过等长向量(即数据帧的列)的元素循环,无需调整switchif编码即可缩放到数据。

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

推荐阅读