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
With the new file option (545aed3), there is now a way to include external document for chunk content.
If this is useful, it could become tricky when manipulating working directory. Currently, chunk options are evaluated without taking into account the root.dir, especially file and code
and currently chunk option are evaluated from the working dir set when knitr::knit is run. (so not even the input file directory )
Is a possible fix as simple as
diff --git a/R/block.R b/R/block.R
index 6eba1996..256465ba 100644
--- a/R/block.R+++ b/R/block.R@@ -37,7 +37,7 @@ call_block = function(block) {
params[["code"]] %n% unlist(knit_code$get(ref.label), use.names = FALSE)
} else {
# TODO: use xfun::read_all() so we can read multiple files at once
- xfun::read_utf8(code_file)+ xfun::in_dir(input_dir(), xfun::read_utf8(code_file))
}
# opts.label = TRUE means inheriting chunk options
I think this is important because currently it is a bit counter intuitive to deal with two different working dir which will impact the way relative file path are evaluated in chunks and in chunk option. It was mainly an issue when code option was used with a read function, and will now be more frequent probably with the file option.
Maybe a larger fix is needed for all chunk options - I did not test it yet with other option like fig.path - maybe this needs a fix only for code and file chunk option. For now, only child option has been fixed regarding this issue: #2059
For it to work, I needed to pass to file a relative path to R working dir of the session and not root.dir.
But this would not work if I were to use rmarkdown::render(), because render() will change the working dir before knit() to be the directory where the input file is
So the "mental model" of how working directory works with knitr and rmarkdown is not easy - also because usually you expect one working directory only for all R code.
I believe there is something to discuss and fix here.
Yes, I think it makes sense to use input_dir() to evaluate chunk options. Done now. Not sure if it will affect any reverse dependencies. I'll see. Thanks!
This old thread has been automatically locked. If you think you have found something related to this, please open a new issue by following the issue guide (https://yihui.org/issue/), and link to this old issue if necessary.
With the new
file
option (545aed3), there is now a way to include external document for chunk content.If this is useful, it could become tricky when manipulating working directory. Currently, chunk options are evaluated without taking into account the root.dir, especially
file
andcode
knitr/R/block.R
Lines 36 to 40 in 545aed3
input_dir()
is not usedknitr/R/utils.R
Lines 153 to 156 in faa5093
and currently chunk option are evaluated from the working dir set when
knitr::knit
is run. (so not even the input file directory )Is a possible fix as simple as
I think this is important because currently it is a bit counter intuitive to deal with two different working dir which will impact the way relative file path are evaluated in chunks and in chunk option. It was mainly an issue when
code
option was used with a read function, and will now be more frequent probably with thefile
option.Maybe a larger fix is needed for all chunk options - I did not test it yet with other option like
fig.path
- maybe this needs a fix only forcode
andfile
chunk option. For now, onlychild
option has been fixed regarding this issue: #2059Example code illustrating this issue
Setting up a dummy project
A working file
We have this
For it to work, I needed to pass to
file
a relative path to R working dir of the session and notroot.dir
.But this would not work if I were to use
rmarkdown::render()
, becauserender()
will change the working dir beforeknit()
to be the directory where the input file isI would need to pass a relative path to the input file
What would be expected is to pass a relative path compared to
root.dir
but that does not work.
So the "mental model" of how working directory works with knitr and rmarkdown is not easy - also because usually you expect one working directory only for all R code.
I believe there is something to discuss and fix here.
cc @jennybc
The text was updated successfully, but these errors were encountered: