Instructing RMarkdown to use relative filepaths or stop shortening folder names

Viewed 260

I have a parameterized RMarkdown PDF report that I am running from a Shiny dashboard (after copying it to a temporary directory).

Example code:

Shiny dashboard:

    ---
    title: "Company Report Frontend"
    output: 
    flexdashboard::flex_dashboard:
        orientation: columns
        vertical_layout: fill
    runtime: shiny
    ---
    
    ```{r global, include=FALSE}
    library(flexdashboard)
    library(shiny)
    ```
    
    ### Select Company
    
    ```{r}
    textInput('name', label ='Firm')
    ```
    
    ### Report
    
    ```{r}
    uiOutput("downloadUI")
    
    # Create the actual downloadButton
    output$downloadUI <- renderUI( {
        downloadButton("downBtn", "Download Report")
    })
    
    # Add download handling
    output$downBtn <- downloadHandler(
        filename = "full_report.pdf",
        content = function(file) {
            tempReport <- file.path(tempdir())
            file.copy("test_report.Rmd", paste0(tempReport, "/test_report.Rmd"), overwrite = TRUE)
    
            rmarkdown::render(paste0(tempReport, "/test_report.Rmd"), output_file = file,
            params = list(input=input),
            envir = new.env(parent = globalenv()), clean = FALSE,
            knit_root_dir = tempReport,
            )
        }
    )
    ```

test_report.RMD:

    ---
    title: "Report"
    header-includes: 
       \usepackage{graphicx}
    params:
      input: NA
    output:
      pdf_document:
        latex_engine: xelatex
    ---


    ```{r}
    input <- params$input 

    data(mtcars)
    plot(mtcars$hp, mtcars$mpg)
    title(input$name)
    ```

When I run it, it successfully produces the .tex file but then can't compile it. When I try to compile the .tex file directly in a LaTeX editor, I get undefined control sequence and Missing endcsname defined errors on any \includegraphics lines like this one:

\includegraphics{C:/User/LONGUS~1/Temp/file5b381fa967c8_files/figure-latex/unnamed-chunk-1-1.pdf}

where LONGUS~1 is a shortened folder name from the actual Windows username LONGUSERNAME.

The error goes away, and the PDF compiles, if I replace LONGUS~1 with LONGUSERNAME, or just point it to the relative filepath. LaTeX does tend to get finicky about filepaths sometimes.

How can I instruct RMarkdown to either avoid shortening folder names, or skip the absolute filepath and just use the relative? test_report.RMD compiles fine if I run it by itself (and specify a default input), so I'd guess this is something to do with the use of tempdir() or at least something in the render() function. But I really should keep the tempdir() stuff in the case of multiple simultaneous users of the shiny app. I did try removing the knit_root_dir option but that didn't fix it.

Any suggestions welcome. Thank you!

0 Answers
Related