How to get Shiny -> rmarkdown::render() to avoid absolute filepaths or shortened folder names

I have a parameterized RMarkdown PDF report that I am running from a Shiny dashboard.

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) {
	          rmarkdown::render("test_report.Rmd", output_file = file,
	            params = list(input=input),
	            envir = new.env(parent = globalenv()), clean = FALSE,
	            intermediates_dir = '.',
	            output_dir = '.',
	            knit_root_dir = '.'
	          )
        }
	    )
	  ```

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.

Some troubleshooting notes:

  • The error goes away, and the PDF compiles, if I open up the resulting .tex file and replace LONGUS~1 with LONGUSERNAME, or just point it to the relative filepath. LaTeX does tend to get finicky about filepaths sometimes. So the problem does appear to be the shortened folder paths, which could be fixed by not shortening them, or by getting rmarkdown::render() to produce relative filepaths
  • The error does not occur if knitting test_report.rmd directly from Rstudio (after setting a default input value). Nor does it occur when running rmarkdown::render() directly from Rstudio; this approach creates a relative filepath. So it does appear to be an issue with how Shiny sets up the folder for rmarkdown
  • Someone else tells me they managed to get my example code working fine on a Mac, so this appears to be a Windows thing
  • I have tried a whole lot of different settings on the _dir options in render() with no luck yet
  • I need this code to run on other peoples' computers, so setting the entire absolute filepath myself without shortened folder names is not a great solution.

So far Googling, StackOverflow, and Twitter have yet to produce a solution. But I can't imagine this is an unusual use case for parameterized documents.

Any help is appreciated, thank you!

This topic was automatically closed 54 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.