2

Imagine a simplified bookdown/rmarkdown document that goes something like this:

---
title: "Test Doc"
author: "Balin"
date: "May 25, 2018"
output: 
  bookdown::pdf_document2:
    toc: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

<!-- Placeholder - See question -->

This stands in for an extensive report where `code`, its documentation
and interpretation of its results are integrated:

1. We load some data:
   ```{r data-loading}
    my_data <- cars
   ```

2. We (rougly) explore that data and report on it:
   ```{r data-exploration}
   summary(my_data)
   ```

3. We transform the data:
   ```{r data-transform}
   my_data <- log2(my_data)
   ```

4. ... many, many more steps ...

5. We perform a (central) graphical analysis:
   ```{r data-plot}
   plot(my_data)
   ```

6. We state some interpretation ... etc.

In such a report I am aiming to replace the <!-- Placeholder - See question --> bit with an "Executive Summary"/"Sneak-Peak" section, that centers on the graphical output of chunk data-plot. Is that achievable in bookdown/rmarkdown/knitr while maintaining the code/narrative integration given the relative positioning?

balin
  • 1,554
  • 1
  • 12
  • 26
  • 1
    Down voting without a comment - I consider that impolite. – balin May 25 '18 at 12:40
  • This is a perfect question in my eyes, and also highly useful. However, to survive on Stack Overflow, [you have to learn to ignore some irresponsible downvoters who don't really know what they are doing](https://yihui.name/en/2018/05/right-easier-than-wrong/). Haters gonna hate, and downvoters gonna downvote. – Yihui Xie May 25 '18 at 14:09
  • Thank you indeed for your soothing words. – balin May 25 '18 at 19:22

1 Answers1

2

Yes, you can use knitr::fig_chunk() to dynamically retrieve the path to a figure produced in a specific code chunk, e.g.,

---
title: "Test Doc"
author: "Balin"
date: "May 25, 2018"
output: 
  bookdown::pdf_document2:
    toc: no
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

# Executive Summary {-}

Here is an amazing discovery!

![](`r knitr::fig_chunk('data-plot', 'pdf')`)

# Detailed analysis

This stands in for an extensive report where `code`, its documentation
and interpretation of its results are integrated:

1. We load some data:
   ```{r data-loading}
    my_data <- cars
   ```

2. We (rougly) explore that data and report on it:
   ```{r data-exploration}
   summary(my_data)
   ```

3. We transform the data:
   ```{r data-transform}
   my_data <- log2(my_data)
   ```

4. ... many, many more steps ...

5. We perform a (central) graphical analysis:
   ```{r data-plot}
   plot(my_data)
   ```

6. We state some interpretation ... etc.

PDF output

To make this work for other types of output formats, you may need to change the filename extension pdf. One way to do it can be:

![](`r knitr::fig_chunk('data-plot', if (knitr::is_latex_output()) 'pdf' else 'png')`)

Of course, this assumes that you use the pdf device for LaTeX/PDF output formats, and use png for other formats (which are the default settings for graphical devices in R Markdown).

Yihui Xie
  • 28,913
  • 23
  • 193
  • 419
  • Thank you very much @Yihui Xie ! Yet another great detail of your suite of tools! Am I correct in assuming that if I want to be able to compile to both `*.pdf` (via `LaTeX`) and `*.docx`, the file extension must change to `png` in the latter case? Is there an easily queried variable to check what we are compiling towards? – balin May 25 '18 at 20:32
  • ... and something else: does it matter whether the plot producing chunk is in a child document? – balin May 25 '18 at 20:34
  • It does not matter if it is called in a child document or not. For Word, yes, you need to change the filename extension to `png`. I have updated my answer. – Yihui Xie May 25 '18 at 21:53
  • Outstanding! Thank you very much! – balin May 26 '18 at 06:35
  • Is there a way to have `![](`r knitr::fig_chunk('data-plot', if (knitr::is_latex_output()) 'pdf' else 'png')`)` itself embedded into inline code? I want to do something like `r if(thisistrue){cat("the link")}`, but neither cat, nor paste bend to my will ... – balin Jul 02 '18 at 07:46