Including images when output dir is elsewhere
I have a simple.Rmd file with no more than:
test
``` {r Graph2, fig.width=7.7,fig.height=6.8}
plot(1,1)
```
If I use
knitr::knit2html("simple.rmd", output= "OUT.html")
everything is fine. If I use
some_where_else <- "C:\\Temp\\"
knitr::knit2html("simple.rmd", output=paste0(some_where_else, "OUT.html") )
the image is not included.
Warnmeldungen:
1: In replace(z) :
File 'figure/Graph2-1.png' not found (hence cannot be embedded).
Is this intended?
Versions: R 4.5.0 knitr 1.50 It works under R 4.3.3 and older knitr version 1.23 or something
Out of curiosity, is there a reason to use knitr::knit2html and not rmarkdown::render() with an Rmd file?
Are you still using R Markdown V1?
With rmarkdown::render() I don't have issue
dir.create(tmp_dir <- tempfile())
rmarkdown::render("test.Rmd", output_file = file.path(tmp_dir, "OUT.html"))
Using this document
---
title: "test"
output: html_document
---
```{r Graph2, fig.width=7.7,fig.height=6.8}
plot(1,1)
```
By not using **rmarkdown** I can reproduce
````markdown
```{r Graph2, fig.width=7.7,fig.height=6.8}
plot(1,1)
```
Internally here is what is happening
dir.create(tmp_dir <- tempfile())
out <- knitr::knit("test.Rmd")
markdown::mark(out, output=file.path(tmp_dir, "OUT.html"))
It seems markdown::mark() does expect file to be in another place.
However, markdown::mark() is now using litedown::mark() which knows how to handle Rmd doc. See https://yihui.org/litedown/
So real reprex is
dir.create(tmp_dir <- tempfile())
out <- knitr::knit("test.Rmd")
litedown::mark(out, output=file.path(tmp_dir, "OUT.html"))
@Stefan2015-5 Are you expecting to use rmarkdown::render() or markdown::mark() now litedown::mark() ? Just checking to guide you on the right trace. But thanks for the report!
@yihui it seems maybe knitr::knit2html() should move known resource folder to output directory for litedown::mark() to found them ? Or is this something in litedown::mark() for md rendering to html when output is another directory ?
`(three times)
@Stefan2015-5 Please read this to know how to format: https://yihui.org/issue/#please-format-your-issue-correctly
Thanks for pointing that out.
I must admit I do not know when to use rmarkdown::render and when knitr::knit2html()
rmarkdown::render fails too in including an image created in a (grand-) child.rmd if
. I provide an absolute path to the output destination rather than one based on the working dir, and
. the output destination starts with "//"
output.path <- "Output"
# path relative: succeedes
rmarkdown::render("OneTest.Rmd", output_file = paste( output.path, "/", "Gruppe ", Group_Letter, " Master.html",sep=""))
# path absolute to C: suceedes:
path<- path<- "C:\\Temp"
rmarkdown::render("OneTest.Rmd", output_file = paste( path,"/", output.path, "/", "Gruppe ", Group_Letter, " Master.html",sep=""))
# path absolute to \\mycompany fails (fails to include image, (output file otherwise correctly created and correctly placed))
path<- "//mycompany/daten/user/myself/Daten/Dokumente"
# (same as above):
rmarkdown::render("OneTest.Rmd", output_file = paste( path,"/", output.path, "/", "Gruppe ", Group_Letter, " Master.html",sep=""))
Could it be a confusion of / and \? At least the final output messages looks like a mixup (but it does so in all cases, working and not working)
Output created: C:\Temp/Output/Gruppe E Master.html
BTW: thanks for pointing to the style page. I apologize for not having done so. Corrected.
Path handling is complex, and can be hard especially when using different network drive. rmarkdown includes layer like Pandoc which does not work well with network drive sometimes.
Do you have the exact error message ?
Why not render in your main folder ? and then move the output to your network drive in another steps once the rendering is done ?
I must admit I do not know when to use rmarkdown::render and when knitr::knit2html()
If you intend to use rmarkdown with .Rmd file, you should use rmarkdown::render()
knit2html() is there for backward compatibility to handle those. And also to handle other type of file (any .md to html for example by calling knit() and then calling litedown::mark())
Do you have the exact error message ?
strangely, there is none with rmarkdown::render. With knitr::knit2html() there is the warning
1: In replace(z) :
File 'figure/Graph2-1.png' not found (hence cannot be embedded).
Why not render in your main folder ?
yes, will do so, so no real issue for me after figuring out what caused it, just an observation. Thanks!
strangely, there is none with rmarkdown::render. With knitr::knit2html() there is the warning
This is because rmarkdown::render() does call knit::knit() and then pandoc to convert to html and then move to output file. This is handling the resources correctly.
knitr::knit2html is not doing this resource management. It is just calling knitr::knit() which creates the figures folder, but then litedown::mark(..., output = ) does not consider this resource folder - probably because some embedding happens on the output that has already been moved, and resources where not.
So definitely a bug somewhere, or limitations