Open mattnuttall00 opened 5 years ago
This might be a stupid answer but do you just need to close off your curly brackets:
{r plot3}
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 :)
Quick idea - Doesn't cowplot
need a ggplot
object?
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
Blank space above was supposed to contain:
{r gpfHistograms}
Ok, just had another look. It works for me if I replace the @knitr
with ----
. See ?read_chunk
.
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 :)
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:In the above, the
## @knitr plot1
and## @knitr plot2
are supposed to be the chunk references. Then in the Rmd document I have:In the online example, it says that should find the
plot1
andplot2
code chunks inexample.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:And then put the following in my Rmd script:
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!