2014-12-06 152 views

回答

5

您可以動態創建像這樣的按鈕,同時更新標籤:

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://stackoverflow.com/questions/29376031/capture-the-label-of-an-actionbutton-once-it-is-clicked/29384955#29384955 – 2015-04-01 07:15:24

2

如果你的按鈕已經建成,解決方案是使用圖書館「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,"')")) 
0

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)