如何更新R Shiny中的按钮标签?
问题描述:
R Shiny网站有一个great example关于如何根据用户输入更新各种输入类型的标签和值。但是,我找不到任何按钮。具体来说,如何根据用户输入更新按钮的标签?如何更新R Shiny中的按钮标签?
答
您可以动态创建像这样的按钮,同时更新标签:
library(shiny)
ui =(pageWithSidebar(
headerPanel("Test Shiny App"),
sidebarPanel(
textInput("sample_text", "test", value = "0"),
#display dynamic UI
uiOutput("my_button")),
mainPanel()
))
server = function(input, output, session){
#make dynamic button
output$my_button <- renderUI({
actionButton("action", label = input$sample_text)
})
}
runApp(list(ui = ui, server = server))
答
如果你的按钮已经建成,解决方案是使用图书馆“shinyjs”。
library(shinyjs)
# in UI section
shinyjs::useShinyjs()
# in Server section: call this function at any time to update the button label
runjs(paste0("$('label[for=\"YourButtonId\"]').text('",TextVariable,"')"))
答
updateActionButton
:
ui <- fluidPage(
actionButton('someButton', ""),
textInput("newLabel", "new Button Label:", value = "some label")
)
server <- function(input, output, session) {
observeEvent(input$newLabel, {
updateActionButton(session, "someButton", label = input$newLabel)
})
}
shinyApp(ui, server)
'updateActionButton'? – 2017-08-29 07:54:24