这是一个后续问题,或者是对这个问题的简化

通过这个Rmarkdown代码,我可以实现我想要的:

logo.png

enter image description here

report.Rmd

---
geometry: margin=20truemm
fontfamily: mathpazo
fontsize: 11pt
documentclass: article
classoption: a4paper
urlcolor: blue
output: 
    pdf_document:
header-includes:
   - \usepackage{fancyhdr}
   - \pagestyle{fancy}
   - \rhead{\includegraphics[width = .05\textwidth]{logo.png}}
params: 
    scores: NA
---
<!-- ```{r, echo=FALSE} -->
<!-- hist(params$scores) -->
<!-- ``` -->

```{r}
hist(runif(100))
```

Getting desired output: The R logo is in the header: enter image description here

现在我想在一个shiny 的应用程序上做同样的事情

For this I pass the plot as a parameter and uncomment the relevant part in the report.Rmd file

report.Rmd文件中的相关部分:

```{r, echo=FALSE}
hist(params$scores)
```

app.R

# Global variables can go here
n <- 200


# Define the UI
ui <- bootstrapPage(
  numericInput('n', 'Number of obs', n),
  plotOutput('plot'),
  downloadButton('report', 'Generate Report')
)


# Define the server code
server <- function(input, output) {
  output$plot <- renderPlot({
    hist(runif(input$n))
  })
  
  # create markdown report  ----------------------------------
  
  output$report <- downloadHandler(
    filename = "report.pdf",
    content = function(file) {
      tempReport <- file.path(tempdir(), "report.Rmd")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      
      params <- list(scores = input$n)
      
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
  
}

# Return a Shiny app object
shinyApp(ui = ui, server = server)

Error:

! Package pdftex.def Error: File `logo.png' not found: using draft setting.

我怀疑是因为它在本地起作用.在保存临时报告的临时文件中找不到png

但我不知道为什么这在markdown上编织时有效,而在shiny应用程序上调用时无效.

推荐答案

基本上你已经知道问题出在哪里了.因此,解决问题的一种方法是将报告模板和徽标复制到同一个临时目录中.

# Define the server code
server <- function(input, output) {
  output$plot <- renderPlot({
    hist(runif(input$n))
  })
  # create markdown report  ----------------------------------
  output$report <- downloadHandler(
    filename = "report.pdf",
    content = function(file) {
      td <- tempdir()
      tempReport <- file.path(td, "report.Rmd")
      tempLogo <- file.path(td, "logo.png")
      file.copy("report.Rmd", tempReport, overwrite = TRUE)
      file.copy("logo.png", tempLogo, overwrite = TRUE)

      params <- list(scores = input$n)

      rmarkdown::render(tempReport,
        output_file = file,
        params = params,
        envir = new.env(parent = globalenv())
      )
    }
  )
}

R相关问答推荐

如何在弹性表中为类别值的背景上色

使用R中的gt对R中的html rmarkdown文件进行条件格式设置表的单元格

在R中列表的结尾添加数字载体

如何使用R中的dhrr函数将李克特量表的因子列从长转换为宽?

在R中创建一个包含转换和转换之间的时间的列

过滤器数据.基于两列的帧行和R中的外部向量

更改默认系列1以更改名称

R-更新面内部的栅格值

如何根据嵌套元素的名称高效而优雅地确定它属于哪个列表?

使用整齐的计算(curl -curl )和杂音

从圆到R中的多边形的标绘雷达图

有没有办法使用ggText,<;Sub>;&;<;sup>;将上标和下标添加到同一元素?

将箭头绘制在图形外部,而不是图形内部

从多层嵌套列表构建Tibble?

从多个可选列中选取一个值到一个新列中

调换行/列并将第一行(原始数据帧的第一列)提升为标题的Tidyr类似功能?

如何根据未知数的多列排除重复行

如何调整一个facet_work()面板内的框图和移动标签之间的水平宽度?

按组使用dummy r获取高于标准的行的平均值

具有自定义仓位限制和计数的GGPLATE直方图