首页 > 解决方案 > R Shiny - 在新选项卡中打开可下载文件

问题描述

我正在使用 R Shiny 的 downloadHandler/downloadLink 函数来制作一个可供下载的 html 文件。根据默认行为,文件由浏览器下载到系统。但是,由于我要下载的文件是 HTML 文件 (.html),有没有办法直接在新的浏览器选项卡中打开文件,而不是将文件下载到系统?

我在 StackOverflow 上发现了一个类似的问题,建议添加target = "_blank"。但是,即使这对我也不起作用。

我的 R 代码:

# UI Code:
ui <- fluidPage(
        includeCSS("custom.css"),
        div(class = "intro-divider"),
        tags$style(".logo {
                 margin-left: 10px;
                 }"),
                 tags$div(class="logo", img(src="logo.gif", height='50px',width='350px')),
        br(),
        div(class = "intro-divider"),
        br(),
        h3("Title Page"),
        wellPanel(
          tags$head(tags$style(type = "text/css", '.well{width: 600px}')),
          h4("July 20, 2018"),
          tags$div(
            tags$ul(
              tags$li(downloadLink("july20_1", "Download HTML File", target = "_blank"))
            )
          )
        ) 
    )

# Server Code
server = function(input, output) {

      output$july20_1 <- downloadHandler(
                                filename = "july20_analysis_html.html",
                                content = function(file) {
                                file.copy("july20_analysis_html.html", file)
                                        })         
    }


# Run the application 
shinyApp(ui = ui, server = server)

如何使文件直接在新选项卡中打开?

PS:我在谷歌 Chrome 浏览器上测试了这个应用程序。

标签: rshinyr-markdownshinydashboard

解决方案


推荐阅读