Reputation: 1611
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.
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)
Upvotes: 2
Views: 168
Reputation: 33580
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)
Upvotes: 2