首页 > 解决方案 > 为什么我的 Shiny iframe 在尝试显示本地 PDF 时告诉我“本地主机没有发送任何数据”?

问题描述

意图:

我正在尝试构建一个包含 pdf 查看器的 Shiny 应用程序。我正在尽力复制这个例子:

以闪亮的方式显示来自本地驱动器的 pdf

问题:

在下面的代表中,我将一个虚拟 pdf 下载到与“App.R”相同的根目录中的“www”目录中。

据我所知,Shiny 应用程序固有的 http 协议要求我将 iframe 的源设置为"http://localhost/test.pdf",而不是"www/test.pdf". 但是当我运行这段代码时,我得到一个 iframe,除了这个之外什么都没有:

我还遵循了前面提到的 SO 线程中的其他建议 - 使用 RStudio 中的“运行应用程序”按钮运行应用程序,而不是通过代码按 Ctrl+return-ing。没有成功。任何人都知道我怎样才能使这项工作?

代表:

require(shiny)
#> Loading required package: shiny

ui = fluidPage(
  htmlOutput("pdfViewer")
)

server = function(input, output, session) {
  ### Libraries
  require(pdftools)
  
  ### Create 'www' dir (understand that this
  ### is where 'localhost' stuff is kept?)
  suppressWarnings(dir.create("www"))
  
  ### Make a test pdf file using the w3 dummy pdf
  pdf = "www/test.pdf"
  download.file(url = "https://www.w3.org/WAI/ER/tests/xhtml/testfiles/resources/pdf/dummy.pdf",
                       destfile = pdf)
  
  ### Define the http: address for the pdf
  pdfHttp = "http://localhost/test.pdf"
  
  output$pdfViewer = 
    renderText({
      ### Copied this from here: "https://stackoverflow.com/a/21024943/11149547"
      return(paste('<iframe style="height:600px; width:100%" src="', pdfHttp, '"></iframe>', sep = ""))
    })
}

shinyApp(ui = ui, server = server)

附录(R版本数据)

> R.version
               _                           
platform       x86_64-w64-mingw32          
arch           x86_64                      
os             mingw32                     
system         x86_64, mingw32             
status                                     
major          3                           
minor          6.1                         
year           2019                        
month          07                          
day            05                          
svn rev        76782                       
language       R                           
version.string R version 3.6.1 (2019-07-05)
nickname       Action of the Toes

标签: rpdfiframeshiny

解决方案


我需要向本地主机地址添加一个端口。

我知道这http://localhost是 的同义词http://127.0.0.1。然后我注意到,当我的 Shiny App 在浏览器中运行时,多功能框将地址列为:127.0.0.1:5056.

我用 替换http://localhost/test.pdf了上面的代码http://127.0.0.1:6056/test.pdf,现在加载了 pdf。

我想这与我正在使用我的工作计算机这一事实有关,这是某种本地网络的一部分。

编辑:

options(shiny.port = 6056)此外,您可以在运行之前明确定义 my running 的端口runApp(server = server, ui = ui)。最后我需要这样做,因为我的另一个 Shiny App(不是 reprex)启动到了不同的端口。


推荐阅读