1

How could I refer to and evaluate (in an .Rmd file) specific chunks of R code, located not in a different .Rmd file, but in an R module, containing chunks of code, tagged with ## @knitr chunk_name? Thanks!

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64

1 Answers1

0

I just figured out what the problem was: I have simply forgotten to call read_chunk() function for the R module, containing those external code chunks. So far, everything appears to be working, with the exception, mentioned below.

One problem I'm currently experiencing (and this might be a good separate question, but I'll leave it as is for now) is that knitr doesn't seem to respect working directory and paths, constructed on its basis, using relative paths, such as file.path(getwd(), "data/transform"). I think this contradicts with knitr design, which allows code reuse via chunks in external R modules. What are approaches that people are using to solve this peculiar situation? I believe that it might be a good idea to submit as a feature request.

Aleksandr Blekh
  • 2,462
  • 4
  • 32
  • 64
  • Relative paths are relative to the directory of the input file by default, as explained in the Note section of the documentation `?knitr::knit`. – Yihui Xie Sep 07 '14 at 04:40
  • @Yihui: Thanks, I read that. My problem is specific to the situation with having **multiple** *parent* and *child* documents in various directories. In this case, I think it would be optimal, if `knitr` would use paths relative to the one set in `root.dir`. I guess that's the reason you introduced this feature. However, it appears to be broken. Please see http://stackoverflow.com/q/25705821/2872891. – Aleksandr Blekh Sep 07 '14 at 04:50
  • @Yihui: As a **workaround**, for now I resorted to reading an *environment variable* set to project's home directory. I look forward to hearing your opinion on whether there is an bug in `knitr`, related to `root.dir`, or not and what I'm missing in this case. – Aleksandr Blekh Sep 07 '14 at 05:02