rmarkdown icon indicating copy to clipboard operation
rmarkdown copied to clipboard

Clarify that the `output_file` directory is relative to the `input` directory

Open billdenney opened this issue 2 years ago • 3 comments

I just hit an unexpected issue that appears to be documentation related.

It appears that the directory for the output_file should be relative to the directory of input rather than relative to the current working directory where rmarkdown::render() is run. I think that this could be clarified by adding "The output_dir is relative to the directory for input and not relative to the current directory."

And, that clarification would go about here:

https://github.com/rstudio/rmarkdown/blob/f2bfeec501441abfa17b64fce12de7157bd75940/R/render.R#L171-L178

billdenney avatar Jun 06 '22 14:06 billdenney

"The output_dir is relative to the directory for input and not relative to the current directory."

output_dir is relative to the current directory as setwd() was not yet called on input directory.

https://github.com/rstudio/rmarkdown/blob/f2bfeec501441abfa17b64fce12de7157bd75940/R/render.R#L339-L345

It appears that the directory for the output_file should be relative to the directory of input rather than relative to the current working directory where rmarkdown::render() is run.

output_file is relative to the output_dir is this is specified https://github.com/rstudio/rmarkdown/blob/f2bfeec501441abfa17b64fce12de7157bd75940/R/render.R#L493-L497

Paths handling in rmarkdown is quite complex and we have encountered some limitations already (https://github.com/rstudio/rmarkdown/issues?q=is%3Aopen+label%3A%22theme%3A+paths%22+sort%3Aupdated-desc). Technically, there is even nothing that prevents the paths to be absolute too or relative but in a parent folder (e.g ../<dir>), however we've seen with time that paths handling is tricky and using non relative dir / file can cause side effect (example https://github.com/rstudio/rmarkdown/issues/2024). So it is a recommandation to use dir / files relative to input file, but not mandatory.

I just hit an unexpected issue that appears to be documentation related.

What was the issue exactly ? This may yet be another limitation we have.

cderv avatar Jun 06 '22 15:06 cderv

I recognize that file paths are complex here. Having worked on something vaguely similar in the past (though FAR less complex), I know it's nontrivial to keep all the parts moving correctly. (That's why I indicated this as a documentation issue rather than a bug.)

The reprex is below. When I traced the "The directory 'issue2370' does not exist" error, it was looking for an issue2370/issue2370 directory under tempdir().

The thing for me is that I did not specify output_dir, so by my reading of the help, output_file without output_dir should have an output_file location relative to getwd().

This is on Windows 10, R 4.1.2, rmarkdown 2.14.

tempdir()
#> [1] "C:\\Users\\BILLDE~1\\AppData\\Local\\Temp\\Rtmpwdx1y6"
newdir <- file.path(tempdir(), "issue2370")
dir.create(newdir)
setwd(file.path(newdir, ".."))
file_to_create <- "issue2370/foo.Rmd"
cat("---\ntitle: 'Untitled'\nauthor: 'William Denney'\ndate: '2022-06-06'\noutput: pdf_document\n---\n\nfoo\n", file=file_to_create)
rmarkdown::render(input=file_to_create, output_file="issue2370/foo.pdf")
#> Error: The directory 'issue2370' does not not exist.

Created on 2022-06-06 by the reprex package (v2.0.1)

billdenney avatar Jun 06 '22 16:06 billdenney

Thanks I'll look into this, and adapt the documentation

cderv avatar Jun 06 '22 16:06 cderv