You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
rmarkdown::render(output_dir=) should be taken into account of knitr::include_graphics() #2473
This is a problem because output.dir is the output directory as understood by knitr. Paths passed to include_graphics(), for example, are made relative to output.dir. When we set rmarkdown::render(output_dir=), a user might reasonably expect that this would change the output.dir, and their document may fail in confusing ways when handling relative paths. In fact there seems to be no mechanism for setting the output.dir externally, which can make it very difficult to knit the same document to two different places, because the relative paths will be wrong in one of the cases.
Example
We have an RMarkdown document:
knitr::opts_knit$get("output.dir")
We knit it using: rmarkdown::render("UntitledRMD.Rmd", output_dir="foo")
Inside the vignette, this prints a directory that is notfoo.
Thanks for your feedback. I would like to try clarify the current situation, and know more about the exact issue you are encountering. This will help better understand the solution you are suggesting regarding output.dir which internal to knitr
output_dir in rmarkdown has another meaning. It is not exactly the same purpose. It is used to compute where the resources and output for rmarkdown::render() are placed. Not the one for the knitted step, but the results after knit + pandoc conversion.
I don't think they are or should be equivalent. Also because output_dir is user facing, and output.dir is internal.
They are currently not meant to be equal as rmarkdown::render() sets the working dir to be the root directory of the input file so output.dir internally to knitr will be set to that. ouput_dir is used to compute some paths for outputs inside rmarkdown processing.
For all this context, I would like to understand better the problem you are identifying by
When we set rmarkdown::render(output_dir=), a user might reasonably expect that this would change the output.dir, and their document may fail in confusing ways when handling relative paths.
output.dir usage being internal to knitr, changing could have a broad impact so we need to be sure of the underlying issue and why we would change it. Maybe the recent change in include_graphics() is not good enough and we should adapt that instead.
To help us further, can you build an example that reproduce the issue you have with your graphics relative path ? This would be the best for us to look into and really see if the solution suggested match the usage problem identified.
Here's my concrete use case. I have one vignette, which lives in vignettes. I want to be able to render it two different ways. The first is as a vignette, to html, using the normal vignette mechanism. The second is to render it to github_markdown, to use as a readme. The issue is that the image paths are not the same in these two use cases. In the former case, I want img.png, in the latter case I want vignettes/img.png. Therefore, I want to be able to tell RMarkdown "this is where I want my document to think it is located", namely "this is the directory I want images to be relative to". output.dir seems to be exactly this, since it's the directory that knitr makes images relate to. It seems that this option should be treated as private (which is possibly why setting it via options() and via knitr::opts_knit$set outside of the document don't seem to work!), so how should I do this? Here is a repo where I demonstrate what I want to happen and how I have currently solved it: https://github.com/multimeric/KnitrPortable
From what you've said, it sounds like I should be setting the working directory and output.dir will inherit from this. Can you show me an example of how I should be doing this?
I totally understand the different meaning of rmarkdown's output_dir, and I think that's valuable as its own option independent from output.dir. I just mention it because it was one thing that I tried to get at the output.dir, but unsuccessfully.
Vignettes are special use case of R Markdown rendering. Especially they are rendered in a package context, with specific rules regarding external resources.
Let me give a generic feedback in this context
The issue is that the image paths are not the same in these two use cases. In the former case, I want img.png, in the latter case I want vignettes/img.png.
Your example is out of the package context, so probably the above not apply, but they will be important unless you correctly embed you image content in the output (the default with self_contained = TRUE I believe)
Regarding your specific example now.
Thanks for it. I can't really run it though because rprojroot::find_package_root_file() does not work outside of a package context.
I see you are rendering a README from withing the vignette Rmd itself. This is not the best way IMO as you are using rmarkdown::render() from within an rmarkdown::render() - there will be some inherited context, and some possible side effect. I believe your path issues are also because of this.
I understand from this that you want to use absolute filepath to image because of this specific nested usage on the same file. I think you should use rel_path = FALSE in include_graphics() (or options knitr.graphics.rel_path if you want to change more globally). As we document in the function's help page
rel_path
Whether to automatically convert absolute paths to relative paths. If you know for sure that absolute paths work, you may set this argument or the global option knitr.graphics.rel_path to FALSE
Absolute paths are not recommended because depending on the output format, they will be used as is and it won't work correctly. Note that you could also compute the relative path yourself from an absolute path. You can use fs::path_rel() or xfun::relative_path().
That being said, I think that using this specific patterns of nested render with some Rmd file in a nested structure, wanting to output in a parent directory is indeed difficult to handle image paths. I don't think that will be the only use case where it is difficult.
Current workaround I have in mind
Use a parameter to pass your image path. You could even use inline markdown syntax like

You could then pass params in your render() call and change based on the file
You could compute yourself the relative path based on your idea, to not let knitr do it automatically (as it does not works for you)
In yihui/knitr@516b6f5 we are computing the relative path based on output.dir internal option, but this will not be coherent with output_dir from rmarkdown::render()
Global idea here is : should output_dir from R Markdown be somehow taken into account for this include_graphics() auto trasnformation to relative path ?
One workaround above is to compute relative path based on root.dir instead...
Global idea here is : should output_dir from R Markdown be somehow taken into account for this include_graphics() auto trasnformation to relative path ?
Yes, it sounds like a good idea to me to take rmarkdown's output_dir into account.
changed the title [-]`rmarkdown::render(output_dir=)` does not set `opts_knit$get("output.dir")`[/-][+]`rmarkdown::render(output_dir=)` should be taken into account of `knitr::include_graphics()`[/+]on Apr 17, 2023
Thanks for it. I can't really run it though because rprojroot::find_package_root_file() does not work outside of a package context.
Sorry, I forgot to commit the DESCRIPTION, this should be fixed now.
I see you are rendering a README from withing the vignette Rmd itself. This is not the best way IMO as you are using rmarkdown::render() from within an rmarkdown::render() - there will be some inherited context, and some possible side effect. I believe your path issues are also because of this.
I realise this is confusing, but this isn't what is happening. The cell that runs the render is set to eval=FALSE. It's just a mechanism to make it easy to build the vignette both ways:
By pressing the knit button in RStudio, which will build it like a vignette
By running this cell manually using the run button, which will build it into a readme
I understand from this that you want to use absolute filepath to image because of this specific nested usage on the same file. I think you should use rel_path = FALSE in include_graphics() (or options knitr.graphics.rel_path if you want to change more globally). As we document in the function's help page
Correct me if I'm wrong, but I don't think this is true. When knitting to GitHub markdown, I absolutely want the paths to be relative, because the relative paths need to be turned into relative URLs so they work on the GitHub readme page.
Use a parameter to pass your image path. You could even use inline markdown syntax like
I don't think this makes sense, because then I would have to pass in all the image paths, for each configuration I knit. I want one setting that changes all images. One solution similar to this is to pass in an "image root" as a parameter which all images are made relative to. Then you just set out.dir to equal params$image_root. But at that point I would prefer a mechanism to just set the out.dir directly.
You could compute yourself the relative path based on your idea, to not let knitr do it automatically (as it does not works for you)
This might work, not sure, but it's complex, and it would be nicer to utilise knitr's existing relative path rewriting functionality here instead of working around it.
Activity
cderv commentedon Apr 17, 2023
Thanks for your feedback. I would like to try clarify the current situation, and know more about the exact issue you are encountering. This will help better understand the solution you are suggesting regarding
output.dirwhich internal to knitrFirst
output.diris a value internal to knitr. It is not documented among available options to tweak and it is used to store the current working directory of the knitting stephttps://github.com/yihui/knitr/blob/e8aae19fcc7443b8a212c8dc65ba2b83894f1fe0/R/output.R#L159-L160
Currently the value is set to the current working dir of
knitr::knit()call and it is used when the original working directory is needed, like child mode context, cache handling. It was added toinclude_graphics()last year in yihui/knitr@516b6f5 to fix yihui/knitr#2119 so that absolute paths are converted to relative path based on the original working directory.output_dirin rmarkdown has another meaning. It is not exactly the same purpose. It is used to compute where the resources and output forrmarkdown::render()are placed. Not the one for the knitted step, but the results after knit + pandoc conversion.I don't think they are or should be equivalent. Also because
output_diris user facing, andoutput.diris internal.They are currently not meant to be equal as
rmarkdown::render()sets the working dir to be the root directory of the input file sooutput.dirinternally to knitr will be set to that.ouput_diris used to compute some paths for outputs inside rmarkdown processing.For all this context, I would like to understand better the problem you are identifying by
output.dirusage being internal to knitr, changing could have a broad impact so we need to be sure of the underlying issue and why we would change it. Maybe the recent change ininclude_graphics()is not good enough and we should adapt that instead.To help us further, can you build an example that reproduce the issue you have with your graphics relative path ? This would be the best for us to look into and really see if the solution suggested match the usage problem identified.
Thank you very much !
multimeric commentedon Apr 17, 2023
Thanks for the informative answer.
Here's my concrete use case. I have one vignette, which lives in
vignettes. I want to be able to render it two different ways. The first is as a vignette, to html, using the normal vignette mechanism. The second is to render it to github_markdown, to use as a readme. The issue is that the image paths are not the same in these two use cases. In the former case, I wantimg.png, in the latter case I wantvignettes/img.png. Therefore, I want to be able to tell RMarkdown "this is where I want my document to think it is located", namely "this is the directory I want images to be relative to".output.dirseems to be exactly this, since it's the directory thatknitrmakes images relate to. It seems that this option should be treated as private (which is possibly why setting it viaoptions()and viaknitr::opts_knit$setoutside of the document don't seem to work!), so how should I do this? Here is a repo where I demonstrate what I want to happen and how I have currently solved it: https://github.com/multimeric/KnitrPortableFrom what you've said, it sounds like I should be setting the working directory and
output.dirwill inherit from this. Can you show me an example of how I should be doing this?I totally understand the different meaning of rmarkdown's
output_dir, and I think that's valuable as its own option independent fromoutput.dir. I just mention it because it was one thing that I tried to get at theoutput.dir, but unsuccessfully.cderv commentedon Apr 17, 2023
Vignettes are special use case of R Markdown rendering. Especially they are rendered in a package context, with specific rules regarding external resources.
Let me give a generic feedback in this context
R Packages book has some recommandation on how to handle external file path, and the R-hub blog post some information about how to include external images with build vignette
Your example is out of the package context, so probably the above not apply, but they will be important unless you correctly embed you image content in the output (the default with
self_contained = TRUEI believe)Regarding your specific example now.
Thanks for it. I can't really run it though because
rprojroot::find_package_root_file()does not work outside of a package context.I see you are rendering a README from withing the vignette Rmd itself. This is not the best way IMO as you are using
rmarkdown::render()from within anrmarkdown::render()- there will be some inherited context, and some possible side effect. I believe your path issues are also because of this.I understand from this that you want to use absolute filepath to image because of this specific nested usage on the same file. I think you should use
rel_path = FALSEininclude_graphics()(or optionsknitr.graphics.rel_pathif you want to change more globally). As we document in the function's help pageAbsolute paths are not recommended because depending on the output format, they will be used as is and it won't work correctly. Note that you could also compute the relative path yourself from an absolute path. You can use
fs::path_rel()orxfun::relative_path().That being said, I think that using this specific patterns of nested render with some Rmd file in a nested structure, wanting to output in a parent directory is indeed difficult to handle image paths. I don't think that will be the only use case where it is difficult.
Current workaround I have in mind
Use a parameter to pass your image path. You could even use inline markdown syntax like
You could then pass
paramsin yourrender()call and change based on the fileYou could compute yourself the relative path based on your idea, to not let knitr do it automatically (as it does not works for you)
@multimeric does it helps ? Do you think that would work for you ?
@yihui long discussion above, but in short
In yihui/knitr@516b6f5 we are computing the relative path based on
output.dirinternal option, but this will not be coherent withoutput_dirfromrmarkdown::render()Global idea here is : should
output_dirfrom R Markdown be somehow taken into account for thisinclude_graphics()auto trasnformation to relative path ?One workaround above is to compute relative path based on
root.dirinstead...Sharing in case you have ideas.
yihui commentedon Apr 17, 2023
Yes, it sounds like a good idea to me to take rmarkdown's
output_dirinto account.[-]`rmarkdown::render(output_dir=)` does not set `opts_knit$get("output.dir")`[/-][+]`rmarkdown::render(output_dir=)` should be taken into account of `knitr::include_graphics()`[/+]multimeric commentedon Apr 18, 2023
Sorry, I forgot to commit the
DESCRIPTION, this should be fixed now.I realise this is confusing, but this isn't what is happening. The cell that runs the render is set to
eval=FALSE. It's just a mechanism to make it easy to build the vignette both ways:Correct me if I'm wrong, but I don't think this is true. When knitting to GitHub markdown, I absolutely want the paths to be relative, because the relative paths need to be turned into relative URLs so they work on the GitHub readme page.
I don't think this makes sense, because then I would have to pass in all the image paths, for each configuration I knit. I want one setting that changes all images. One solution similar to this is to pass in an "image root" as a parameter which all images are made relative to. Then you just set
out.dirto equalparams$image_root. But at that point I would prefer a mechanism to just set theout.dirdirectly.This might work, not sure, but it's complex, and it would be nicer to utilise knitr's existing relative path rewriting functionality here instead of working around it.