StirlingCodingClub / studyGroup

Gather together a group to skill-share, co-work, and create community
http://StirlingCodingClub.github.io/studyGroup/
Other
2 stars 1 forks source link

Referencing code chunks in R Markdown #21

Open mattnuttall00 opened 5 years ago

mattnuttall00 commented 5 years ago

Hi all, I have a R Markdown question.

I am trying to write a results report for my project partners, and R Markdown is the obvious choice as the models/results/plots etc are not necessarily the final versions. The script for my analysis is getting pretty long (and is going to get much longer), and so ideally I don't want to have all of the original code in my .Rmd script.

I found a cool example online of how you can reference individual code chunks from your R script, and so you just add the reference in your Rmd script and it'll just run that one chunk and produce the output (rather than sourcing the entire R script). However, I can't get it to work properly.

I have created a very simple example to test it, and to demonstrate.

I create a new R script called example.R, which contains the following:

## @knitr plot1
x <- 1:100
y <- x + rnorm(100)
plot(x,y)

## @knitr plot2
z <- rnorm(100)
hist(z)

In the above, the ## @knitr plot1 and ## @knitr plot2 are supposed to be the chunk references. Then in the Rmd document I have:

knitr::opts_chunk$set(echo=TRUE)
knitr::read_chunk('example.R')
```{r plot1}```
```{r plot2}```

In the online example, it says that should find the plot1 and plot2 code chunks in example.R and run them and produce the output. The above two examples work, but slightly more complicated examples do not. For example, if I add the following to my R script:

## @knitr plot3
library('cowplot')
x <- 1:100
y <- x+rnorm(100)
z <- rnorm(100)
p1 <- plot(x,y)
p2 <- hist(z)
plot_grid(p1,p2)

And then put the following in my Rmd script:

```{r plot3```

Nothing happens when I try and run plot3 in Rmd. Does anyone know what I'm doing wrong? In my actual analysis script the code chunks don't seem to be doing anything (i.e. I guess I'm not referencing them properly?) Are there some tricks to referencing code chunks between an R script and a Rmd script that I'm missing?

Cheers!

MattGuy57 commented 5 years ago

This might be a stupid answer but do you just need to close off your curly brackets:

{r plot3}

mattnuttall00 commented 5 years ago

Ha! Not a stupid answer, but unfortunately that's just a typo, and in my actual code I have been more careful and have a closed bracket :)

jmcvw commented 5 years ago

Quick idea - Doesn't cowplot need a ggplot object?

mattnuttall00 commented 5 years ago

That's a good point - yes it does. Sorry, poor example. But in my actual results script the plots are all ggplot objects, and I get the same result. In my R script I have a bunch of ggplot histogram objects, named h1, h2, h3 etc, and the code chunk starts with a reference e.g. ## @knitr gpfHistograms. Then in the Rmd script I have

But it doesn't seem to find / run the chunk

mattnuttall00 commented 5 years ago

Blank space above was supposed to contain:

{r gpfHistograms}
jmcvw commented 5 years ago

Ok, just had another look. It works for me if I replace the @knitr with ----. See ?read_chunk.

mattnuttall00 commented 5 years ago

Hmmm, still can't get it to work. No worries, I think I need to start from the beginning as I've been messing around with the same Rmd script for a while now and I've probably got it all confused. Thanks for the suggestions guys. I'll have another crack at it when I get back from holiday :)