Home > Back-end >  Render logo.png in header of pdf output shiny - Rmarkdown
Render logo.png in header of pdf output shiny - Rmarkdown

Time:04-10

This is a followup or more a simplification of this question 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

Now I would like to do the same from a shiny app

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

relevant part in report.Rmd file:

```{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.

I suspect because it works locally logo.png is not found in the temporary file where shiny saves tempReport

But I don't know why this works when knitting from markdown and not when calling it from the shiny app. I think I have been through of the relevant sites on the internet! Many thanks!

CodePudding user response:

Basically you already figured out what's the issue. Hence one approach to fix your issue would be to do copy both the report template and the logo to the same temporary directory.

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