【发布时间】: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 下的服务器。
可重现的代码:
将以下文件(见下文)另存为,例如
app.R。运行以下代码为闪亮创建一个 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)
【问题讨论】: