我想通过使用包括rmarkdown :: render函数的for循环并包含带条件的多个参数(参数),使用R创建多个.html。在这种情况下,如何设置多个参数?
我尝试了以下无效的代码。
Rmd模板示例:
Dim con As ADODB.Connection
Dim oResult As ADODB.Recordset
Private Sub createDbConnection()
On Error Resume Next
Set con = New ADODB.Connection
con.CommandTimeout = 180
con.Open anyconnection ‘ any connection
End Sub
Private Sub closeDbConnection()
On Error Resume Next
con.Close
End Sub
Private Sub execsql(ByVal Sqlstr As String, ByVal Destination As Range)
Dim oResult As ADODB.Recordset
Set oResult = New ADODB.Recordset
createDbConnection
execsql = 0
With oResult
.ActiveConnection = con
.Open Sqlstr
.Destination.CopyFromRecordset
End With
'Destination.CopyFromRecordset oResult
execsql = 1
closeDbConnection
End Sub
Sub runsql()
Dim Sqlstr
Dim Destination
Sqlstr = ThisWorkbook.Sheets("SQL").Range("f3").Value
Destination = ThisWorkbook.Sheets("SQL").Range("f11")
execsql Sqlstr, Destination
End Sub
创建的for循环用于使用不同的参数从Rmd模板生成html:
---
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.
错误:
# 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))
}
我希望for循环产生的是一个名为iri2015USA.html的html,当country == USA且其他参数为word == American并支付== United States时。
当国家==加拿大,其他参数为单词==加拿大,并支付==加拿大时,它将产生一个名为iri2015Canada.html的html。
等
非常感谢。
答案 0 :(得分:0)
请考虑与其他语言(Java,C#,C ++,PHP,Perl)类似使用的switch
:
for (country in c("Canada", "USA", "France", "Germany", "UK", "Japan")) {
switch (country,
"Canada" = {
word <- "Canadian"
pays <- "Canada"
},
"USA" = {
word <- "American"
pays <- "the United States"
},
"Germany" = {
word <- "German"
pays <- "Germany"
},
"France" = {
word <- "French"
pays <- "France"
},
"UK" = {
word <- "British"
pays <- "The United Kingdom"
},
"Japan" = {
word <- "Japanese"
pays <- "Japan"
})
input <- paste0(directory, "/", "iri2015template", ".Rmd")
output <- paste0(directory, "/","iri2015", country, ".html")
tryCatch(rmarkdown::render(input,
params = list(country = country, word = word, pays = pays),
output_file = output)
, error = function(e) print(e)
)
}
答案 1 :(得分:0)
countries <- c("Germany",
"France",
"UK",
"USA",
"Japan",
"Canada")
for (country in countries) {
nationality <- switch (country,
"USA" = "American",
"France" = "French",
"Germany" = "German",
"UK" = "British",
"Japan" = "Japanese",
"Canada" = "Canadian")
name <- switch (country,
"USA" = "the United States",
"France" = "France",
"Germany" = "Germany",
"UK" = "the United Kingdom",
"Japan" = "Japan",
"Canada" = "Canadian")
input <- paste0(directory, "/", "iri2015template", ".Rmd")
output <- paste0(directory, "/","iri2015", country, ".html")
rmarkdown::render(input, params = list(country = country, nationality = nationality, name = name), output_file = output)
}
答案 2 :(得分:0)
甚至考虑Map
(包装到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