bookdown/rmarkdown/knitr:用更高版本的代码块的(图形)结果打开文档吗? [英] bookdown/rmarkdown/knitr: Open a document with the (graphical) result of a later code chunk?

查看:158
本文介绍了bookdown/rmarkdown/knitr:用更高版本的代码块的(图形)结果打开文档吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一个简化的bookdown/rmarkdown文档,其内容如下:

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.

在这样的报告中,我旨在将"c2>"位替换为执行摘要"/隐密峰值"部分,该部分以块data-plot的图形输出为中心.在给定相对位置的情况下保持code/叙述性整合,同时bookdown/rmarkdown/knitr是否可以实现?

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?

推荐答案

是的,您可以使用knitr::fig_chunk()动态检索以特定代码块(例如

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.一种方法是:

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')`)

当然,这假定您将pdf设备用于LaTeX/PDF输出格式,并将png用于其他格式(这是R Markdown中图形设备的默认设置).

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).

这篇关于bookdown/rmarkdown/knitr:用更高版本的代码块的(图形)结果打开文档吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆