In my projects I have separate folders for documentation, plots, and data. I would like to run knit2html
from the project root directory. In this case the input test.Rmd
file is in the doc/
folder and I would like the output to go to the same place. I can do this; however, the test.md
file gets left in the parent directory (below).
library(knitr)
# set paths for knitr
opts_knit$set(base.dir=normalizePath(getwd()))
opts_knit$set(root.dir=normalizePath(getwd()))
opts_chunk$set(fig.path="figures/")
# create the output folder
dir.create("doc", showWarnings=FALSE)
dir.create("figures", showWarnings=FALSE)
# a minimal example
writeLines(c("```{r hello-random, echo=TRUE}", "plot(rnorm(5))", "```"),
"doc/test.Rmd")
# this works and outputs to test.html
knit2html(input = "doc/test.Rmd", output="doc/test.html")
list.files()
[1] "doc" "figures" "test.md"
list.files("doc")
#[1] "test.html" "test.Rmd"
The test.html
renders well, but of course the test.md
file is in the wrong place. Alternatively, I could do this...
library(knitr)
library(markdown)
# set paths for knitr
opts_knit$set(base.dir=normalizePath(getwd()))
opts_knit$set(root.dir=normalizePath(getwd()))
opts_chunk$set(fig.path="figures/")
# create the output folder
dir.create("doc", showWarnings=FALSE)
# a minimal example
writeLines(c("```{r hello-random2, echo=TRUE}", "plot(rnorm(5))", "```"),
"doc/test2.Rmd")
knit(input = "doc/test2.Rmd", output="doc/test2.md")
markdownToHTML(file = "doc/test2.md", output = "doc/test2.html")
list.files()
[1] "doc" "figures" "test.md"
list.files("doc")
#[1] "test.html" "test.Rmd" "test2.html" "test2.md" "test2.Rmd"
#correct output!
list.files("figures")
#[1] "hello-random-1.png" "hello-random2-1.png"
#correct output!
Here test.md
and hello-random-2.png
go to the right place, but test.html
is looking for hello-random-2.png
in doc/figures/hello-random2-1.png
and so it doesn't render correctly.
If I do opts_chunk$set(fig.path="../figures/")
(as described here: Working with knitr using subdirectories
), then the html is looking in the right place, but the png
file is in the parent of the project root and the html does not render.
That the intermediate test.md
doesn't go to the output path directory with knit2html
makes me wonder whether it is working as intended.