在rmarkdown表中看不到图像

时间:2019-12-03 15:58:55

标签: r shiny r-markdown

我正在尝试从一个闪亮的应用程序创建可下载的报告。该报告显示其中有图像的表。

我将表中带有URL的表作为参数传递给报表,如下所示:

server <- function(input, output,session) {



  data <- reactive({
    data <- data.frame(RV3$data[,input$Map_EndoscopistIn],
                       RV3$data[,input$Map_FindingsIn],
                       RV3$data[,input$Map_MicroscopicTextIn],
                       RV3$data$url)


    names(data)<-c(input$Map_EndoscopistIn,input$Map_FindingsIn,input$Map_MicroscopicTextIn,"Image")
    if(!is.null(input$EndoscopistChooserIn)){
      data<-data%>%filter(get(input$Map_EndoscopistIn)==input$EndoscopistChooserIn)
    }



    data
  })  


}




output$report <- downloadHandler(
    # For PDF output, change this to "report.pdf"
    filename = "report.doc",
    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)

      browser()
      # Set up parameters to pass to Rmd document
      params <- list(performanceTable=data()
                     )

      # 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())
      )
    }
  )

data()中的url列包含以下路径,例如:

![](<img src='Images/Images Captured with Proc Data Audit.files/img2527.jpg'>)

报告如下:

---
title: "Dynamic report"
always_allow_html: yes
output:
  word_document:
    fig_caption: true
params:
  performanceTable: NA
---


```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)

library(ggplot2)
```

```{r,echo=FALSE,warning=FALSE}

pander(params$performanceTable, justify='left',split.table=Inf,caption="Table 1: Cancer diagnoses")


```

该报告与“图像”文件夹位于同一文件夹中。

由此我得到以下错误示例:

[pandoc warning] Could not find image `img%20src='Images/Images%20Captured%20with%20Proc%20Data%20Audit.files/img2500.jpg'', skipping...

我在做什么错?这是重新设置格式问题还是路径问题,还是什么?或者这可能是报告的根目录是tmp目录,而我指的是存储在其他静态目录中的图像。如果是后者,我该怎么做才能获得相对于temp目录的路径,以便可以看到图像?

1 个答案:

答案 0 :(得分:0)

无法找到图像文件的原因是因为您从不存在图像的临时目录中渲染了report.Rmd

tempReport <- file.path(tempdir(), "report.Rmd") 
file.copy("report.Rmd", tempReport, overwrite = TRUE)

解决方案是,还应将映像复制到同一行的临时目录,降价语法![](img.png)将在该目录中拾取该文件。

在代码更改中,您应该可以通过以下操作使其正常工作:

分配一个tempDir

tempDir <- tempdir()

将Rmd以及图像移动到tempDir

file.copy(c("report.Rmd", "Images/Images Captured with Proc Data 
Audit.files/img2527.jpg"), tempDir, overwrite = TRUE)

然后从临时目录渲染

rmarkdown::render(file.path(tempDir, 'report.Rmd'), output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())