javascript - 如何在 Shiny 的仪表板的标题中添加下载按钮

标签 javascript r shiny shinydashboard

我想在我的 shiny Dashboard 上有一个 downloadButton 来下载我在 www 文件夹中的 .pdf,它解释了如何使用仪表板。

到目前为止,我已经成功添加了一个 Home Button 和一个 GitHub 链接 (window.open),但是我找不到实现 downloadButton< 的方法.

到目前为止,这是我的代码:

标题.R:

header <- dashboardHeader(title = "Support Vector Machine - Credit Fraud",
                          titleWidth = 400,
                          tags$li(a(onclick = "onclick =window.open('https://github.com/xxx/xxx')",
                                    href = NULL,
                                    icon("github"),
                                    title = "GitHub",
                                    style = "cursor: pointer;"),
                                  class = "dropdown"),
                          tags$li(a(onclick = "openTab('foa')",
                                    href = NULL,
                                    icon("home"),
                                    title = "Homepage",
                                    style = "cursor: pointer;"),
                                  class = "dropdown",
                                  tags$script(HTML("
                                       var openTab = function(tabName){
                                       $('a', $('.sidebar')).each(function() {
                                       if(this.getAttribute('data-value') == tabName) {
                                       this.click()
                                       };
                                       });
                                       }")))


)

服务器.R :

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

  observeEvent(input$home, {
    updateTabItems(session, "sidebar", "foa")
  })
}

结果是:

code screenshot

我只想在 GitHub 图标旁边添加一个 downloadButton ,上面写着“How to use this Dashboard”,当你点击它时,它会下载一个我的 www 文件夹中的 .md 或 .pdf。

最佳答案

您可以通过为下载按钮添加另一个 tag 来完成此操作(假设您的 pdf 文件保存在 www 文件夹下名为 img 的子文件夹下).

 tags$li(a(id = "download", class = "fa fa-download",
           href = "/img/how_to.pdf", download = "how_to.pdf"), class = "dropdown")

因此附加到您现有的 header 代码:

header <- dashboardHeader(title = "Support Vector Machine - Credit Fraud",
                          titleWidth = 400,
                          tags$li(a(onclick = "onclick =window.open('https://github.com/xxx/xxx')",
                                    href = NULL,
                                    icon("github"),
                                    title = "GitHub",
                                    style = "cursor: pointer;"),
                                  class = "dropdown"),
                          tags$li(a(onclick = "openTab('foa')",
                                    href = NULL,
                                    icon("home"),
                                    title = "Homepage",
                                    style = "cursor: pointer;"),
                                  class = "dropdown",
                                  tags$script(HTML("
                                       var openTab = function(tabName){
                                       $('a', $('.sidebar')).each(function() {
                                       if(this.getAttribute('data-value') == tabName) {
                                       this.click()
                                       };
                                       });
                                       }"))),

                          ## DOWNLOAD
                          tags$li(a(id = "download", class = "fa fa-download",
                                    href = "/img/how_to.pdf", download = "how_to.pdf"),
                                    class = "dropdown")



)

enter image description here

关于javascript - 如何在 Shiny 的仪表板的标题中添加下载按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58050499/

相关文章:

javascript - 如何捕获或 Hook javascript 中对对象的任何调用

javascript - 在使用 HTML5 Canvas + 网络音频 API 的音频可视化工具方面需要帮助

java - 我应该在使用后重置 Java 堆空间最大值吗?

r - 两个选项卡中的传单输出 : LeafletProxy() doesn't render initially in 2nd tab

javascript - 图片加载异步等待

javascript - 使用 JavaScript 正则表达式删除除亚美尼亚语、ASCII 和俄语字母以及一些特殊字符之外的所有字符

r - 在 R 中的 fill.contour 图中绘制一个框?

r - 将图像添加到R中的表格状输出中

r - 作为条件面板条件的数字输出值

r - 通过编辑 table 和/或 eventReactive 更新 handsontable