r-更新输入并触发观察者内部的操作按钮



我有一个应用程序,它利用actionButton将过滤器选择应用于绘图。该应用程序还包含一个重置actionButton,它将下拉选择器重置为其原始值,在本例中为mpg

我想知道是否可以让重置按钮不仅更新选择器本身,而且触发应用按钮,使绘图恢复为显示mpg作为y轴值,就像初始化时一样。

请注意,应用程序必须使用下面显示的reactiveValues构造,因为它存在于实际的业务用例中。

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)

只需在重置时更新reactiveVal

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)

最新更新