反应式 selectInput 选项和选定值无法正确重置为 NULL

Posted

技术标签:

【中文标题】反应式 selectInput 选项和选定值无法正确重置为 NULL【英文标题】:reactive selectInput choices and selected values cannot be reset to NULL correctly 【发布时间】:2017-10-20 03:46:14 【问题描述】:

在我的 Shiny 应用程序中,我会将input$columns 中选择的列弹出到input$vars,然后将input$vars 中的选择弹出到input$fromvars 以供进一步选择。但是,当我取消选择inut$columns 中的所有列时,我的代码无法正常工作(我用renderPrint 进行了测试,它显示input$columns 是“NULL”),因为我期望不仅input$vars 值是NULL(它可以工作),而且这个selectInput中的选项也应该是NULL,下拉列表中没有任何内容可供选择,但是,我点击下拉列表,发现显示最后一个input$columns中的最后一个选择。

更糟糕的是input$fromvars,我希望它的值被重置为 NULL,因为 input$vars 被重置为 NULL(在input$columns 中的选择发生变化时),选择也应该被重置为 NULL,这样在下拉列表。但是,除非我手动更新input$vars,否则这不起作用。

请在下面找到我的代码,有人可以帮忙看看吗?

if(interactive()) 

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel( 

      fileInput("file", "Choose CSV File",
                # limit the file extention to the following:
                accept=c("text/csv", 
                         "text/comma-separated-values,text/plain", 
                         ".csv")),

      h5("Upload file for data analysis"),


      # no choices before a file uploaded
      uiOutput("columns"),

      # inputs to generate pivot table (demo in a single tab here, but in real app, this will be on a different tab)
      selectInput("vars", "vars to use:", choices = NULL, multiple = TRUE, selected = NULL),
      selectInput("fromvars", "select from vars:", choices = NULL, multiple = TRUE, selected = NULL)

      ),

    mainPanel( 
      verbatimTextOutput("print")
    ))
)

server <- function(input, output, session) 

  dt <- reactiveValues()

  # upload file
  observeEvent(input$file, 
    inFile <- input$file
    req(inFile)
    # upload the file to dataset
    dt$data = read.csv(inFile$datapath, header = TRUE)
  )


  # create columns groupcheckbox ui
  output$columns <- renderUI(

    # get the col names of f and assign them to a list
    cols = mapply(list, names(dt$data))

    # render column group checkbox ui after loading the data
    checkboxGroupInput("columns", "Select columns to display", choices = cols, selected = cols)
  )

  #########################################
  # blocks with problem
  #########################################

  ######## dependent on columns selection ########
  observeEvent(input$columns, 
    dt$cols <- input$columns
    # update input$vars for pivottable tab
    updateSelectInput(session, "vars", "vars to use:", choices = dt$cols, selected = NULL)
  , ignoreNULL = FALSE)


  observeEvent(input$vars, 
    if( is.null(input$vars))  
      updateSelectInput(session, "fromvars", "select from vars:", choices = NULL, selected = NULL)  
    
    else 
      updateSelectInput(session, "fromvars", "select from vars:", choices = isolate(input$vars), selected = NULL)
    
  , ignoreNULL = FALSE)

  output$print <- renderPrint(
    list(
      paste("input$vars:",input$vars), 
      paste("input$columns:", input$columns),
      paste("input$fromvars:", input$fromvars)
    )
  )



shinyApp(ui = ui, server = server)

【问题讨论】:

【参考方案1】:

基本上你很接近。唯一真正的问题是 updateSelectInput 似乎喜欢其 NULL 参数中的 choices 值,因此您必须避免使用这些值。

我通过进行以下更改使代码正常工作:

observerEvent(input$columns, 中添加了一条语句,该语句将dt$cols 设置为空字符串"",如果它具有值NULLobserverEvent(input$vars,choices=NULL 更改为choices="" 调整了renderPrint 语句以制作更好的打印输出(使用双pastes,因此标签不会重复。

这是要验证的代码:

ui <- fluidPage(

  sidebarLayout(
    sidebarPanel( 

      fileInput("file", "Choose CSV File",
                # limit the file extention to the following:
                accept=c("text/csv", 
                         "text/comma-separated-values,text/plain", 
                         ".csv")),

      h5("Upload file for data analysis"),

      # no choices before a file uploaded
      uiOutput("columns"),

      # inputs to generate pivot table (demo in a single tab here, but in real app, this will be on a different tab)
      selectInput("vars", "vars to use:", choices = NULL, multiple = TRUE, selected = NULL),
      selectInput("fromvars", "select from vars:", choices = NULL, multiple = TRUE, selected = NULL)
    ),
    mainPanel( 
      verbatimTextOutput("print")
    ))
)

server <- function(input, output, session) 

  dt <- reactiveValues()

  # upload file
  observeEvent(input$file, 
    inFile <- input$file
    req(inFile)
    # upload the file to dataset
    dt$data = read.csv(inFile$datapath, header = TRUE)
  )

  # create columns groupcheckbox ui
  output$columns <- renderUI(

    # get the col names of f and assign them to a list
    cols = mapply(list, names(dt$data))

    # render column group checkbox ui after loading the data
    checkboxGroupInput("columns", "Select columns to display", choices = cols, selected = cols)
  )

  #########################################
  # blocks with problem
  #########################################

  ######## dependent on columns selection ########
  observeEvent(input$columns, 
    dt$cols <- input$columns
    if (is.null(dt$cols)) dt$cols <- ""
    # update input$vars for pivottable tab
    print("observerEvent")
    print(dt$cols)

    updateSelectInput(session, "vars", "vars to use:", choices = dt$cols, selected = NULL)
  , ignoreNULL = FALSE)


  observeEvent(input$vars, 
    if( is.null(input$vars))  
      updateSelectInput(session, "fromvars", "select from vars:", choices = "", selected = NULL)  
    
    else 
      updateSelectInput(session, "fromvars", "select from vars:", choices = input$vars, selected = NULL)
    
  , ignoreNULL = FALSE)

  output$print <- renderPrint(
    list(
      paste("input$columns:", paste(input$columns,collapse=",")),
      paste("input$vars:",paste(input$vars,collapse=",")), 
      paste("input$fromvars:", paste(input$fromvars,collapse=","))
    )
  )

shinyApp(ui = ui, server = server)

这就是它的样子:

【讨论】:

谢谢!这解决了!我想知道为什么 Shiny 包中没有记录这一点。 抱歉刚刚做了。谢谢! 谢谢。也检查您的旧答案。至少其中一个在我看来你应该接受。

以上是关于反应式 selectInput 选项和选定值无法正确重置为 NULL的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 actionButton 更改 R Shiny 中 selectInput 上的选定值?

从 SelectInput (R Shiny) 中的分组选项列表中获取组标签

R Shiny:修改选择后保留/保留反应输入的值

如何在 selectInput R Shiny 中为一长串选项设置值

基于选项卡面板选择在 R Shiny 中显示/隐藏 selectinput

闪亮的下拉复选框输入