r - UpdateSelectInput 不更新选项
问题描述
我无法更新 selectinputs 的输入选项。我以为我有我需要的一切 -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)
解决方案
由于您的数据集名称是字符串,因此您需要使用get()
. 尝试这个
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({
req(input$dataset)
get(input$dataset)
})
observe({
req(data())
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()))
req(data(),input$x,input$y)
if (input$x==" " | input$y==" ") return(NULL)
ggplot(data(), aes(x = .data[[input$x]],y = .data[[input$y]])) + geom_boxplot()
})
# output$codetext <- renderText({
# code()
# })
}
shinyApp(ui, server)
推荐阅读
- node.js - 添加身份验证后 Express GET 请求不起作用
- selenium - 预期的浏览器二进制位置,但无法在默认位置找到二进制文件,没有提供“moz:firefoxOptions.binary”功能,我不知道 w
- ionic-framework - 离子和 MSAL 身份验证
- python - 如何使用 wptools
- pagespeed - 为什么即使在配额限制内,PageSpeed Insights API 也会返回“无法处理请求”?
- flutter - 未找到目标文件“ios”
- javascript - 在一个画布中捕获多个图像 - javascript
- rendering - 如何创建 Vulkan 离线渲染
- sql - Crystal Reports 中查询的 SQL 语法错误
- android - 当我在观察者作业 A 中调用其初始化方法时,为什么不触发观察者作业 B?