简体   繁体   中英

R Shiny ObserveEvent with multiple inputs

The below code is working, but I need to enhance it by observing multiple inputs.

I need a multiple observeEvent on input$dateinput which selects the xlsx file to open and input$myfilter which checks if the user wants to apply a specific filter to the data.

but when I change

observeEvent(input$dateinput,... to:

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

The app crashes with Warning: Error in file: invalid 'description' argument [No stack trace available]

The code otherwise runs fine. Any help? thanks

full code: EDIT: THIS IS NOW REPRODUCIBLE AND DOES NOT REQUIRE ANY EXCEL FILE

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)

For multiple observes in observeEvent() wrap them in curly brackets without commas, just as regular code.

Try:

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

In my experience it can be safer to wrap complex observeEvent expressions ( handlerExpr ) in isolate({}) to suppress any undesired reactivity.

When your observer reacts to input$myfilter , it is triggered at the startup. And input$dateinput is NULL . So you get this error:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM