我可以从服务器中的反应函数中获得 ui 中 SelectInput 的选择吗? (R闪亮)
Could I get the choices of a SelectInput in the ui from a reactive function in server? (RShiny)
我在服务器中创建了一个反应函数来获取元素列表。这个想法是将列表的每个元素显示为 ui 中的单独选择,就像 selectInput 在 ui.
中所做的一样
我用mtcars写了个例子
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = options, "Select one", choices = cars())
),
mainPanel(
verbatimTextOutput("print")
)
)
)
server <- function(input, output) {
cars <- reactive({
data("mtcars")
cars <- list(rownames(mtcars))
return(cars)
})
output$print <- renderPrint(cars())
}
# Run the application
shinyApp(ui = ui, server = server)
如果您删除或评论此行:ui 中的 selectInput(inputId = options, "Select one", choices = cars())
,该应用程序将正常运行。输出是这样的:
[[1]]
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
但是,如果我尝试在 ui 中输入 select 以显示该列表,我会收到此错误:Error in cars (): could not find function "cars"
我不知道是否可行,但我可以在 ui 的 selection bar 中显示该列表的每个元素吗?为了让用户能够 select 一个选项并执行我必须编程的其他事情。
非常感谢,
此致
在服务器端生成selectInput
。
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
uiOutput('selectUI')
),
mainPanel(
verbatimTextOutput("print")
)
)
)
server <- function(input, output) {
cars <- reactive({
data("mtcars")
cars <- rownames(mtcars)
return(cars)
})
output$selectUI <- renderUI({
selectInput(inputId = 'options', "Select one", choices = cars())
})
output$print <- renderPrint(cars())
}
# Run the application
shinyApp(ui = ui, server = server)
我在服务器中创建了一个反应函数来获取元素列表。这个想法是将列表的每个元素显示为 ui 中的单独选择,就像 selectInput 在 ui.
中所做的一样我用mtcars写了个例子
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
selectInput(inputId = options, "Select one", choices = cars())
),
mainPanel(
verbatimTextOutput("print")
)
)
)
server <- function(input, output) {
cars <- reactive({
data("mtcars")
cars <- list(rownames(mtcars))
return(cars)
})
output$print <- renderPrint(cars())
}
# Run the application
shinyApp(ui = ui, server = server)
如果您删除或评论此行:ui 中的 selectInput(inputId = options, "Select one", choices = cars())
,该应用程序将正常运行。输出是这样的:
[[1]]
[1] "Mazda RX4" "Mazda RX4 Wag" "Datsun 710"
[4] "Hornet 4 Drive" "Hornet Sportabout" "Valiant"
[7] "Duster 360" "Merc 240D" "Merc 230"
[10] "Merc 280" "Merc 280C" "Merc 450SE"
[13] "Merc 450SL" "Merc 450SLC" "Cadillac Fleetwood"
[16] "Lincoln Continental" "Chrysler Imperial" "Fiat 128"
[19] "Honda Civic" "Toyota Corolla" "Toyota Corona"
[22] "Dodge Challenger" "AMC Javelin" "Camaro Z28"
[25] "Pontiac Firebird" "Fiat X1-9" "Porsche 914-2"
[28] "Lotus Europa" "Ford Pantera L" "Ferrari Dino"
[31] "Maserati Bora" "Volvo 142E"
但是,如果我尝试在 ui 中输入 select 以显示该列表,我会收到此错误:Error in cars (): could not find function "cars"
我不知道是否可行,但我可以在 ui 的 selection bar 中显示该列表的每个元素吗?为了让用户能够 select 一个选项并执行我必须编程的其他事情。
非常感谢,
此致
在服务器端生成selectInput
。
library(shiny)
ui <- fluidPage(
titlePanel("Old Faithful Geyser Data"),
sidebarLayout(
sidebarPanel(
uiOutput('selectUI')
),
mainPanel(
verbatimTextOutput("print")
)
)
)
server <- function(input, output) {
cars <- reactive({
data("mtcars")
cars <- rownames(mtcars)
return(cars)
})
output$selectUI <- renderUI({
selectInput(inputId = 'options', "Select one", choices = cars())
})
output$print <- renderPrint(cars())
}
# Run the application
shinyApp(ui = ui, server = server)