【问题标题】:Where is the local host directory of a shiny app in R to display local pdf file?R中闪亮应用程序的本地主机目录在哪里显示本地pdf文件?
【发布时间】:2020-07-30 22:33:15
【问题描述】:

我正在查看这个问题:displaying a pdf from a local drive in shiny 并希望显示本地 pdf 文件。

我将我的 pdf 文件放在 C:\Users\user1\Documents\shiny_pdf\www 中。该应用程序位于C:\Users\user1\Documents\shiny_pdf,我将我的工作目录设置为后一个目录。

现在我不确定如何在应用程序中引用该文件。

链接问题中答案帖子的作者指出:

所以您必须将它们保存在您的 www 目录(本地 Web 服务器)中 并使用其 http(s): URL 访问文件(该 URL 将是 类似http://localhost/.../mypdf.pdf)

所以我不确定如何从http://localhost/ 导航到C:\Users\user1\Documents\shiny_pdf\www

我尝试了什么:

  • 我会假设我有 www 是服务器目录,所以我会使用 http://localhost/R-intro.pdf

  • 我在闪亮的应用程序中添加了一个图像,并在浏览器中检查了它的服务器地址。然后我相应地找到了pdf文件。我可以通过以下方式打开它:http://127.0.0.1:6023/r-intro.pdf(6023 是我的端口号)。但我不能用它在 iframe 中引用它。

  • 我也试过list.files(),但这会(obv.)给我工作目录中的文件。

  • http://localhost/R-intro.pdf 也不起作用。

错误:

Fehler: Verbindung fehlgeschlagen Firefox kann keine Verbindung zu dem Server under localhost aufbauen.

大致翻译为。连接失败。 Firefox 无法连接到 localhost 下的服务器。

可重现的代码:

  1. 将以下文件(见下文)另存为,例如app.R

  2. 运行以下代码为闪亮创建一个 WWW 目录并将示例 pdf 放入其中。

dir.create("www") pdf(file = "www/r-intro.pdf") plot(1) dev.off() list.files()

这里是要保存的代码,例如app.R.

代码:

library(shiny)

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

  observe({
    print(list.files("http://localhost/R-intro.pdf"))
  })

  output$pdfviewer <- renderText({
    return(paste('<iframe style="height:600px; width:100%" src="', input$pdfurl, '"></iframe>', sep = ""))
  })

})


row <- function(...) {
  tags$div(class="row", ...)
}

col <- function(width, ...) {
  tags$div(class=paste0("span", width), ...)
}

ui <- shinyUI(bootstrapPage(

  headerPanel("PDF VIEWER"),

  mainPanel(

    tags$div(
      class = "container",

      row(
        col(3, textInput("pdfurl", "PDF URL"))
      ),
      row(
        col(6, htmlOutput('pdfviewer')),
        col(6, tags$iframe(style="height:600px; width:100%", src="https://localhost/www/R-intro.pdf"))
      )
    )
  )
))


shinyApp(ui, server)

【问题讨论】:

    标签: r pdf shiny


    【解决方案1】:

    嗯,好吧,反对帖子中的建议

    col(6, tags$iframe(style="height:600px; width:100%", src="R-intro.pdf")),

    只是引用文件似乎有效。

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 2019-10-25
      • 1970-01-01
      • 1970-01-01
      • 2014-01-06
      • 2020-11-18
      • 2019-01-26
      • 2011-10-05
      • 1970-01-01
      相关资源
      最近更新 更多