Reputation: 25
I am new to knitr in R and love it. There is two items I am struggling with.
I would like to execute a code chunk (echo=F), talk about the results of the operations (with numbers available), then display the code chunk. I could use the eval/echo toggles to accomplish this, but would have to duplicate the code before and after I talk about it. Which is fine except when I have to modify the code, then I need to change it in both places.
Is there another way to do it?
Second, I would like to do the above, then include the code, minus all the text in the middle as an appendix. Looking at the similar questions that are now popping up, it looks like I need to look up purl. Any thoughts?
Thanks, Bob
Upvotes: 2
Views: 62
Reputation: 20463
You can do this with the ref.label
parameter.
```{r chunk1, echo = FALSE} summary(cars) ``` Here I am talking about `chunk1` even though we did not see the code that generated its results. Let's look at the code that made the output above... ```{r chunk2, ref.label = `chunk1`, eval = FALSE} # Nothing here, not going to evaluate... ``` Now we see the code that generated `chunk1` even though I did not re-evaluate it nor did I have to copy it.
Knitting this to HTML
should generate:
Note: There is no reason to name the chunks chunk1
, chunk2
, etc. You can name them whatever you'd like and refer to them throughout the .Rmd
document.
Upvotes: 1