1 year ago

#388618

test-img

TarJae

Error: File header.tex not found in resource path in a rmarkdown generated pdf report from a shiny app

Update at the end!

This is a follow up question of this: How to pass a reactive plot generated in Shiny to Rmarkdown to generate dynamic reports

I would like to generate a header with a logo using a tex file to a pdf report output generated with Rmarkdown from a shiny app.

Everything seems to work but the tex.file cannot be find.

What am I doing wrong. Here is the working code:

app

library(shiny)
library(radarchart)

js <- paste0(c(
  "$(document).ready(function(){",
  "  $('#downloadPlot').on('click', function(){",
  "    var el = document.getElementById('plot1');",
  "    // Clone the chart to add a background color.",
  "    var cloneCanvas = document.createElement('canvas');",
  "    cloneCanvas.width = el.width;",
  "    cloneCanvas.height = el.height;",
  "    var ctx = cloneCanvas.getContext('2d');",
  "    ctx.fillStyle = '#FFFFFF';",
  "    ctx.fillRect(0, 0, el.width, el.height);",
  "    ctx.drawImage(el, 0, 0);",
  "    // Download.",
  "    const a = document.createElement('a');",
  "    document.body.append(a);",
  "    a.download = 'radarchart.png';",
  "    a.href = cloneCanvas.toDataURL('image/png');",
  "    a.click();",
  "    a.remove();",
  "    cloneCanvas.remove();",
  "  });",
  "});"
), collapse = "\n")

ui <- pageWithSidebar(
  headerPanel('Radarchart Shiny Example'),
  sidebarPanel(
    checkboxGroupInput('selectedPeople', 'Who to include', 
                       names(radarchart::skills)[-1], selected="Rich"),
    actionButton('downloadPlot', 'Download Plot'),
    downloadButton('report', 'Generate Report')
  ),
  mainPanel(
    tags$head(tags$script(HTML(js))),
    chartJSRadarOutput("plot1", width = "450", height = "300"), width = 7
  )
)

server <- function(input, output) {
  output$plot1 <- renderChartJSRadar({
    chartJSRadar(skills[, c("Label", input$selectedPeople)], 
                 maxScale = 10, showToolTipLabel=TRUE) 
  })
  
  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 = skills[, c("Label", input$selectedPeople)])
      
      rmarkdown::render(tempReport, output_file = file,
                        params = params,
                        envir = new.env(parent = globalenv())
      )
    }
  )
}
shinyApp(ui, server)

report.Rmd

---
title: "Dynamic report"
output: pdf_document
params:
  scores: NA
---
\vspace{-60truemm}

```{r}
chartJSRadar(params$scores, maxScale = 5, showToolTipLabel=TRUE)
```

```{r echo=FALSE, fig.width=5,fig.height=3,fig.cap="\\label{fig:figs}plotting example"}
library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_col()
```

I want to add a header with a logo to this pdf:

This works: When I press the knit button in Rstudio:

header.tex

\usepackage{fancyhdr}
\pagestyle{fancy}
\rhead{\includegraphics[width = .05\textwidth]{logo.png}}

report.Rmd file with header and logo

---
geometry: margin=20truemm
fontfamily: mathpazo
fontsize: 11pt
documentclass: article
classoption: a4paper
urlcolor: blue
output: 
    pdf_document:
      keep_tex: true
      includes:
          in_header: header.tex
params: 
    scores: NA
---
\vspace{-60truemm}

<!-- ```{r} -->
<!-- chartJSRadar(params$scores, maxScale = 5, showToolTipLabel=TRUE) -->
<!-- ``` -->


```{r echo=FALSE, fig.width=5,fig.height=3,fig.cap="\\label{fig:figs}plotting example"}
library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_col()
```

enter image description here

When I try to do the same thing with the same code from the shiny app:

It gives the error:

output file: report.knit.md

"C:/Program Files/RStudio/bin/pandoc/pandoc" +RTS -K512m -RTS report.knit.md --to latex --from markdown+autolink_bare_uris+tex_math_single_backslash --output pandoc66a45c476bc6.tex --lua-filter "C:\Users\tarka\Documents\R\win-library\4.1\rmarkdown\rmarkdown\lua\pagebreak.lua" --lua-filter "C:\Users\tarka\Documents\R\win-library\4.1\rmarkdown\rmarkdown\lua\latex-div.lua" --self-contained --highlight-style tango --pdf-engine pdflatex --include-in-header header.tex --variable graphics 
File header.tex not found in resource path
Warning: Error in : pandoc document conversion failed with error 99
  [No stack trace available]

Update: code with header-inlcudes

---
geometry: margin=20truemm
fontfamily: mathpazo
fontsize: 11pt
documentclass: article
classoption: a4paper
urlcolor: blue
output: 
    pdf_document:
      extra_dependencies:
          fancyhdr: null
      keep_tex: true
      latex_engine: xelatex
header-includes:
   - \usepackage{fancyhdr}
   - \pagestyle{fancy}
   - \rhead{\includegraphics[width = .05\textwidth]{logo.png}}
params: 
    scores: NA
---
\vspace{-60truemm}

```{r setup, include=FALSE}
knitr::opts_chunk$set(dpi=300,fig.width=7)
```

<!-- ```{r} -->
<!-- chartJSRadar(params$scores, maxScale = 5, showToolTipLabel=TRUE) -->
<!-- ``` -->


```{r echo=FALSE,  fig.width=5,fig.height=3,fig.cap="\\label{fig:figs}plotting example"}
library(ggplot2)

ggplot(iris, aes(Species, Sepal.Length)) +
  geom_col()
```

r

pdf

shiny

latex

r-markdown

0 Answers

Your Answer

Accepted video resources