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"
		orientation: columns
		vertical_layout: fill
	runtime: shiny
	```{r global, include=FALSE}
	### Select Company
	textInput('name', label ='Firm')
	### Report

	  # 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 = '.'


	title: "Report"
	  input: NA
		latex_engine: xelatex

	input <- params$input 

	plot(mtcars$hp, mtcars$mpg)

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:


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!