Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I present a variable out of sequence in R markdown?

Tags:

markdown

r

I am preparing a .Rmd document that begins with an executive summary. I would like to include some inline R code to present a few of the key results up front; however, those results are calculated as part of the body later in the document.

Is there any way to present a result in the rendered document out of order/sequence with the actual calculation?

like image 993
sinjin Avatar asked Jun 19 '14 16:06

sinjin


1 Answers

You could use chunk reuse in knitr (see http://yihui.name/knitr/demo/reference/). Here you would put your chunks to analyze first but not create the output, then output the summary, then the details. Here is some quick markdown knitr code to show this:

```{r chunk1, echo=FALSE, results='hide'}
x <- rnorm(1)
x
```

the value of x is `r x`.

```{r chunk2, ref.label='chunk1', echo=TRUE, results='markup', eval=2}
```

Note that the code will be evaluated twice unless you take steps to prevent this (the eval=2 in my example).

Another option would be to create 2 child documents, the first runs your main code and creates the output, the second creates the summary. Then in your parent document you include the summary first, then the detail part. I think that you will need to run knitr on these by hand so that you do it in the correct order, the automatic child document tools would probably run in the wrong order.

like image 177
Greg Snow Avatar answered Oct 03 '22 20:10

Greg Snow