【问题标题】:Unable to see images in rmarkdown table无法在 rmarkdown 表中看到图像
【发布时间】:2019-12-03 15:58:55
【问题描述】:

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

我将表格中的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...

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

【问题讨论】:

  • 为什么在 URL 列中需要img src 标签?不知道是什么产生的......但我认为你应该摆脱它并尝试类似![](Images/Images Captured with Proc Data Audit.files/img2527.jpg)
  • 谢谢@daroczig。我已经删除了 img src,现在按照您的建议删除了,但我仍然将空格替换为 %20。我怎样才能避免这种情况?
  • 如果没有一个最小的可重现示例,真的很难弄清楚那里发生了什么:(你能尝试创建一个吗?例如一个有 2 行的小数据框,URL 可以指向一些在线的静态图像等等,但我怀疑您可能需要将pander 调用的style 更改为grid,以便正确解析降价。

标签: r shiny r-markdown


【解决方案1】:

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

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

解决方案是图像也应该被复制到同一个临时目录中,markdown 语法![](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())

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 1970-01-01
    • 2019-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多