闪亮/ RMarkdown:找不到外部图片

时间:2018-12-04 11:28:21

标签: shiny r-markdown

我有一个Shiny应用程序,它允许通过downloadHandler()创建PDF报告。我的.Rmd文件中包含一个外部图像文件。当我单独编织.Rmd文件时,将显示此图像。当我通过Shiny应用程序(调用.Rmd文件)生成PDF报告时,找不到图像并且无法生成报告。我在做什么错了?

Shiny应用程序的相关部分:

var dateComponent = DateComponents()
dateComponent.hour = 7
dateComponent.minute = 0

let trigger = UNCalendarNotificationTrigger(dateMatching: dateComponent, repeats: true)

这是我的.Rmd文件的相关部分:

  output$report <- downloadHandler(
    filename = "report.pdf",
    content = function(file) {
      # Copy the report file to a temporary directory before processing it, in
      # case we don't have write permissions to the current working dir (which
      # can happen when deployed).
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)

      abs_kontr <- "test variable"

      # Set up parameters to pass to Rmd document
      params <- list(abs_kontr = abs_kontr)

      # Knit the document, passing in the `params` list, and eval it in a
      # child of the global environment (this isolates the code in the document
      # from the code in this app).
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )

“ knitr :: include_graphics(” www / logo.jpg“)”是直接编译文档时有效的部分,但是当通过Shiny应用程序调用pdf创建时无效。当我想通过Shiny应用程序下载报告时,需要花费几秒钟的时间才能重定向到“找不到文件”页面,指示代码已崩溃。

1 个答案:

答案 0 :(得分:0)

感谢Using image in r markdown report downloaded from Shiny app,我自己找到了解决方案:

您还必须将所有包含的外部映像也复制到临时路径,以便找到它们!只需将以下内容添加到您的app.R:

tempReport2 <- file.path(tempdir(), "logo.jpg")
      file.copy("logo.jpg", tempReport2, overwrite = TRUE)