Consider the following reprex:
writeLines(text = "blablabla",
con = "child.Rmd")
writeLines(text = c("# Minimal child document example",
"",
"``` {r, child = 'child.Rmd'}",
"```"),
con = "main.Rmd")
tmp_file <- tempfile(fileext = ".Rmd")
readLines(con = "main.Rmd") |>
stringr::str_replace(pattern = "(# Minimal )",
replacement = "\\1R Markdown ") |>
writeLines(con = tmp_file)
# this renders fine (child document found)
rmarkdown::render(input = "main.Rmd",
output_format = rmarkdown::github_document(),
quiet = TRUE)
# but this doesn't (child document NOT found)
rmarkdown::render(input = tmp_file,
output_format = rmarkdown::github_document(),
knit_root_dir = getwd(),
quiet = TRUE)
#> Warning in call_block(x): The chunk 'unnamed-chunk-1' has the 'child' option,
#> and this code chunk must be empty. Its code will be ignored.
#> Warning in file(con, "r"): cannot open file './child.Rmd': No such file or
#> directory
#> Quitting from lines 4-4 (./child.Rmd)
#> Error in file(con, "r"): cannot open the connection
Created on 2021-10-09 by the reprex package (v2.0.1)
Obviously, knitr's root.dir
option (set via rmarkdown::render(knit_root_dir = getwd())
) is ignored for resolving relative child document paths and only respected for code evaluation inside code chunks.
Now I wonder: Why?
And more importantly: How can I change the root directory for resolving relative child document paths? I couldn't find anything about this so far...
(The warning saying The chunk 'unnamed-chunk-1' has the 'child' option and this code chunk must be empty
only occurs inside the reprex, and I have no clue why.)