R markdown: Accessing variable from code chunk (variable scope)

m.templemire picture m.templemire · Jun 5, 2012 · Viewed 57.4k times · Source

In R markdown (knitr package), can I access a variable within the body of the document that was calculated in a code chunk?

Answer

Maiasaura picture Maiasaura · Jun 5, 2012

Yes. You can simply call any previously evaluated variable inline.

e.g. If you had previously created a data.frame in a chunk with df <- data.frame(x=1:10)

`r max(df$x)`

Should produce

10