0

I'm wondering if it's possible to hook into the code in an external R script that is read by knitr.

Specifically, say that you have the following R file

test.R

## ---- CarPlot
library(ggplot2)
CarPlot <- ggplot() +
    stat_summary(data = mtcars,
                 aes(x = factor(gear),
                     y = mpg
                     ),
                 fun.y = "mean",
                 geom = "bar"
                 )
CarPlot

Imagine that you wanted to use this graph in multiple reports, but in one of these reports you want the graph to have a title and in the other report you do not.

Ideally, I would like to be able to use the same external R script to be able to do this so that I do not have to make changes to multiple R files in case I decide to change something about the graph.

I thought that one way to perhaps do this would be by setting the fig.show chunk option to hold—since it will "hold all plots and output them in the very end of a code chunk"—and then appending a title to the plot like so:

test.Rnw

\documentclass{article}

\begin{document}

<<external-code, cache=FALSE,echo=FALSE>>=
read_chunk('./test.R')
@

<<CarPlot,echo=FALSE,fig.show='hold'>>=
CarPlot <- CarPlot + ggtitle("Plot about cars")
@

\end{document}

This, however, does not work. Although the plot is printed, the title that I tried to append does not show up.

Is there some way to do what I would like to do?

Adam Liter
  • 875
  • 2
  • 10
  • 30
  • "does not work" is a poor descriptor. If you mean you didn't see a plot, that's because that code wouldn't print the plot normally, just like `x <- 1` doesn't print `x`. You need to explicitly `print(CarPlot)`, or just call it on a line by itself `CarPlot` after modifying it. Setting `fig.show` is unnecessary. – Gregor Thomas Jun 05 '15 at 01:11
  • @Gregor It shows the plot, but the plot does not have the title I tried to append in the `.Rnw` file. If you look at the `test.R` file you will see that there is indeed a call to `CarPlot` on a line by itself. I don't understand how `fig.show='hold'` works, but I thought that it might prevent that line from being executed until the end of the chunk (based on the description of its functionality), allowing me to append a title. – Adam Liter Jun 05 '15 at 01:14
  • So, in the end, this boils down to R FAQ 7.22 [as seen here](http://stackoverflow.com/q/6675066/903061) – Gregor Thomas Jun 05 '15 at 01:15
  • @AdamLiter There's a call to `CarPlot` in `test.R`, but all that happens *before* your `CarPlot <- CarPlot + ggtitle("Plot about cars")` line in the following chunk. What if you remove the `CarPlot` line from `test.R`, and add it to the end of the `CarPlot` chunk? – David Robinson Jun 05 '15 at 01:15
  • @DavidRobinson Yes, I understand that. What I'm trying to ask is if there's a way to effectively run `CarPlot <- CarPlot + ggtitle("Plot about cars")` before `CarPlot` without having to explicitly do that in the external R file. – Adam Liter Jun 05 '15 at 01:17
  • `test.R` prints the plot, but since you don't want **that** chunk to print the plot (as it's still unmodified), you should change the chunk option to, say `include = FALSE`. It's your next chunk where you modify the plot. If you want to print the modified plot, you need to `print(CarPlot)` or `CarPlot` **after** you modify it by adding the title. – Gregor Thomas Jun 05 '15 at 01:17
  • You don't need to remove the `CarPlot` line from `test.R`, just hide the figures for the chunk that calls `test.R`, and explicitly print the plot after you modify it as you like. – Gregor Thomas Jun 05 '15 at 01:18
  • @Gregor Ah, thanks! `:)` I should have thought about that. I forgot about the `include` chunk option. Do you want to convert that into an answer? Or is there a duplicate somewhere around? – Adam Liter Jun 05 '15 at 01:19
  • The duplicate I'd suggest is the r-faq linked above, but this isn't *just* that... – Gregor Thomas Jun 05 '15 at 01:26

1 Answers1

2

You don't want to show the plot created by test.R, so you should set fig.show = 'hide' or include = FALSE for that chunk:

<<external-code, cache=FALSE,echo=FALSE,fig.show = 'hide'>>=
read_chunk('./test.R')
@

You do want to show the plot after modification, so you have to print it:

<<CarPlot,echo=FALSE>>=
CarPlot <- CarPlot + ggtitle("Plot about cars")
CarPlot
@

fig.show = 'hold' is used if you have a large code chunk that prints a plot in the middle, but you don't want the plot to show in your document until the end. It doesn't apply to this case.

Gregor Thomas
  • 136,190
  • 20
  • 167
  • 294