重置 R shiny actionButton 以多次使用它
reset R shiny actionButton to use it more than once
有人知道如何将 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)
有人知道如何将 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)