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

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

问题描述

我还是 r 和闪亮的新手,我对否则应该是简单的逻辑感到困惑.我试图在 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)}) 

})

我遇到的大多数错误都与预期的字符向量参数或原子向量有关.我知道闪亮或多或少旨在渲染和显示图像或绘图,但必须有一种方法来显示本地驱动器上已经存在的 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.

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

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

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

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 :

用户界面:
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天全站免登陆