显示来自本地驱动器的pdf [英] displaying a pdf from a local drive in shiny

查看:165
本文介绍了显示来自本地驱动器的pdf的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm still new to r and shiny, and i'm stumped with what should otherwise be simple logic. I am trying to display pdf files in imageOutput widgets but with no luck. Could someone steer me in the right direction?

示例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")
))

示例服务器.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)}) 

})

我得到的大部分错误都与预期的角色有关矢量参数,或原子矢量。我知道闪亮或多或少都是为渲染而设计的r AND显示图像或图表,但必须有一种方法来显示已经在本地驱动器上的pdf ..

Most of the errors i get have to do with expected character vector arguments, or atomic vectors. I know that shiny is more or less designed to render AND display images or plots but there has to be a way to display pdf's that are already on a local drive..

推荐答案

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

To embed a PDF viewer (the default PDF viewer of your web browser, pdf.js on mozilla for example) in your Shiny ui, you can use an iframe which the src will be the path to your PDF.

以下是2种不同的方式在您的界面中包含iframe:

Here is 2 differents ways to include an iframe in your interface :

在Ui中您可以直接添加 iframe 标签的绝对src属性如下:

in the Ui you can directly add an iframe tag with an absolute src attribute as bellow :

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

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

Or get an URL from the ui in the server , write the iframe tag with the input URL and return the HTML code in a htmlOutput in the ui :

Ui:

textInput(pdfurl,PDF URL)

htmlOutput( 'pdfviewer')

服务器:

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

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

Note that when pages are loaded with a HTTP(S) protocol (the case of the Shiny app) for security reasons you can't framed locals files with their "file:" URLs. If you want to display locals pdf you should access to them with a http(s): URL, so you have to save them in your www directory (a local web server) and access to files with their http(s): URLs (the URL will be something like http://localhost/.../mypdf.pdf) as in the second iframe of my example. (Then you can't use a fileInput directly, you have to format it)

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"))
      )
    )
  )
))

Server.R:

shinyServer(function(input, output, session) {

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

})

带有PDF查看器的网页:

The web pages with the PDF viewers :

希望此帮助。

这篇关于显示来自本地驱动器的pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆