2015-03-31 14 views
1

一旦它被點擊就可以捕獲actionButton的標籤嗎?捕獲一個actionButton被點擊後的標籤

想象一下,我的ui.R上有3個按鈕,取決於我點擊了哪一個按鈕,我想在server.R上執行不同的操作。

一個需要注意的是,按鈕動態創建與動態標籤server.R

感謝

回答

1

1)(從而掌握上單擊該標籤的必要性)被點擊了什麼按鈕,在去年用戶?

要回答這個問題,你可以使用observeEvent函數,並通過使用reactiveValues函數設置一個變量。請確保您更新您的庫並在最新版本的R (version 3.1.3)中工作,因爲shiny依賴於此版本。 Windows系統,你可以按照例如在如何更新here

rm(list = ls()) 
library(shiny) 

ui =fluidPage(
    sidebarPanel(
    textInput("sample1", "Name1", value = "A"), 
    textInput("sample2", "Name2", value = "B"), 
    textInput("sample3", "Name3", value = "C"), 
    div(style="display:inline-block",uiOutput("my_button1")), 
    div(style="display:inline-block",uiOutput("my_button2")), 
    div(style="display:inline-block",uiOutput("my_button3"))), 
    mainPanel(textOutput("text1")) 
) 

server = function(input, output, session){ 

    output$my_button1 <- renderUI({actionButton("action1", label = input$sample1)}) 
    output$my_button2 <- renderUI({actionButton("action2", label = input$sample2)}) 
    output$my_button3 <- renderUI({actionButton("action3", label = input$sample3)}) 

    my_clicks <- reactiveValues(data = NULL) 

    observeEvent(input$action1, { 
    my_clicks$data <- input$sample1 
    }) 

    observeEvent(input$action2, { 
    my_clicks$data <- input$sample2 
    }) 

    observeEvent(input$action3, { 
    my_clicks$data <- input$sample3 
    }) 

    output$text1 <- renderText({ 
    if (is.null(my_clicks$data)) return() 
    my_clicks$data 
    }) 
} 
runApp(list(ui = ui, server = server)) 

2)保存用於進一步操縱的點擊低於

下面是Shiny UI: Save the Changes in the Inputs和基於的jdharrison工作的小例子shinyStorage包。

rm(list = ls()) 
#devtools::install_github("johndharrison/shinyStorage") 
library(shinyStorage) 
library(shiny) 

my_clicks <- NULL 

ui =fluidPage(
    # 
    addSS(), 
    sidebarPanel(
    textInput("sample_text", "test", value = "0"), 
    uiOutput("my_button")), 
    mainPanel(uiOutput("text1")) 
) 

server = function(input, output, session){ 
    ss <- shinyStore(session = session) 

    output$my_button <- renderUI({ 
    actionButton("action", label = input$sample_text) 
    }) 

    observe({ 
    if(!is.null(input$sample_text)){ 
     if(input$sample_text != ""){ 
     ss$set("myVar", input$sample_text) 
     } 
    } 
    }) 

    output$text1 <- renderUI({ 
    input$action 
    myVar <- ss$get("myVar") 
    if(is.null(myVar)){ 
     textInput("text1", "You button Name") 
    }else{ 
     textInput("text1", "You button Name", myVar)   
    } 
    }) 
} 
runApp(list(ui = ui, server = server)) 
+0

謝謝pops,但那不是我要找的。 我想根據按鈕的標籤在組件上設置文本。我用你在這裏發佈的代碼(http://stackoverflow.com/questions/27326929/how-to-update-button-labels-in-r-shiny/27458250#comment46948109_27458250)來動態創建帶有動態標籤的按鈕(讓我們說A,B和C),現在我需要知道用戶點擊了哪個按鈕。 謝謝 – Diego 2015-04-01 10:10:39

+0

嗨@Diego,你有沒有想過如何做到這一點?我遇到類似的問題......謝謝。 – Carlos 2016-12-02 11:27:03