【问题标题】:updateSelectInput does not update the input$id value of selectInputupdateSelectInput 不更新 selectInput 的 input$id 值
【发布时间】:2021-11-03 18:24:30
【问题描述】:

我正在开发一个用于教育目的的应用程序,该应用程序应该显示用户上传的数据图。要求用户上传一个 csv 文件,然后从所述文件中选择两个将被绘制的变量。我提供了两个可与 repex 一起使用的数据文件的代码:uniformData.csv 包含来自均匀分布的两列数据和一个分组列; normalData.csv 包含两列来自正态分布的数据和一个分组列。在下面我描述的示例中,我首先上传 uniformData.csv,选择 x_unif 和 y_unif 变量进行绘图,然后上传 normalData.csv。

由于这个想法是用户可以根据需要尝试多个数据文件,因此每次用户上传新数据文件时,我都会使用 observeEvent() 和 updateSelectInput() 更新我的应用程序的 selectInput 控件选项。这工作正常。我想让用户选择他们选择的变量,因此我在对 updateSelectInput() 的调用中设置了 selected = character(0)。我不希望自动选择上传数据文件的第一列,因为我希望用户有意识地选择他们的 x 和 y 变量。

应用程序的第一个周期运行良好。我可以上传一个数据文件(例如,uniformData.csv),选择一个 x (x_unif) 和一个 y (y_unif) 变量,然后生成绘图。但是,上传新文件时会出现问题。我上传了一个数据文件,两个 selectInput 控件的选项得到更新,但 selectInput 的值没有设置为 character(0)。 input$x 的值和 input$y 的值在新数据文件上传后不会改变。 input$x 的值仍然是“x_unif”,而 input$y 的值仍然是“y_unif”(如果上传了 uniformData.csv 并选择了相应的 x 和 y 变量)。我认为设置 selected = character(0) 会将 input$x 和 input$y 的值设置为 ""。

在 repex 中,我使用 verbatimTextOutput() 打印输入 ID 为“x”的 selectInput 的值。您可以在绘图区域的正上方找到它。在那里您可以看到,即使在上传第二个文件之后, input$x 的值也不会像我想象的那样更改为 "" 。 该图不会引发错误,因为我在 renderPlot() 中使用 req(x(), y()) 来捕获任何不真实的输入(如果我不使用 req() 上传后会出现错误第二个数据文件)。

有谁知道使用 updateSelectInput() 或其他方法设置 input$x 和 input$y = "" 的方法吗?不幸的是,我知道零 JavaScript,这可能会解决问题吗?如果您有任何想法,我将不胜感激,并提前感谢您。

library(shiny)
ui <- fluidPage(fluidRow(
  column(
    width = 4,
    fileInput(
      inputId = "file",
      label = "Upload your datafile",
      accept = ".csv"
    ),
    selectInput(
      inputId = "x",
      label = "Select x variable:",
      choices = NULL
    ),
    selectInput(
      inputId = "y",
      label = "Select a y variable",
      choices = NULL
    )
  ),
  column(
    width = 8,
    verbatimTextOutput("selectInputVal"),
    plotOutput("plot")
  )
  
))

server <- function(input, output, session) {
  data <- reactive({
    req(input$file)
    read.table(
      input$file$datapath,
      sep = ",",
      dec = ".",
      header = TRUE
    )
  })
  
  observeEvent(eventExpr = input$file,
               handlerExpr = {
                 updateSelectInput(session,
                                   "x",
                                   choices = names(data()),
                                   selected = character(0))
                 
                 
                 updateSelectInput(session,
                                   "y",
                                   choices = names(data()),
                                   selected = character(0))
               })
  
  x <- reactive({
    req(data())
    req(input$x)
    data()[[input$x]]
  })
  
  y <- reactive({
    req(data())
    req(input$y)
    data()[[input$y]]
  })
  
  output$selectInputVal <- renderPrint(input$x)
  output$plot <- renderPlot({
    req(x(), y())
    plot(x(), y())
  })
}
runApp(shinyApp(ui = ui, server = server))



# Create example data filies:
RNGversion("4.1.0")
set.seed(123)
normalData <- data.frame(
  x_norm = rnorm(n = 10, mean = 100, sd = 15),
  y_norm = rnorm(n = 10, mean = 50, sd = 10),
  grp_ab = sample(c("a", "b"), size = 10, replace = TRUE)
)

uniformData <- data.frame(
  x_unif = runif(n = 20, min = 0, max = 10),
  y_unif = runif(n = 20, min = -10, max = 10),
  grp_cd = sample(c("c", "d"), size = 20, replace = TRUE)
)

# write.csv(normalData, "normalData.csv", quote = FALSE, row.names = FALSE)
# write.csv(uniformData, "uniformData.csv", quote = FALSE, row.names = FALSE)

【问题讨论】:

  • 试试observeEvent(eventExpr = input$file, {...})。它对我来说很好。
  • 无法复制错误。对我来说很好。你能附上一些截图吗?
  • 感谢大家关注我的问题! @YBS - 我更改了代码,但 input$x 的值最终没有更新 - 我现在已经包含了一些 scrrenshots 并添加了一些关于我发现错误的位置的更多详细信息(在 verbatimTextOutput() 字段中。跨度>
  • @mnist - 我添加了屏幕截图 - 没有发生“错误”,因为我在 renderPlot() 中使用 req() 捕获了非真实反应 - 但在逐字文本输出中,您可以看到 input$x 的值第二个数据文件上传后不更新!

标签: r shiny


【解决方案1】:

出现问题是因为selectInput 无法将character(0) 处理为有效选择,因为它未在choices 中列出,但是当multiple = FALSE 也无法进行选择时。

您可以使用selectizeInput,设置multiple = TRUE,它允许空输入,但也可以通过maxItem 选项将其限制为一个选项

library(shiny)
ui <- fluidPage(fluidRow(
    actionButton("browser", "Browser"),
    column(
        width = 4,
        fileInput(
            inputId = "file",
            label = "Upload your datafile",
            accept = ".csv"
        ),
        selectizeInput(
            inputId = "x",
            label = "Select x variable:",
            multiple = TRUE,
            choices = NULL,
            options = list(maxItems = 1)
        ),
        selectizeInput(
            inputId = "y",
            label = "Select a y variable",
            multiple = TRUE,
            choices = NULL,
            options = list(maxItems = 1)
        )
    ),
    column(
        width = 8,
        verbatimTextOutput("selectInputVal"),
        plotOutput("plot")
    )

))

server <- function(input, output, session) {
    data <- reactive({
        req(input$file)
        read.table(
            input$file$datapath,
            sep = ",",
            dec = ".",
            header = TRUE
        )
    })



    observeEvent(eventExpr = input$file,
                 handlerExpr = {
                     updateSelectizeInput(session,
                                          "x",
                                          choices = names(data()),
                                          selected = NULL)


                     updateSelectizeInput(session,
                                          "y",
                                          choices = names(data()),
                                          selected = NULL)
                 })

    x <- reactive({
        req(data())
        req(input$x)
        data()[[input$x]]
    })

    y <- reactive({
        req(data())
        req(input$y)
        data()[[input$y]]
    })

    output$selectInputVal <- renderPrint(input$x)
    output$plot <- renderPlot({
        req(x(), y())
        plot(x(), y())
    })
}
runApp(shinyApp(ui = ui, server = server))

【讨论】:

  • 谢谢,我明白了,multiple = FALSE 也是updateSelectInput(session, "x", choices = c("", names(data())),selected = "")updateSelectInput(session, "x", choices = c("", names(data())), selected = NULL) 也不起作用的原因吗?感谢您指出选项 - 从来没有想过使用 selectizeInput 的选项参数!
猜你喜欢
  • 1970-01-01
  • 2021-10-28
  • 2017-06-16
  • 2018-07-09
  • 2022-01-15
  • 1970-01-01
  • 2013-10-01
  • 2015-10-27
  • 2019-02-11
相关资源
最近更新 更多