Strings may be parsed and evaluated as described in https://stackoverflow.com/a/1743796/1659890 as follows:
eval(parse(text=paste0("c(",paste(c("'a'","'b'","'c'"),
c("'z'","'y'","'x'"), sep = "=", collapse = ","),")")))
c('a' = 'z', 'b' = 'y', 'c' = 'x')
Such an expression is useful in Shiny apps and Switch statements, i.e.
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)
}
)
In the R language, how might such an expression be constructed entirely programmatically with the following input? Acknowledging that " must be replaced with '?
list1 <- c("a","b","c")
list2 <- c("z","y","x")
Or the more likely case of a data.frame
with two columns that are as.character
.