R - 计数闪亮的下载按钮点击次数 [英] R - count Shiny download button clicks

查看:20
本文介绍了R - 计数闪亮的下载按钮点击次数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一个内置的 Shiny 属性来计算下载按钮被点击的次数?我没有在功能帮助或网络搜索中找到它.如果没有内置方法,我将如何计算点击次数.这是一个工作示例:

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 a built in method how would I go about counting the clicks. Here's a working example:

data <- matrix(1:20, nrow=5)
ui <-  fluidPage(title = 'Count Button Clicks',
              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 =','what variable goes here?')
    })

    output$dwnldBtn <- downloadHandler(
      filename = 'data.csv',
      content = function(file){
        write.csv(data, file)
      },
      contentType = 'csv'
    )
  }

shinyApp(ui = ui, server = server)

推荐答案

我认为没有内置方法.但是你可以自己构建它.

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

您可以通过使用 javascript 向按钮添加点击侦听器来实现:

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)};
            ")      
    }
  })

Shiny.onInputChange('rnd', click) 的输出可以通过 input$rnd 在 Shiny 中访问.

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

对于您可以使用的多个按钮:

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)

这篇关于R - 计数闪亮的下载按钮点击次数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆