R shiny - last clicked button id

后端 未结 2 1549
花落未央
花落未央 2021-01-15 05:41

I have multiple action buttons, on which i want to show different select Inputs and I want to know last clicked button id, how can I do that? When I use

whic         


        
2条回答
  •  清歌不尽
    2021-01-15 06:05

    You can do it by adding JS

    smthing like

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

    Example ( add class needed to btn if you want to control not all 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)
    

提交回复
热议问题