闪亮:updateSelectizeInput 抛出错误“$ 运算符对于原子向量无效”

2023-12-30

我是闪亮的新手,很难弄清楚这一点。我试图在 selectizeInput 中创建一个“全选”按钮,但在将输入从 selectizeInput 传递到 updateSelectizeInput 时遇到错误。有人可以帮我解决这个问题吗?

当我从输入框中选择“全选”时,应用程序关闭并显示错误:“$ 运算符对于原子向量无效”

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

脚本的数据:https://drive.google.com/file/d/0B_sB3ef_9RntOWJjNlhrUjk3a1k/view 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 的输入字段中选择“全选”时,会出现错误

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

Thanks!


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

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

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

shinyServer(function(input, output, session) { 

.......

} 

希望能帮助到你!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

闪亮:updateSelectizeInput 抛出错误“$ 运算符对于原子向量无效” 的相关文章

随机推荐