2016-10-21 132 views
2

我有多個動作按鈕,我想要顯示不同的選擇輸入,我想知道最後點擊的按鈕ID,我該怎麼做?當我使用R閃亮 - 最後點擊按鈕ID

which(lapply(c(1:10), function(i) { input[[paste0("ActionButton", i)]]}) == TRUE) 

它顯示我被點擊了哪個按鈕全部,但是我想知道哪一個是最後爲了使再次點擊上一個按鈕。我怎樣才能做到這一點?我是新的閃亮,不知道是否瞭解所有反應/隔離問題,所以我會爲任何提示greateful。

回答

3

您可以通過添加JS

smthing像

$(document).on('click', '.needed', function() { 
           Shiny.onInputChange('last_btn',this.id); 
          }); 

例(添加needed類BTN如果你想控制不這樣做全部btn)

ui <- shinyUI(fluidPage(

    titlePanel("Track last clicked Action button"), 
    tags$head(tags$script(HTML("$(document).on('click', '.needed', function() { 
           Shiny.onInputChange('last_btn',this.id); 
          });"))), 

    sidebarLayout(
    sidebarPanel(
     actionButton("first", "First",class="needed"), 
     actionButton("second", "Second",class="needed"), 
     actionButton("third", "Third",class="needed"), 
     actionButton("save", "save"), 
     selectInput("which_","which_",c("first","second","third")) 
    ), 

    mainPanel(

     textOutput("lastButtonCliked") 
    ) 
) 
)) 


server <- shinyServer(function(input, output,session) { 
    observeEvent(input$save,{ 
    updateSelectInput(session,"which_",selected = input$last_btn) 
    }) 
    output$lastButtonCliked=renderText({input$last_btn}) 

}) 
# Run the application 
shinyApp(ui = ui, server = server) 
+0

非常感謝。你能否解釋一下,如何在selectInput按鈕中保存當前選定的值?假設我有selectInput和actionButton'Save'。對於每個'保存'點擊我想要保存當前選擇的值selectInput作爲updateSelectInput,你可以添加嗎?如果你想,我可以添加新的問題。 – user3463225

+0

看到更新它你想要什麼? – Batanichek

+0

不是,我的意思是,如果selectInput我有選擇= c(1:5),選擇= 1,如果我將應用程序選定值更改爲4並單擊'保存',選擇輸入按鈕將更新其選定值爲4 'Save'按鈕之前的最後選擇的值) – user3463225

2

此代碼跟蹤哪個按鈕最後點擊:

library(shiny) 


    ui <- shinyUI(fluidPage(


     titlePanel("Track last clicked Action button"), 


     sidebarLayout(
      sidebarPanel(
      actionButton("first", "First"), 
      actionButton("second", "Second"), 
      actionButton("third", "Third") 
     ), 

      # Show a plot of the generated distribution 
      mainPanel(
      textOutput("lastButtonCliked") 
     ) 
     ) 
    )) 


    server <- shinyServer(function(input, output) { 

      rv <- reactiveValues(lastBtn = character()) 
      observeEvent(input$first, { 
        if (input$first > 0) { 
          rv$lastBtn = "first" 
        } 
      }) 
      observeEvent(input$second, { 
        if (input$second > 0) { 
          rv$lastBtn = "second" 
        } 
      }) 
      observeEvent(input$third, { 
        if (input$third > 0) { 
          rv$lastBtn = "third" 
        } 
      }) 
      output$lastButtonCliked <- renderText({ 
        paste("Last button clicked: ", rv$lastBtn) 
      }) 
    }) 
    # Run the application 
    shinyApp(ui = ui, server = server) 

版本與lapply有許多按鈕。信用歸於@Victorp和this answer

這是代碼:

library("shiny") 
    ui <- fluidPage(
      fluidRow(
        column(
          width = 6, 
          lapply(
            X = 1:5, 
            FUN = function(i) { 
              actionButton(inputId = paste0("button", i), label = paste("Button ", i)) 
            } 
          ) 
        ), 
        column(
          width = 6, 
          textOutput("lastButtonCliked") 
        ) 
      ) 
    ) 
    server <- function(input, output){ 

      rv <- reactiveValues(lastBtn = character()) 

      lapply(
        X = 1:6, 
        FUN = function(i){ 
          observeEvent(input[[paste0("button", i)]], { 
            if (input[[paste0("button", i)]] > 0) { 
              rv$lastBtn = paste0("button", i)  
            } 
          }) 
        } 
      ) 

      output$lastButtonCliked <- renderText({ 
        paste("Last button clicked: ", rv$lastBtn) 
      }) 
    } 
    shinyApp(ui = ui, server = server) 
+1

如果我有100個actionButton,怎麼寫這麼多的observeEvents呢?假設你的按鈕以'button'+ X開頭,其中X從1到100 – user3463225