r-以编程方式从字符串中构造Switch语句和selectInput选项



字符串可以按照https://stackoverflow.com/a/1743796/1659890如下所示:

eval(parse(text=paste0("c(",paste(c("'a'","'b'","'c'"),
c("'z'","'y'","'x'"), sep = "=", collapse = ","),")")))
c('a' = 'z', 'b' = 'y', 'c' = 'x')

这样的表达式在Shiny应用程序和Switch语句中很有用,即

switch(EXPR = "a",
       "a" = "z",
       "b" = "y",
       "c" = "x"
       )
shinyApp(
  ui = fluidPage(
    uiOutput("selectUI"),
    "The selected value is:",
    textOutput("selectedValue")
  ),
  server = function(input, output, session){
    output$selectUI <- renderUI(
      selectInput("selectVariable",
                  choices = c("a" = "z",
                              "b" = "y"),
                  label = "My Label")
    )
    output$selectedValue <- renderText(input$selectVariable)
  }
)

在R语言中,如何通过以下输入以编程方式完全构建这样的表达式?承认"必须替换为"

list1 <- c("a","b","c")
list2 <- c("z","y","x")

或者更可能的情况是具有两列CCD_ 2的CCD_。

do.call会有所帮助。传递一个列表,其中第一个参数是EXPR,其他参数是命名的备选方案列表:

list1 <- c("a","b","c")
list2 <- list("z","y","x")
names(list2) <- list1
do.call("switch", c("a", list2))

最新更新