无效的selectInput选项和选定的值无法正确重置为NULL

时间:2017-05-19 22:18:42

标签: r shiny

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

对于input$vars更糟糕,我希望它的值重置为NULL,因为输入$ vars重置为NULL(在input$columns中更改选择时),选项也应重置为NULL,以便下拉列表中没有显示任何内容。但是,除非我手动更新input$fromvars,否则这不起作用。

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

input$columns

1 个答案:

答案 0 :(得分:2)

基本上你很亲密。唯一真正的问题是,updateSelectInput似乎类似于NULL参数中的choices值,因此您必须避免这些。

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

  • observerEvent(input$columns,中添加了一条语句,如果dt$cols具有值""
  • ,则将NULL设置为空字符串observerEvent(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) ,因此标签不会重复。

以下是要验证的代码:

META-INF/LICENSE

这就是它的样子:

enter image description here