How to include output from the end of an Rmd in its introduction or TL;DR

It's always great to lead off a report with the key takeaways, for instance a TL:DR header. But it's also great to have a reproducible report where key takeaways are directly linked to your analysis via inline code (E.g., Mean performance was r MY_MEAN).

I'm wondering if there's a smart way to include stats in the TL;DR of an RMarkdown report when the TL;DR necessarily comes before the bulk of the code chunks that would generate its values.

I prefer putting almost everything in the opening chunk with assignment to named objects, which are them availability inline or for subsequent chunks.

I believe you can use the dependson code chunk option for this. Put your tl;dr code in a chunk with dependson set to a character vector of the (labelled) chunks providing values to be listed.

See https://yihui.org/knitr/options/ for further info...

Hi @joepowers16!

I believe you can do that several way. here are some ressources that will help find the solution that suits you.

There is also other feature that could help:

Hope it helps for your own needs.

1 Like

Using cache with two renderings worked great!

1 Like

image

@joepowers16 you can manage to not have to removed then paste back.

The function knitr::load_cache() handles for you the fact that if the variable is not found it will not error.
Example:

---
title: An important report
output: html_document
---

# TL;DR

* The mean mpg is `r knitr::load_cache("mpg", "my_mean")`
* The median mpg is `r knitr::load_cache("mpg", "my_median")`

# Details

We calculate some stats

```{r mpg, cache=TRUE}
my_mean <- mean(mtcars$mpg)
my_median <- median(mtcars$mpg)
```

First run will show NOT AVAILABLE.

You can recreate this by following the first example in the book chapter using the if clause.

The trick is to print something if the object is not found to not error and render anyway the first time. knitr::load_cache to that for you.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.