【发布时间】:2021-10-28 07:58:29
【问题描述】:
我无法更新选择输入的输入选项。我以为我有我需要的一切 - observe、updateSelectInput 和必要的反应,但它不起作用。
library(shiny)
library(ggplot2)
ui <- fluidPage(
sidebarLayout(
sidebarPanel(selectInput("dataset", label = "Choose dataset", choices = c(" ", "diamonds", "mtcars", "mpg")),
selectInput("x", "X", choices = NULL),
selectInput("y", "Y", choices = NULL),
selectInput("group", "Group", choices = NULL),
actionButton("update", "Update")),
mainPanel(plotOutput("plot"), verbatimTextOutput("codetext"))
))
server <- function(input, output, session) {
data <- reactive({
input$dataset
})
observe({
updateSelectInput(session, "x", choices = c(" ", names(data())))
updateSelectInput(session, "y", choices = c(" ", names(data())))
updateSelectInput(session, "group", choices = c(" ", names(data())))
})
code <- reactive({
req(data())
req(input$x)
req(input$y)
paste("ggplot(", data(),",aes(x = ", input$x, ",y = ", input$y, ")) + geom_boxplot()")
})
output$plot <- renderPlot({
req(input$update)
req(code())
req(input$x)
req(input$y)
eval(parse(text = code()))
})
output$codetext <- renderText({
code()
})
}
shinyApp(ui, server)
【问题讨论】: