【问题标题】:R Shiny ObserveEvent with multiple inputs具有多个输入的 R Shiny ObserveEvent
【发布时间】:2021-06-06 08:32:26
【问题描述】:

下面的代码可以工作,但我需要通过观察多个输入来增强它。

我需要在 input$dateinput 上选择要打开的 xlsx 文件和 input$myfilter 上的多个观察事件,以检查用户是否要对数据应用特定过滤器。

但是当我改变时

observeEvent(input$dateinput,... 至:

observeEvent( c(input$dateinput, input$myfilter),{

应用程序因Warning: Error in file: invalid 'description' argument [No stack trace available] 而崩溃

否则代码运行良好。有什么帮助吗?谢谢

完整代码:编辑:现在可以重现,不需要任何 EXCEL 文件

library(shiny)
library(shinyWidgets)
library(openxlsx)

opendir <- function(dir = getwd()){
  if (.Platform['OS.type'] == "windows"){
    shell.exec(dir)
  } else {
    system(paste(Sys.getenv("R_BROWSER"), dir))
  }
}

ui <- fluidPage(
  sidebarPanel(
    uiOutput("gpui")
  ),
  mainPanel(
    titlePanel("test app"),
    br(),
    checkboxInput("myfilter", label = "Filter all unnecessary (71, 46, 44) documents", value = TRUE),
    br(),
    tableOutput("datatable")
  )
)

server <- function(input, output, session) {
  rvalues <- reactiveValues()
  rvalues$listfiles <- list.files(pattern=".xlsx")
  
  observeEvent(input$refresh, {
    print(input$dateinput)
    session$reload()
  })
  
  observeEvent(input$openfolder, {
    opendir()
  })
  
  output$gpui <- renderUI({
    tagList(
      actionButton("openfolder", "Open Data Folder"),
      actionButton("refresh", "Refresh data folder"),
      pickerInput("dateinput","Choose the date", choices=isolate(rvalues$listfiles), options = list(`actions-box` = TRUE),multiple = F)
    )
  })
  
  observeEvent(input$myfilter,{
    print("myfilter")
  })
  
  observeEvent( input$dateinput ,{
      print(input$dateinput)
      print("selecteddata")
      cols <- c("Purchasing.Document",  "Net.Order.Value",  "Currency", "G/L.Account",
                "Short.Text",
                "Requisitioner",  "Release.indicator",  "Deletion.indicator")
      seldata <- read.xlsx(input$dateinput)
      print(nrow(seldata))
      seldata <- seldata[,cols]
      myfilter <- substr(seldata$Purchasing.Document,1,2) %in% c("71", "46", "44")
      if(input$myfilter)  {
        rvalues$data <- seldata[myfilter,]
      }
    rvalues$data <- seldata
    })
  
  output$datatable <- renderTable(
    
    
    rvalues$data, 
    striped = T,
    spacing = 's'
  )
}
shinyApp(ui, server)

【问题讨论】:

  • read.xlsx(input$dateinput) 听起来不对,因为input$dateinput 不是文件名。或者可能因为执行此代码时input$dateinputNULL 而发生此错误。
  • 不,即使没有dateinput,错误仍然存​​在,请检查您这边的代码,它没有使用任何 excel 文件。 (并且dateinput NULL 无论如何都不会导致崩溃)
  • “dateinput 为 NULL 无论如何都不会导致崩溃”:确实如此。
  • 不,但是 input$myfilter 确实...
  • 它确实......在我的回答中显示的意义上,我的意思是。

标签: r shiny


【解决方案1】:

对于observeEvent() 中的多个观察,将它们用大括号括起来,不带逗号,就像常规代码一样。

试试:

shiny::observeEvent(
  eventExpr = {
    input$dataInput
    input$myFilter
  }, 
  handlerExpr = {
    # You code to run
  }
)

根据我的经验,将复杂的 observeEvent 表达式 (handlerExpr) 包装在 isolate({}) 中以抑制任何不希望的反应会更安全。

【讨论】:

  • 还是同样的错误,你能不能试试?代码是可重现的
  • 我猜这个错误是由 read.xlsx 引起的,而不是由闪亮引起的
【解决方案2】:

当您的观察者对input$myfilter 做出反应时,它会在启动时触发。而input$dateinputNULL。所以你得到这个错误:

> openxlsx::read.xlsx(NULL)
Error in file(description = xlsxFile) : argument 'description' incorrect

【讨论】:

  • 确实如此。谢谢。
猜你喜欢
  • 2022-01-19
  • 2019-08-01
  • 2017-11-06
  • 1970-01-01
  • 2018-12-05
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
相关资源
最近更新 更多