我知道类似question已经回答了,但是该解决方案在输入字符串时创建了一个具有不同标签的新 actionButton。我需要保留按钮(按钮的计数器),因为当我更改标签并创建一个新按钮时,它的计数器为 0(未单击)。
所以基本上我需要一个类似更新函数的东西来改变 actionButton 的标签,当它被按下时。你按下它一次,标签就会改变。
input$Button <- renderUI({
if(input$Button >= 1) label <- "new label"
else label <- "old label"
actionButton("Button", label = label)
})
类似这样,但没有重置按钮的值(通过创建一个全新的按钮)。
谢谢!
最佳答案
reactiveValues()
可以提供帮助。检查http://shiny.rstudio.com/articles/reactivity-overview.html了解详情。 在下面的示例中,我将您的input$Button
重命名为input$click
以避免重复使用“Button”名称。 由于我们将标签包装在renderUI()
中,input$click
最初会在创建后触发?!?,这就是我放置标签的原因 条件为:if(vars$counter >= 2)
另一种解决方案是删除只读属性(可在此处找到:https://github.com/rstudio/shiny/issues/167)
attr(input, "readonly") <- FALSE input$click <- 1
举个例子 将以下内容粘贴到您的 R 控制台中:
ui <- bootstrapPage( uiOutput('Button') ) server <- function(input, output) { # store the counter outside your input/button vars = reactiveValues(counter = 0) output$Button <- renderUI({ actionButton("click", label = label()) }) # increase the counter observe({ if(!is.null(input$click)){ input$click isolate({ vars$counter <- vars$counter + 1 }) } }) label <- reactive({ if(!is.null(input$click)){ if(vars$counter >= 2) label <- "new label" else label <- "old label" } }) } # run the app shinyApp(ui = ui, server = server)
关于r - 更新 shiny 中 actionButton 的标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33777991/