firmo23
firmo23

Reputation: 8404

Combine 2 actionbuttons outputs inside the same eventReactive()

I have the shiny app below in which I have 2 file inputs and 2 actionbuttons.

The 1st submit button 'submit' is used to trigger the table and the 2nd 'reset' to reset every file input.

When the 1st actionbutton is empty I get a warning message.

Which table is going to be displayed depends on the if conditions inside the log(). The issue is that when reset is pressed the file inputs are getting empty but the table remains.

Ideally every table should be displayed after clicking the submit button and when reset is clicked the file inputs are getting empty the table is disappeared and the warning message is displayed again. What I think I need is something like an a combination of the 2 actionbuttons (input$submit,input$reset) inside the log().

library(shiny)
library(shinyjs)
library(tidyverse)
library(DT)

ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
      useShinyjs(),
      fileInput('inFile', 'Choose 1st file'),
      fileInput('inFile2', 'Choose 2nd file'),
      actionButton('submit', 'Submit'),
      tags$hr(),
      actionButton('reset', 'Reset')
    ),
    mainPanel(

      uiOutput("choose"),
      DTOutput( "choose2" )
    )
  )
)

server <- function(input, output, session) {

  rv <- reactiveValues(
    data = NULL,
    clear = FALSE
  )
  rv2 <- reactiveValues(
    data = NULL,
    clear = FALSE
  )

  ########1st
  observe({
    req(input$inFile)
    req(!rv$clear)

    rv$data <- read.csv(input$inFile$datapath,header = T)



  })

  observeEvent(input$inFile, {
    rv$clear <- FALSE
  }, priority = 1000)

  observeEvent(input$reset, {
    rv$data <- NULL
    rv$clear <- TRUE
    reset('inFile')
  }, priority = 1000)


  #############2nd
  observe({
    req(input$inFile2)
    req(!rv2$clear)

    rv2$data <- read.csv(input$inFile2$datapath,header = T)



  })

  observeEvent(input$inFile2, {
    rv2$clear <- FALSE
  }, priority = 1000)

  observeEvent(input$reset, {
    rv2$data <- NULL
    rv2$clear <- TRUE
    reset('inFile2')
  }, priority = 1000)




  output$choose <- renderUI ({
    if(is.null(rv$data))
    {
      "You must upload 1st csv at least"
    }
    else
    {
      return(NULL)

    }
  })
  log<-eventReactive({input$submit }, {
    if(is.null(rv$data)){
      return(NULL)
    }
    if(!is.null(isolate(rv$data))){

      if(!is.null(isolate(rv2$data))){
        datatable(input$inFile2$datapath)
      }
      else if(is.null(isolate(rv2$data))){
        datatable(input$inFile$datapath)
      }
    }
  })

  output$choose2<-renderDataTable({

    log()
  })
}

shinyApp(ui, server)

Upvotes: 1

Views: 48

Answers (1)

JD Caddell
JD Caddell

Reputation: 403

You can add multiple actions into eventReactive() by placing them in a vector.

eventReactive(
  c(action1, action2),{

  }

Upvotes: 1

Related Questions