9

I am a new user to knitr. I know that knitr can "tangle out" (taken from the Literate programming community) or extract source code blocks into an R script file. Being an org-mode-user, I am used to being able to specify a specific file for each code block, with potentially the same file for different blocks. When "tangling" or extracting source in org-mode, instead of having one output code file, several code files are produced (this helps with modularity in large projects).

I wonder if something similar is possible in knitr? Can I specify the output file in knitr on a block by block basis?

Holger Hoefling
  • 388
  • 3
  • 13
  • Perhaps you could clarify the question a bit further. Providing examples would help. I am a regular knitr user, but I have no idea what you are asking. – Maxim.K Aug 11 '16 at 09:28
  • @Maxim.K I think the reason why you have no idea what Holger is asking is that the answer to the question is "no". But I totally agree, the question (c/sh)ould be clearer. I think Holger is looking for a chunk option that specifies the output file for `purl` on a per-chunk basis (which does not exist, AFAIK). – CL. Aug 12 '16 at 08:44
  • @CL yes - that is exactly what I was looking for. I realized already from the manual that there likely isn't a "simple" option, but I had hoped someone with more knitr knowledge than me knows how to write a hook function of some sort that can do this. – Holger Hoefling Aug 12 '16 at 08:58
  • I've been digging through the code a little bit. Probably `hook_purl` could be extended that way. Look at [line 123](https://github.com/yihui/knitr/blob/7a60145dcf2d03c45aa2eec6aa991570f6e87cd5/R/hooks-extra.R#L123): You could try to replace `output` by another filename, generated based on a (newly created) chunk option. However, I'm reluctant to implement this and post it as answer because I am not sure whether not writing to `.knitEnv$tangle.file` could have nasty side effects. – CL. Aug 12 '16 at 09:06
  • @CL Thanks. That is a good idea. For now I have filed a feature request in the knitr repo. I will see what Yihui says and take it from there. – Holger Hoefling Aug 12 '16 at 10:48
  • 1
    See [here](http://stackoverflow.com/questions/35855837/with-knitr-preserve-chunk-options-when-purling-chunks-into-separate-files/35863869#35863869) and [here](http://stackoverflow.com/questions/25800604/how-to-purl-each-chunks-in-rmd-file-to-multiple-r-files-using-knitr). – alistaire Aug 13 '16 at 00:49

1 Answers1

3

There are at least two different readings of your question, each requiring slightly different workflows.

If each chunk is going to be written into a separate output document, then to assist modularity, you should split the reporting part down into multiple documents. Since knitr supports child documents, you can always recombine these into larger documents in any combinations that you like.

If you want conditional execution of some chunks, and there are a few different combinations of conditions that can be run, use an R Markdown YAML header, and include a params element.

----
params:
  report_type: "weekly" # should be "weekly" or "yearly"
----

You can set which chunks are run by setting the eval and include chunk options.

```{r, some_chunk, eval = params$report_type == "weekly", include = params$report_type == "weekly"}
# chunk contents
```
Richie Cotton
  • 118,240
  • 47
  • 247
  • 360
  • Thanks - I think you are already pretty close. I actually don't want each chunk into separate files, I want to be able to specify for each chunk, in which file it goes (with potentially several into the same file). Your conditional execution comes close, except I don't want to execute them, I want to write them out. Overall, from all the comments and answers I have read, the answer seems to be that what I want to do is currently not possible. – Holger Hoefling Aug 16 '16 at 15:29
  • @HolgerHoefling Yeah, I was trying to think of workflows that would fit your need and be possible using the current capabilities of knitr. Sometimes it's better to tweak your workflow to the software rather than the other way around. – Richie Cotton Aug 16 '16 at 17:53