我正在创建一个闪亮的应用程序,我希望允许用户为列表中的值指定一个响应名称。在下面的最小示例中,我希望将下拉菜单值(“example1”)指定为列表中值的名称。我正在考虑使用
glue
({{}}
) 使其具有交互性,如下所示:
library(shiny)
ui <- fluidPage(
titlePanel("Make reactive a named list"),
sidebarLayout(
sidebarPanel(
selectInput("choices",
"Make a choice",
c("example1", "example2", "example3"))
),
mainPanel(
textOutput("text")
)
)
)
server <- function(input, output) {
data <- reactive({
list("{{input$choices}}" = TRUE)
})
output$text <- renderPrint({
data()
})
}
shinyApp(ui = ui, server = server)
电流输出:
不幸的是,这不起作用。正如我们从输出中看到的,反应性
input$choices
值未分配给列表。所以我的预期输出是:$example2 [1] TRUE
。所以我想知道是否有人知道如何为这样的列表分配反应值?
您可以使用
setNames
或 names
:
library(shiny)
ui <- fluidPage(
titlePanel("Make reactive a named list"),
sidebarLayout(
sidebarPanel(
selectInput(
"choices",
"Make a choice",
c("example1", "example2", "example3")
)
),
mainPanel(
textOutput("text")
)
)
)
server <- function(input, output) {
data <- reactive({
setNames(list(TRUE), input$choices)
})
output$text <- renderPrint({
data()
})
}
shinyApp(ui = ui, server = server)
#>
#> Listening on http://127.0.0.1:8918