RMarkdown:一种从许多 mini-RMarkdown 文档中撰写报告的方法?
RMarkdown: a way to compose a report from many mini-RMarkdown documents?
将小标题视为输入:
in_data <- tribble(
~ style, ~input1, ~input2, ~text_input
'style_1', 5, NA, 'first result',
'style_2', 4, 6, 'fun',
'style_1', 2, NA, 'other result')
我想做以下事情:
- 有不同的模式,例如
style_1
和style_2
那种
style_1.Rmd
---
params:
input1: NA
text_input: ''
---
`r params$text_input` is of the value `r params$input1`
style_2.Rmd
---
params:
input1: NA
input2: NA
text_input: ''
---
```{r}
plot(params$input1, params$input2, main=params$text_input)
```
- 以某种方式
in_data %>% pmap(parse_and_knit)
文档,其中每一行都与其相应的样式和参数编织在一起,最后组合成一个文档,这可能需要额外的 header。
有 heddlr
https://github.com/mikemahoney218/heddlr 做类似的事情。但它似乎依赖于为每个“模式”(这是我的“风格”)替换一个参数。我可以将该参数设为行号,然后使用该信息提取数据;将整行作为 params
输入 pmap
样式会更好。
这可能吗?我可以 knit
每一个单独的部分,但我最终需要将它组合成一份报告,最好不要使用我自己的 DOM 魔法。
感谢@Jon Spring,以下内容非常接近我想要的。
- R 脚本将数据框作为
params
项传递给 rmarkdown::render
- 遍历各行的逻辑在报告模板中(并非如我最初预期的那样,在触发报告生成的 R 脚本中)。
- 行数据放入环境中,用
envir
传入。我还可以将列变量直接分配给 envir
,这样就可以在没有 data$
的情况下直接访问它们,但我还不确定是否需要那样。
- 注意:可能
pwalk(..., ~cat(...))
比 cat(pmap_chr(...) %>% str_flatten())
好。
在脚本中generate_report.R
:
library(knitr)
library(tidyverse)
in_data <- tribble(
~ style, ~input1, ~input2, ~text_input,
'style1', 5, NA, 'first result',
'style2', 4, 6, 'fun',
'style1', 2, NA, 'other result')
rmarkdown::render("rmdreport.Rmd", params = list(data = in_data))
在报告模板中report.Rmd
---
title: "report title"
output: html_document
params:
data: NA
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='asis')
```
# Report
This is the intro to the report. What follows is one block per row in `params$data`.
```{r}
in_data <- params$data
cat(
pmap_chr(in_data, function(...) {
data <- list(...)
envir = new.env()
envir$data <- data
knitr::knit_child(glue("{data$style}.rmd"), envir = envir, quiet = TRUE)
}) %>% str_flatten())
```
个人templates/styles:
style1.Rmd
## Section on the topic `r data$text_input`
`r data$text_input` is of the value `r data$input1`
style2.Rmd
## Section with a plot
```{r}
plot(data$input1, data$input2, main=data$text_input)
res <- data$input1 * data$input2
cat(glue("{data$input1} * {data$input2} = {res}"))
```
将小标题视为输入:
in_data <- tribble(
~ style, ~input1, ~input2, ~text_input
'style_1', 5, NA, 'first result',
'style_2', 4, 6, 'fun',
'style_1', 2, NA, 'other result')
我想做以下事情:
- 有不同的模式,例如
style_1
和style_2
那种
style_1.Rmd
---
params:
input1: NA
text_input: ''
---
`r params$text_input` is of the value `r params$input1`
style_2.Rmd
---
params:
input1: NA
input2: NA
text_input: ''
---
```{r}
plot(params$input1, params$input2, main=params$text_input)
```
- 以某种方式
in_data %>% pmap(parse_and_knit)
文档,其中每一行都与其相应的样式和参数编织在一起,最后组合成一个文档,这可能需要额外的 header。
有 heddlr
https://github.com/mikemahoney218/heddlr 做类似的事情。但它似乎依赖于为每个“模式”(这是我的“风格”)替换一个参数。我可以将该参数设为行号,然后使用该信息提取数据;将整行作为 params
输入 pmap
样式会更好。
这可能吗?我可以 knit
每一个单独的部分,但我最终需要将它组合成一份报告,最好不要使用我自己的 DOM 魔法。
感谢@Jon Spring,以下内容非常接近我想要的。
- R 脚本将数据框作为
params
项传递给rmarkdown::render
- 遍历各行的逻辑在报告模板中(并非如我最初预期的那样,在触发报告生成的 R 脚本中)。
- 行数据放入环境中,用
envir
传入。我还可以将列变量直接分配给envir
,这样就可以在没有data$
的情况下直接访问它们,但我还不确定是否需要那样。 - 注意:可能
pwalk(..., ~cat(...))
比cat(pmap_chr(...) %>% str_flatten())
好。
在脚本中generate_report.R
:
library(knitr)
library(tidyverse)
in_data <- tribble(
~ style, ~input1, ~input2, ~text_input,
'style1', 5, NA, 'first result',
'style2', 4, 6, 'fun',
'style1', 2, NA, 'other result')
rmarkdown::render("rmdreport.Rmd", params = list(data = in_data))
在报告模板中report.Rmd
---
title: "report title"
output: html_document
params:
data: NA
---
```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE, results='asis')
```
# Report
This is the intro to the report. What follows is one block per row in `params$data`.
```{r}
in_data <- params$data
cat(
pmap_chr(in_data, function(...) {
data <- list(...)
envir = new.env()
envir$data <- data
knitr::knit_child(glue("{data$style}.rmd"), envir = envir, quiet = TRUE)
}) %>% str_flatten())
```
个人templates/styles:
style1.Rmd
## Section on the topic `r data$text_input`
`r data$text_input` is of the value `r data$input1`
style2.Rmd
## Section with a plot
```{r}
plot(data$input1, data$input2, main=data$text_input)
res <- data$input1 * data$input2
cat(glue("{data$input1} * {data$input2} = {res}"))
```