更新输入并触发观察者内的操作按钮
Updating an input and triggering an action button inside an observer
我有一个应用程序,它利用 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)
我有一个应用程序,它利用 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)