代码之家  ›  专栏  ›  技术社区  ›  TarJae

渲染徽标。pdf输出的页眉中的png-Rmarkdown

  •  0
  • TarJae  · 技术社区  · 2 年前

    这是对这个问题的后续或更简单的解释 Error: File header.tex not found in resource path in a rmarkdown generated pdf report from a shiny app

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

    标志巴布亚新几内亚

    enter image description here

    汇报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))
    ```
    

    获取所需输出:标题中有R徽标: enter image description here

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

    为此,我将情节作为一个整体传递 参数 并取消注释中的相关部分 汇报Rmd 文件

    相关部分 汇报Rmd 文件:

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

    应用程序。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)
    

    错误:

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

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

    但我不知道为什么这在markdown上编织时有效,而在shiny应用程序上调用时无效。 我想我已经浏览了很多相关的网站了! 非常感谢!

    1 回复  |  直到 2 年前
        1
  •  1
  •   stefan    2 年前

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

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