Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R markdown: Accessing variable from code chunk (variable scope) [duplicate]

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

like image 656
m.templemire Avatar asked Jun 05 '12 18:06

m.templemire


People also ask

What does ## do in R Markdown?

For example, # Say Hello to markdown . A single hashtag creates a first level header. Two hashtags, ## , creates a second level header, and so on. italicized and bold text - Surround italicized text with asterisks, like this *without realizing it* .

How do I use chunk codes in R?

You can insert an R code chunk either using the RStudio toolbar (the Insert button) or the keyboard shortcut Ctrl + Alt + I ( Cmd + Option + I on macOS). There are a large number of chunk options in knitr documented at https://yihui.name/knitr/options.


1 Answers

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 
like image 131
Maiasaura Avatar answered Sep 17 '22 21:09

Maiasaura