【问题标题】:Include link to local html file in DataTable in Shiny在 Shiny 的 DataTable 中包含指向本地 html 文件的链接
【发布时间】:2017-06-06 16:20:26
【问题描述】:

我想包含一个指向本地 html 文件的链接,该文件位于我的闪亮应用程序的 www 目录中,位于 data.table 的列中。单击时应打开一个新选项卡,显示 html 文件。 我找到了链接到互联网页面的解决方案,但是我该如何调整它,以便 Shiny 在浏览器中呈现时找到本地文件?

这是我的代码

library(DT)
library(shiny)

link <- "www/my_html.html"
link <- paste0("<a href='", link,"' target='_blank'>", link,"</a>")  # works fine for global url, but not for local file
df <- data.frame(a = 10.5, b = 48, link = link)

ui <- fluidPage(
  DT::dataTableOutput('table1')
)

server <- function(input, output) {
  output$table1 <- DT::renderDataTable({df}, escape = -3)
}

shinyApp(ui, server)

【问题讨论】:

    标签: r shiny dt


    【解决方案1】:

    也许您可以尝试使用闪亮的文件夹运行您的应用程序。确保您的 my_html.html 文件位于闪亮文件夹中的 www 文件夹中。

    ui.R

    library(DT)
    library(shiny)
    
    fluidPage(
      DT::dataTableOutput('table1')
    )
    

    服务器.R

    library(DT)
    library(shiny)
    
    df <- data.frame(a = 10.5, b = 48, link = "<a href='my_html.html' target='blank' >MyFile</a>")
    
    function(input, output) {
      output$table1 <- DT::renderDataTable({df}, escape = FALSE)
    }
    

    【讨论】:

    • 很好,它适用于您的代码。它也适用于 escape = 3 (我弄错了)。但是当我有一个名为“我的 html”的 html 文件时,代码会失败,中间有一个空格。我必须改变什么,这样才能正常工作?
    • 这个stackoverflow.com/questions/4172579/… 可能会帮助你:-)
    【解决方案2】:

    我认为您的代码的主要问题是您将 html 文件的地址指定为link &lt;- "www/my_html.html"。你应该放弃www/

    确实在您的应用程序目录中必须有一个 www 目录,并且您的 html 文件应该在这个 www 目录中。但是要正确处理您的文件,您应该将其视为您的工作目录已经在 www/ 目录中了。

    如果您将闪亮的应用程序放在单个 app.R 文件或 ui.R + server.R 的组合中,这两种方式都可以。

    另一个细节在renderDataTable() 函数的escape 参数中。它不应该等于 -3,而是使用:DT::renderDataTable({df}, escape = FALSE)

    所以最终的代码应该是这样的(假设你有两个 html 文件):

    library(shiny)
    
    link <- c("my_html_1.html", "my_html_2.html")
    link <- sprintf('<a href="%s" target="_blank">click_here</a>', link)
    
    df <- data.frame(name = c("1st_file", "2nd_file"), 
        value = c(10.5, 48), 
        link = link)
    
    ui <- fluidPage(
      dataTableOutput('table1')
    )
    
    server <- function(input, output) {
      output$table1 <- renderDataTable({df}, escape = FALSE)
    }
    
    shinyApp(ui, server)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-10
      • 2020-06-05
      • 1970-01-01
      • 1970-01-01
      • 2019-09-11
      • 1970-01-01
      • 2010-11-10
      • 1970-01-01
      相关资源
      最近更新 更多