Home > other >  Using a dropdown and actionbutton to trigger and observeEvent() or eventReactive()
Using a dropdown and actionbutton to trigger and observeEvent() or eventReactive()

Time:02-01

I am building an App and some parts require more computational resources than others. I have a single sidebar where all the controls are located. It contains a dropdown and an apply button.

I would like to only execute some analysis if both the dropdown and then the action button is clicked. i.e. two conditions need to be TRUE in order to execute the code.

App

#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(

    # Application title
    titlePanel("Old Faithful Geyser Data"),

    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
          shinyWidgets::pickerInput(
            inputId = "counterFactual",
            label = h4("Select an Analysis"),
            choices = c("A", "B", "C", "D"),
            selected = "A"
          ),
          actionButton(inputId = "apply", label = "Apply", icon = icon("play"))
        ),

        # Show a plot of the generated distribution
        mainPanel(
           plotOutput("distPlot")
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output) {

  
  event_trigger <- reactive({
    list(input$counterFactual, input$apply)
  })
  
  observeEvent(ignoreInit = TRUE, event_trigger(),
               {
                 if(event_triger()[[1]] == "C" && event_triger()[[2]] == TRUE)
                   print('Hello World')
               })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

CodePudding user response:

Short answer:

In this specific case (i.e. dropdown must be truthy (see shiny::isTruthy() and button needs to be clicked), you can use:

library(shiny)

# Define UI for application that draws a histogram
ui <- fluidPage(
  
  # Application title
  titlePanel("Old Faithful Geyser Data"),
  
  # Sidebar with a slider input for number of bins 
  sidebarLayout(
    sidebarPanel(
      shinyWidgets::pickerInput(
        inputId = "counterFactual",
        label = h4("Select an Analysis"),
        choices = c("A", "B", "C", "D"),
        selected = "A"
      ),
      actionButton(inputId = "apply", label = "Apply", icon = icon("play"))
    ),
    
    # Show a plot of the generated distribution
    mainPanel(
      plotOutput("distPlot")
    )
  )
)

# Define server logic required to draw a histogram
server <- function(input, output) {
  
  
  observeEvent(ignoreInit = TRUE, input$apply,
               {
                 req(input$counterFactual)
                   print('Hello World')
               })
  
}

# Run the application 
shinyApp(ui = ui, server = server)

Check out the server part - I have removed reactive and now we observe button and cotinue (function shiny::req()) only if the dropdown is truthy (not "" in our case).

Some more comments

I do not understand this:

event_triger()[[1]] == "CF1"

Why it can be equal to "CF1", if this input can be only one letter A-D? So in your case Hello World won't be printed at all.

Also:

if(event_triger()[[1]] == "CF1" && event_triger()[[2]] == TRUE)

Type - event_triger instead of event_trigger

And:

You don't want to check if the button is TRUE - it will always be TRUE after the first use (because this is a counter counting from 0 and only 0 will be FALSE, so it will be FALSE only if never used in the session)

In case this is not obvious:

you use print() in observeEvent(), so Hello World will be printed in the console, not the app (for the app you would need renderText).

  • Related