如何更新R Shiny中的按钮标签?

如何更新R Shiny中的按钮标签?

问题描述:

R Shiny网站有一个great example关于如何根据用户输入更新各种输入类型的标签和值。但是,我找不到任何按钮。具体来说,如何根据用户输入更新按钮的标签?如何更新R Shiny中的按钮标签?

+0

'updateActionButton'? – 2017-08-29 07:54:24

您可以动态创建像这样的按钮,同时更新标签:

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)) 
+0

这是伟大的,感谢分享 – Diego 2015-03-31 17:29:40

+0

是否有可能捕捉按钮的标签,一旦我点击它? – Diego 2015-03-31 18:29:51

+0

示例在这里提供:http://*.com/questions/29376031/capture-the-label-of-an-actionbutton-once-it-is-clicked/29384955#29384955 – 2015-04-01 07:15:24

如果你的按钮已经建成,解决方案是使用图书馆“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)