R shiny - last clicked button id

后端 未结 2 1538
花落未央
花落未央 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:13

    This code track which button was last clicked:

       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)
    

    Version with lapply with many buttons. Credit goes to @Victorp and this answer.

    This is the code:

        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)
    

提交回复
热议问题