R - count Shiny download button clicks

前端 未结 1 891
长情又很酷
长情又很酷 2020-12-15 14:45

Is there a built in Shiny attribute that counts the number of times a downloadButton is clicked? I\'m not finding it in the function help or web searches. If there\'s not

1条回答
  •  时光取名叫无心
    2020-12-15 15:10

    I think there is no build-in method. But you could build it yourself.

    You can do this by adding a click listener to the button with javascript:

      observe({
        if(is.null(input$rnd)){
          runjs("
                var click = 0;
                Shiny.onInputChange('rnd', click)
                var dwnldBtn = document.getElementById('dwnldBtn')
                dwnldBtn.onclick = function() {click += 1; Shiny.onInputChange('rnd', click)};
                ")      
        }
      })
    

    The output from Shiny.onInputChange('rnd', click) will be accessible in Shiny via input$rnd.

    Edit: For multiple buttons you can use:

      observe({
        for(btn1 in 1:2){
          if(is.null(input[[paste0("rnd", btn1)]])){
            runjs(
              paste0("
                  var counter", btn1 ,"= 0;
                  var dwnldBtn = document.getElementById('", paste0("dwnldBtn", btn1), "')
                  dwnldBtn.onclick = function() {counter", btn1, " +=1; Shiny.onInputChange('", paste0("rnd", btn1), "', counter", btn1,")};
                  ")
            )      
          }
        }
      })
    

    For a working example see below:

       library(shiny)
    library(shinyjs)
    data <- matrix(1:20, nrow=5)
    
    ui <-  fluidPage(title = 'Count Button Clicks',
                     useShinyjs(),
                     fluidRow(style = "padding-bottom: 20px;",
                              column(width=6,
                                     textOutput("actionclickCount"),
                                     br(),
                                     textOutput("downloadclickCount")
                              ),
                              column(width=6,
                                     actionButton("actionBtn", "Action Button"),
                                     br(),
                                     downloadButton("dwnldBtn", "Download Button")
                              )
                     )
    )
    
    server <- function(input, output, session) {
      output$actionclickCount <- renderText({
        paste('Action Button Clicks =',input$actionBtn)
      })
    
      output$downloadclickCount <- renderText({
        paste('Download Button Clicks =', input$rnd)
      })
    
      output$dwnldBtn <- downloadHandler(
        filename = 'data.csv',
        content = function(file){
          write.csv(data, file)
        },
        contentType = 'csv'
      )
    
      observe({
        if(is.null(input$rnd)){
          runjs("
                var click = 0;
                Shiny.onInputChange('rnd', click)
                var dwnldBtn = document.getElementById('dwnldBtn')
                dwnldBtn.onclick = function() {click += 1; Shiny.onInputChange('rnd', click)};
                ")      
        }
        })
    
      }
    
    runApp(shinyApp(ui = ui, server = server), launch.browser = TRUE)
    

    0 讨论(0)
提交回复
热议问题