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