Ich möchte mehrere .html mit R erstellen, indem ich eine for-Schleife verwende, die die Funktion rmarkdown :: render enthält und mehrere Parameter (params) mit Bedingungen enthält. Wie kann ich in diesem Fall mehrere Parameter festlegen?

Ich habe den folgenden Code ausprobiert, der nicht funktioniert.

Beispiel für die Rmd-Vorlage:

---
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.

Die for-Schleife, die erstellt wurde, um das HTML aus der Rmd-Vorlage mit verschiedenen Parametern zu erstellen:

# 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))
}

Der Fehler:

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 "}"
> 

Was ich möchte, dass die for-Schleife ein HTML mit dem Namen iri2015USA.html erzeugt, wenn das Land == USA mit den anderen Parametern als Wort == Amerikanisch und die Vereinigten Staaten bezahlt.

Es sollte ein HTML mit dem Namen iri2015Canada.html erzeugen, wenn country == Canada mit den anderen Parametern als word == Canadian und pays == Canada.

Etc.

Danke vielmals.

0
Marine Leroi 26 Juni 2019 im 00:38

3 Antworten

Beste Antwort

Betrachten Sie switch ähnlich wie in anderen Sprachen (Java, C #, C ++, PHP, Perl):

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)
  )              
}
0
Parfait 26 Juni 2019 im 02:28
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)
}
0
Marine Leroi 26 Juni 2019 im 18:11

Betrachten Sie sogar Map (Wrapper zu mapply), die elementweise Schleife durch Vektoren gleicher Länge (dh Spalten eines Datenrahmens), die auf Daten skaliert werden, ohne switch oder {{X3 anzupassen }} Code.

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
0
Parfait 26 Juni 2019 im 18:40