2

I am starting to embed R codes with text and I'm using knitr to generate pdf and html documents. I've already done that before by editing Rmarkdown files, but it seems to me that there's an advantage of compiling reports directly from R scripts (instead of from Rmarkdown files): it is much easier to run the R code itself in an .R document, and test it, to after that be able to add text and print it in a beaultiful and presentable fashion.

However, I noticed there is a difference in how code chunks are defined when using Rmd or R directly.

In Rmarkdown, to delimit a code chunk I need to set where it starts (it is denoted by ```{r}) and when it ends (which is denoted by ```). Everything between these two elements corresponds to R code and comments, that will be read as code in a single chunk.

When compiling the reports directly from R code, however, I can delimit when a chunk starts (by typing #+ or #- or anything like #---, according to the function spin, used to render the documents) but I do not set where the chunk ends (and I did not find information about that, yet). I assume that, in this notation, a code chunk automatically ends if another chunk is set to begin (through #+, for instance) or if something is set to be compiled as text (through #' in the beginning of the line). Otherwise, I would assume everything in the script is considered as the same code chunk.

Am I right about these assumptions?
Is there a way of explicitly stating that a code chunk has to end when compiling reports from R scripts?

Thanks in advance,
Bernardo

bniebuhr
  • 129
  • 1
  • 10
  • 1
    Does this web page, https://rmarkdown.rstudio.com/articles_report_from_r_script.html, answer your question? It's not clear what you're asking. Perhaps if you post a reproducible example? – user2554330 Sep 17 '18 at 12:40
  • I agree with @user2554330 that the question is rather unclear. But please have a look at [`read_chunk()`](https://yihui.name/knitr/demo/externalization/), as this might be what you are looking for. – CL. Sep 17 '18 at 12:55
  • Hello @user2554330, I am sorry, I still new to StackOverflow. I re-stated my question (and even the title), do you think it is clearer now? Thanks for the link, I had already visited it by I did not pay attention to the function spin, that presents some more information about when a code chunk is set to begin. I used that to re-state my question. – bniebuhr Sep 17 '18 at 14:32
  • @CL., thanks for the link, but I think this is not what I was looking for. `read_chunk()` may be used to read chunks from external scripts (what may be a solution for clearly delimiting where it starts and ends, indeed), but I was wondering how I could delimit the beginning and end of the chunk in a single R script (so that I keep the advantage of being able to run a whole analysis using only one R script - not many scripts nor one file with the code separated, as in .Rmd files). Is my point clear or I messed it again? (Sorry in advance in this case!) – bniebuhr Sep 17 '18 at 14:38
  • If my question is still unclear, please let me know so I'll think about an example. – bniebuhr Sep 17 '18 at 14:40
  • I still don't understand the issue. Why does it matter to you whether several lines of code are in one chunk or two? – user2554330 Sep 17 '18 at 16:13
  • Hi @user2554330, it is important for since I want to change the options for each chunk. For some of them I want to see only the resulting figures (in different sizes depending on the figure), for others I may also want to print the results, the error messages, or the warnings. Then, it would be good to make sure until where a certain set of local chunk options apply, so that the output report show exactly what I want for each code chunk. Does it make sense now? – bniebuhr Sep 17 '18 at 18:00
  • Anyway, from lots of tests I did here, it seems that my assumptions are correct... – bniebuhr Sep 17 '18 at 18:01
  • 1
    I think your assumptions *are* correct. If I have the time I'll add a more detailed answer later. – CL. Sep 18 '18 at 05:54

1 Answers1

1

I think I understand your question. It has taken me a long time to figure things out, I worked a little tonight.

a '#+' delimiter begins a section than can only be code. blank lines are ok, R-style comments are ok, but any #' style encoding for markdown will terminate the chunk. Any default chunk options will revert to default after embedded rmarkdown encoding.

I have an example coded if there's still interest in this.

Nathan Siemers
  • 423
  • 4
  • 8