闪亮:updateSelectizeInput 抛出错误“$ operator is invalid for atomic vectors”

Shiny: updateSelectizeInput is throwing the error "$ operator is invalid for atomic vectors"

我是 shiny 的新手,很难弄明白这一点。我试图在我的 selectizeInput 中创建一个 "Select ALL" 按钮,但是在将输入从 selectizeInput 传递到 updateSelectizeInput 时出现错误。有人可以帮我解决这个问题吗?

当我从输入框select"Select ALL"时,应用程序关闭并显示错误:“$ operator is invalid for atomic vectors”。 =14=]

我在输入字段中添加了 "Select All" (selectizeInput())。当用户单击 "Select All" 时,updateSelectizeInput() 添加输入中所有名称与 "Select All" 之间的设置差异,用所有值填充过滤器框。

脚本数据:https://drive.google.com/file/d/0B_sB3ef_9RntOWJjNlhrUjk3a1k/view

这是我的脚本

UI.R

library(shiny)
shinyUI(fluidPage(
  navbarPage(
    "Tab_name",
    tabPanel("Engine",
             bootstrapPage(
               div(style="display:inline-block", fileInput("file_attr", "attributes:")),
               uiOutput("CountryList")
             )         
    )
  )
))

Server.R

library(shiny)
shinyServer(function(input, output) {     
  data_attr <- reactive({
      file1 <- input$file_attr
      if(is.null(file1)){return()} 
      read.table(file=file1$datapath, sep=",", header = TRUE, stringsAsFactors = FALSE)       
    })

    countries <- reactive({
      if(is.null(data_attr()$Country)){return()}
      data_attr()$Country
    })  

    observeEvent(input$file_attr, {
      output$CountryList <- renderUI({
        if(is.null(data_attr()$Country)){return()}
        selectizeInput('show_vars', 'Country Filter', choices = c("Select All",unique(countries())), multiple = TRUE)
      })
    }) 

    observe({
      if ("Select All" %in% input$show_vars){
        selected_choices <- setdiff(c("Select All",unique(countries())), "Select All")
        updateSelectizeInput('show_vars', 'Country Filter', selected = selected_choices)

      }

    })

  })

当我们从 UI

的输入字段 select "select all" 时出现错误
Warning: Error in $: $ operator is invalid for atomic vectors
Stack trace (innermost first):
    58: updateSelectInput
    57: updateSelectizeInput
    56: observerFunc [C:\Users\naresh.ambati\Documents\dummt/server.R#30]
     1: runApp
ERROR: [on_request_read] connection reset by peer

谢谢!

错误是因为你得到的要传递给更新的参数updateSelectizeInput不正确。您需要将 session 对象作为第一个参数传递。在你的情况下,它应该是这样的:

updateSelectizeInput(session,'show_vars', 'Country Filter', selected = selected_choices)

并且当您定义服务器函数时,您需要按如下方式传递 session 对象:

shinyServer(function(input, output, session) { 

.......

} 

希望对您有所帮助!