【问题标题】:How to display dynamically generated PDF files inside shiny golem app如何在闪亮的 golem 应用程序中显示动态生成的 PDF 文件
【发布时间】:2021-04-29 20:30:09
【问题描述】:

我正在使用officer 在 R 中生成一个 powerpoint 演示文稿,然后将其转换为 PDF,以便用户可以在闪亮的 golem 应用程序中预览幻灯片。但是在引用this threadthis SO answer to a similar question 之后,我仍然不太确定如何实现这一点。我能够在 iframe 内显示位于 inst/app/www 的外部 PDF,但不确定如何为应用程序本身生成的 PDF 执行此操作。

这是我的 app_server.R 文件中的相关代码 sn-p:

  output$preview <- renderUI({

  # Creates rpptx object with one slide
  preview_rpptx <- add_title_slide(x = NULL, title = input_list[["title"]]) 

  # Creates www/ directory if it doesn't exist
  if (!dir_exists("www")) dir_create("www")

  # Generates .pptx file 
  pptx_file_path <- file_temp("preview", tmp_dir = "www", ext = ".pptx")
  print(preview_rpptx, pptx_file_path)
  
  # Converts .pptx to .pdf 
  pdf_file_path <- file_temp("preview", tmp_dir = "www", ext = ".pdf")
  convert_to_pdf(path = pptx_file_path, pdf_file = pdf_file_path)
  
  tags$iframe(style = "height:600px; width:100%", src = str_sub(pdf_file_path, 5))
}

运行应用程序时,我在 iframe 中收到“未找到”错误。但是我可以看到在 www/ 目录中正确生成了 PDF 文件。

【问题讨论】:

  • 如果您想以比 iframe 方式更漂亮的方式嵌入 pdf,我可以向您展示如何使用 pdfobject 库。

标签: r pdf shiny golem


【解决方案1】:

由于我不知道如何处理您询问的文件,因此我会将此文件编码为 base64 字符串并将此字符串设置为 src 属性:

library(base64enc)

output$preview <- renderUI({

  ......
    
  pdf_file_path <- "PATH/TO/PDF_FILE"
  b64 <- dataURI(file = pdf_file_path, mime = "application/pdf")
  
  tags$iframe(
    style = "height: 600px; width: 100%;", src = b64
  )
  
}

【讨论】:

  • 这很好用!谢谢!我很好奇使用 pdfobject 库的方法会是什么样子。
猜你喜欢
  • 2023-01-30
  • 2014-11-17
  • 2017-09-30
  • 1970-01-01
  • 2021-10-12
  • 2020-07-31
  • 2017-09-16
  • 1970-01-01
  • 2022-01-04
相关资源
最近更新 更多