gpt4 book ai didi

重置 R Shiny 的 actionButton 以多次使用它

转载 作者:行者123 更新时间:2023-12-04 01:35:00 26 4
gpt4 key购买 nike

有人知道如何将 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)

先感谢您

最佳答案

通常在 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)

关于重置 R Shiny 的 actionButton 以多次使用它,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253655/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com