Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Updating an input and triggering an action button inside an observer

I have an application which utilizes an actionButton to apply a filter selection to a plot. The application also contains a reset actionButton which resets the drop-down selector to its original value, in this instance mpg.

I would like to know whether it is possible to have the reset button not only update the selector itself, but then trigger the apply button so that the plot is reverts back to showing mpg as the y-axis value as it did at initialization.

Please note that the application must utilize the reactiveValues construct shown below as that is present in the actual business use case.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

library(shiny)
library(plotly)

ui <- fluidPage(
  ## input and output ui elements and apply/reset buttons
  selectInput("var", "Select Y-Axis Variable", c("mpg", "hp", "wt", "am")),
  actionButton("apply", "Apply"),
  actionButton("reset", "Reset"),
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  ## stored default values
  plot_vals <- reactiveValues(y = "mpg")
  observeEvent(input$apply, {
    plot_vals$y <- input$var
  })
  ## render plot
  output$plot <- renderPlotly(
    mtcars %>% 
      plot_ly(x = ~disp,
              y = ~get(plot_vals$y),
              type = "scatter",
              mode = "markers")
  )
  ## update selectors (how can I have this segment not only update the drop down, but also trigger the apply button?)
  observeEvent(input$reset, {
    updateSelectInput(session = session, "var", selected = "mpg")
  })
}

shinyApp(ui, server)

>Solution :

Just update the reactiveVal on reset:

library(shiny)
library(plotly)

ui <- fluidPage(
  ## input and output ui elements and apply/reset buttons
  selectInput("var", "Select Y-Axis Variable", c("mpg", "hp", "wt", "am")),
  actionButton("apply", "Apply"),
  actionButton("reset", "Reset"),
  plotlyOutput("plot")
)

server <- function(input, output, session) {
  ## stored default values
  plot_vals <- reactiveValues(y = "mpg")
  observeEvent(input$apply, {
    plot_vals$y <- input$var
  })
  ## render plot
  output$plot <- renderPlotly({
    mtcars %>% 
      plot_ly(x = ~disp,
              y = ~get(plot_vals$y),
              type = "scatter",
              mode = "markers")
  })
  ## update selectors (how can I have this segment not only update the drop down, but also trigger the apply button?)
  observeEvent(input$reset, {
    updateSelectInput(session = session, "var", selected = "mpg")
    plot_vals$y <- "mpg"
  })
}

shinyApp(ui, server)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading