Rmarkdown - 从闪亮的应用程序下载时无法在 html 报告中呈现图像

Rmarkdown - cannot render image in html report when downloaded from shiny apps

我正在尝试从我闪亮的应用程序制作“可下载的 html 报告”,但是当我将应用程序部署到 shinyapps.io 并尝试下载报告时,它失败了,因为我可以' 在 R markdown 文件中渲染图像。它在本地运行良好,这意味着我认为问题与相对文件路径有关。

为方便起见而制作的简短示例:

app.R

library(shiny)
library(dplyr)
library(tidyverse)
library(knitr)
library(here)


#load pca plots from working directory
pca <- list.files(pattern="*pca_check.png")
#move file to www folder for it to render correctly
dir.create("www")
file.copy(pca[[1]], "www")

#pca[[1]] is "sept_2021.pca_check.png"


##################
# Make Shiny App #
##################

ui <- fluidPage(titlePanel("QC output"),
                navbarPage("Menu",
                           tabPanel("Report",
                                    sidebarLayout(
                                      sidebarPanel(downloadButton("report", "Generate report"), width=0
                                      ),
                                      mainPanel(tags$h2("Ancestry prediction Peddy"),
                                                a(img(src=pca[[1]], height = 500, width = 300, slign="center",
                                                      target="_blank"),
                                                  href=pca[[1]])
                                                )))))
                           
                          

server <- function(input, output) {
  
  output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.html",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      
      # Set up parameters to pass to Rmd document
      params <- list(pca = pca[[1]])
      
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
}

shinyApp(ui = ui, server = server)

注意:使用此代码时图像在应用程序中显示正常:

a(img(src=pca[[1]], height = 500, width = 300, slign="center",
                                                      target="_blank"),
                                                  href=pca[[1]])

但是,生成报告时失败...

report.Rmd

---
title: "Dynamic report"
output: html_document
params:
  pca: NULL
---

PCA plot
```{r out.width="70%"}
knitr::include_graphics(here::here(pca))```

如果不使用 here::here(pca),则代码在本地失败。然而,它在部署时显然给出了错误的路径。所以,我只是尝试了:

knitr::include_graphics(pca)

部署时仍然失败。完整的应用程序在这里:https://lecb.shinyapps.io/QC_sept_21/ and the image in question was successfully uploaded to https://lecb.shinyapps.io/QC_sept_21/sept_2021.pca_check.png,这意味着我指的是正确的目录......也许降价不知道“工作目录”是什么?

有人知道如何在可下载报告中呈现图像吗?

非常感谢!

knitr::rendr.Rmd 文件所在的文件夹视为根文件夹。您正在将 report.Rmd 复制到临时文件夹。因此,将您的 png 复制到同一个临时文件夹并在没有 here 的情况下引用它应该可以在远程和本地完成:

未经测试的代码片段:

  output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.html",
    content = function(file) {
      tmp_dir <- tempdir()
      tempReport <- file.path(tmp_dir, "report.Rmd")
      tmp_pic <- file.path(tmp_dir, pca[[1]])
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      file.copy(pca[[1]], tmp_pic, overwrite = TRUE)
      # Set up parameters to pass to Rmd document
      params <- list(pca = pca[[1]])
      
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )

---
title: "Dynamic report"
output: html_document
params:
  pca: NULL
---

PCA plot
```{r out.width="70%"}
knitr::include_graphics(params$pca)
```