我知道类似的question https://stackoverflow.com/questions/27326929/how-to-update-button-labels-in-r-shiny已经回答了,但是该解决方案在字符串输入时创建一个具有不同标签的新操作按钮。我需要的是保留按钮(按钮的计数器),因为当我更改标签并创建新按钮时,它的计数器为 0(未单击)。
所以基本上我需要像更新函数这样的东西,以便在按下操作按钮时更改其标签。你按一次,标签就会改变。
input$Button <- renderUI({
if(input$Button >= 1) label <- "new label"
else label <- "old label"
actionButton("Button", label = label)
})
像这样的东西,但没有重置按钮的值(通过创建一个全新的按钮)。
Thanks!
reactiveValues()
可以帮助。查看http://shiny.rstudio.com/articles/reactivity-overview.html http://shiny.rstudio.com/articles/reactivity-overview.html了解详情。
在下面的示例中,我将您的重命名为input$Button
to input$click
以避免重复使用“按钮”名称。
由于我们将标签包裹在renderUI()
, input$click
创建后最初会触发?!?,这就是我贴标签的原因
条件为:if(vars$counter >= 2)
-
另一种解决方案可能是删除只读属性(在此处找到:https://github.com/rstudio/shiny/issues/167 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)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)