Closed gadenbuie closed 3 years ago
Hmm, are there other situations where relative paths would be required? It's a pretty easy fix if it makes sense to always use relative instead of absolute paths.
I just left a comment that overlaps with this in terms of absolute/relative paths. Building the html
file is definitely a unique in that it basically needs to happen in the same directory as the source Rmd. Once you have the html
file, everything else can be referenced by absolute path.
I realized that another thing we can do to fix this is to force self_contained = TRUE
when the user wants to render the HTML file into a directory that isn't the same as the input directory. This definitely makes life a bit easier.
And since turning on or off self_contained
to build slides is a thing that people will want to do without having to muck with the YAML headings, I made self_contained
an argument of build_html()
in a6b24e0dd4c5f507a23199ceb54c0c2b61d7a0b1.
It's a bit of a quirk of
rmarkdown::render()
, but using an absolute path inoutput_file
or usingoutput_dir
can cause problems that are often not easily apparent until working in an environment where relative paths are required. In particular, using either of the above will causermarkdown::render()
to write figure outputs using absolute file paths. This becomes an issue when an HTML file is served to a browser where the paths need to be relative.Here's a reprex that demonstrates the issue:
The image link points to the full absolute file path of the the rendered plot, but the absolute path can't be found once the page is being served to a browser. (Notice the link starts with
/private/var/...
.)Instead we need the relative URL, e.g.
demo_fies/figure-html/unnamed-chunk-3-1.png
.If we need to be able to support rendering the HTML with an output file in a different folder, it's best to move into the folder containing
input.Rmd
, render the file to a temporary file (in that folder), and then move the temp file to a new folder. Except in the case of xaringan, this is likely to fail without a bunch more work, unless the slides areself_contained: true
.In light of the above, I'd recommend temporarily moving into the directory containing the input file before calling
rmarkdown::render()
and giving just thebasename(output_file)
to theoutput_file
argument.