显示来自本地驱动器的pdf

Jer*_*amy 16 pdf r image displayobject shiny

我仍然是新手,并且有光泽,而且我很难接受原本应该是简单的逻辑.我试图在imageOutput小部件中显示pdf文件,但没有运气.有人能引导我朝正确的方向发展吗?

样本ui.R

shinyUI(pageWithSidebar(
mainPanel(
  selectInput("sel_ed",
              label = "View outputs for Ecodistrict:", 
              choices = c(244,245,247,249), 
              selected = NULL,
              multiple = FALSE),

  imageOutput("imp_pdf",width="500px",height="500px")
))
Run Code Online (Sandbox Code Playgroud)

样本服务器.R

shinyServer(function(input, output, session) {

importance <- function(inputSpecies){
img_dir <- pdf(paste(inputSpecies,"\\models\\MATL\\MATRF_Importance",sep=""))
}

output$imp_pdf <- renderImage({importance(input$sel_ed)}) 

})
Run Code Online (Sandbox Code Playgroud)

我得到的大多数错误都与预期的字符向量参数或原子向量有关.我知道闪亮或多或少的设计用于渲染和显示图像或绘图,但必须有一种方法来显示已经在本地驱动器上的pdf ..

Jul*_*rre 23

要在您的Shiny ui中嵌入PDF查看器(Web浏览器的默认PDF查看器,例如mozilla上的pdf.js),您可以使用iframe,其中src将成为PDF的路径.

以下是在界面中包含iframe的两种不同方法:

在Ui中,您可以直接添加iframe带有绝对src属性的标记,如下所示:

tags$iframe(style="height:600px; width:100%", src="http://localhost/ressources/pdf/R-Intro.pdf"))
Run Code Online (Sandbox Code Playgroud)

或者从服务器中的ui获取URL,iframe使用输入URL 编写标记并在ui中的htmlOutput中返回HTML代码:

Ui:
textInput("pdfurl", "PDF URL")
htmlOutput('pdfviewer')

服务器:

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

请注意,出于安全原因,当页面加载了HTTP(S)协议(Shiny应用程序的情况)时,您无法使用其"file:"URL框架本地文件.如果要显示本地化pdf,您应该使用http(s):URL 访问它们,因此您必须将它们保存在您的www目录(本地Web服务器)中,并使用其http(s):URL(URL将类似于http://localhost/.../mypdf.pdf)访问文件,如同我的例子中的第二个iframe.(那么你不能直接使用fileInput,你必须格式化它)

Ui.R:

library(shiny)

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

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

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="http://localhost/ressources/pdf/R-Intro.pdf"))
      )
    )
  )
))
Run Code Online (Sandbox Code Playgroud)

Server.R:

shinyServer(function(input, output, session) {

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

})
Run Code Online (Sandbox Code Playgroud)

带有PDF查看器的网页:

在此输入图像描述

希望这有帮助.

  • 仅供参考:根据我使用此方法的经验,您需要在浏览器中打开Shiny应用程序以实际查看PDF.它不会显示在RStudio窗口中. (3认同)

Kri*_*ris 7

在包含 server.R 和 ui.R 脚本的原始目录中创建一个名为 www 的文件夹。将 PDF 放在 www/ 文件夹中,然后使用类似以下代码的内容:

在 server.R 中:

shinyServer(function(input, output) {

  observeEvent(input$generate, {
    output$pdfview <- renderUI({
      tags$iframe(style="height:600px; width:100%", src="foo.pdf")
    })
  })
})
Run Code Online (Sandbox Code Playgroud)

在 ui.R 中:

shinyUI(fluidPage(

  titlePanel("Display a PDF"),

  sidebarLayout(
    sidebarPanel(
      actionButton("generate", "Generate PDF")
    ),

    mainPanel(
      uiOutput("pdfview")
    )
  )
))
Run Code Online (Sandbox Code Playgroud)