How to use two actionButton for passing the same two values to two different datatables in a shiny app?


#1

I have a simple shiny app that creates two data tables after getting inputs from 2 date inputs. The problem is that I want to be able to create the data tables after selecting values for both date inputs. I do not know if it is possible with only one actionbutton.

location = c("100 ail","16th and Whitmore","40AB01 - ANTWERPEN","100 ail","16th and Whitmore","40AB01 - ANTWERPEN") 
date = c("2015-09-01 00:00:00","2016-03-06 19:00:00","2016-11-22 15:00:00","2018-02-01 09:30:00", "2018-02-01 03:00:00", "2017-03-07 10:00:00") 
pm25=c("FALSE","FALSE","FALSE","FALSE","FALSE","FALSE")
pm10=c("TRUE","FALSE","FALSE","TRUE","FALSE","FALSE")
no2=c("TRUE","FALSE","FALSE")
latitude=c(47.932907,41.322470,36.809700,47.932907,41.322470,36.809700)
longitude=c(106.92139000,-95.93799000
            ,-107.65170000,106.92139000,-95.93799000
            ,-107.65170000)

df = data.frame(location, date,latitude,longitude,pm25,pm10,no2)

ui = fluidPage(

  uiOutput("dt"),
  actionButton("button1", "Action 1"),
  uiOutput("dt2"),
  actionButton("button2", "Action 2"),

  shiny::dataTableOutput("merged"),
  shiny::dataTableOutput("merged2")
)

#server.r

#df$location <- gsub( " " , "+" , df$location)
server = function(input, output, session) {







  output$dt<-renderUI({

    dateInput('date',
              label = 'First Available Date',
              value = df$date
    )           


  })
  output$dt2<-renderUI({

    dateInput('date2',
              label = 'Last available Date',
              value = df$date
    )            


  })
  output$merged <- shiny::renderDataTable({
    df %>%
      filter(date >= input$dt & date <= input$dt2)
  })
  output$merged2 <- shiny::renderDataTable({
    df %>%
      filter(date >= input$dt & date <= input$dt2)
  })
}

shinyApp(ui = ui, server = server)

#2

Hi,

I’m not sure if I understand your need well but may be you should use a “dateRangeInput” element instead of 2 “dateInput” elements ?

HTH :slight_smile:

Best regards,

A.D.