R中用于显示本地pdf文件的闪亮应用程序的本地主机目录在哪里



我在看这个问题:在shine中显示本地驱动器的pdf文件,并希望显示本地pdf文件。

我把pdf放在C:Usersuser1Documentsshiny_pdfwww中。应用程序被放置在C:Usersuser1Documentsshiny_pdf中,我将我的工作目录设置为后一个目录。

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

链接问题中答案帖子的作者表示:

因此您必须将它们保存在您的www目录(本地web服务器(中以及使用其http:URL访问文件(URL将为类似于http://localhost/.../mypdf.pdf)

所以我不确定如何从http://localhost/导航到C:Usersuser1Documentsshiny_pdfwww

我尝试了什么:

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

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

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

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

错误:

Fehler:Verbindung fehlgeschlagenFirefox可以在本地主机上运行Verbindung zu dem Server。

松散地翻译为。连接失败。Firefox无法在localhost下连接到服务器。

可复制代码:

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

  2. 运行以下代码为shine创建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)

好吧,反对后的建议

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

仅仅引用文件似乎是可行的。

最新更新