5

有人知道如何将 actionButton (R shiny) 重置为初始值以便多次使用它吗?

请在下面找到一个可复制的示例

在此示例中,我想通过选择相应的按钮来更改图表颜色:我的问题是它无法在一次迭代后重新加载图表。

library(shiny)

ui <- fluidPage(

  actionButton(inputId = "button1",label = "Select red"),
  actionButton(inputId = "button2",label = "Select blue"),
  plotOutput("distPlot")

)


server <- function(input, output) {

   output$distPlot <- renderPlot({
      x    <- faithful[, 2] 
      bins <- seq(min(x), max(x))

      my_color <- "green"
      if (input$button1){my_color <- "red"}
      if (input$button2){my_color <- "blue"}

      hist(x, breaks = bins, col = my_color)
   })
}

shinyApp(ui = ui, server = server)

先感谢您

4

1 回答 1

6

通常在 Shiny 中重置 ActionButton 并不是一个好主意。我建议您使用 ObserveEvent 并将颜色存储到 reactiveValues。

library(shiny)

ui <- fluidPage(
  actionButton(inputId = "button1", label = "Select red"),
  actionButton(inputId = "button2", label = "Select blue"),
  plotOutput("distPlot")
)


server <- function(input, output) {
  r <- reactiveValues(my_color = "green")

  output$distPlot <- renderPlot({
    x <- faithful[, 2]
    bins <- seq(min(x), max(x))
    hist(x, breaks = bins, col = r$my_color)
  })

  observeEvent(input$button1, {
    r$my_color <- "red"
  })

  observeEvent(input$button2, {
    r$my_color <- "blue"
  })
}

shinyApp(ui = ui, server = server)
于 2018-05-09T12:55:24.523 回答