使用循环在rmarkdown中生成文本部分

时间:2021-05-02 12:06:06

I need to produce a report that is composed of several sections, all sections look similar, with only some differences in data. The number of sections is also dependent on the data. What I ultimately want to have is something like this:

我需要生成一个由几个部分组成的报告,所有部分看起来都很相似,只有一些数据差异。部分的数量也取决于数据。我最终想拥有的是这样的:

```{r}
  section_names = c("A","B","C")
  section_data = c(13,14,16)
```

# some looping mechanism here with variable i

This is section `r section_names[i]`

This section's data is `r section_data[i]`

#more things go here for the section

#end of loop should go here

The result should be a single html/document with all the sections one after the other.

结果应该是单个html /文档,所有部分一个接一个。

Can you point me to a way for producing such an Rmd file with the loop?

你能指点我用循环生成这样一个Rmd文件的方法吗?

Ideally I would have hoped to see something like in PHP:

理想情况下,我希望看到像PHP这样的东西:

<$php for(i=0;i<10;i++) { ?>
   ## some html template + code chunks here
<$php } ?>

2 个解决方案

#1


8  

This question is similar to that one, although it is LateX/RNW based. Besides, this answer demonstrates how to generate a rmarkdown document dynamically. However, neither of the questions is a exact duplicate of this one.

这个问题类似于那个问题,尽管它基于LateX / RNW。此外,这个答案演示了如何动态生成rmarkdown文档。但是,这两个问题都不是这个问题的完全重复。

Basically, there are two mental steps to take:

基本上,有两个心理步骤:

  • Figure out the markdown markup needed per section. This could be something along the lines of

    找出每个部分所需的降价标记。这可能是一些事情

    ## This is section <section_name>
    Section data is `<section_data>`.
    Additional section text is: <section_text>.
    
  • Write R code that generates this markup, replacing the placeholders with the appropriate values.

    编写生成此标记的R代码,用适当的值替换占位符。

For step 2, using sprintf is a natural candidate to combine static and dynamic text. Don't forget to use the chunk options results = "asis" to prevent knitr from adding formatting to your output and use cat (instead of print) to prevent R from adding additional stuff like quotes and element numbers.

对于步骤2,使用sprintf是组合静态和动态文本的自然候选者。不要忘记使用块选项results =“asis”来防止knitr在输出中添加格式并使用cat(而不是print)来防止R添加其他内容,如引号和元素编号。

I changed the input data structure a little bit for the sake of clarity (using a data.frame instead of independent vectors section_names and section_data).

为了清楚起见,我稍微改变了输入数据结构(使用data.frame而不是独立的向量section_names和section_data)。

```{r echo = FALSE, results = "asis"}
input <- data.frame(
  name = LETTERS[1:4],
  data = runif(n = 4),
  text = replicate(4, paste(sample(x = LETTERS, size = 100, replace = TRUE), collapse = "")),
  stringsAsFactors = FALSE)

template <- "## This is section %s
Section data is `%0.2f`.
Additional section text is: %s.

" # dont't forget the newline

for (i in seq(nrow(input))) {
  current <- input[i, ]
  cat(sprintf(template, current$name, current$data, current$text))
}
```

Output:

This is section A

Section data is 0.83. Additional section text is: PUFTZQFCYJFNENMAAUDPTWIKLBSVKWMJWODFHSPRJRROTVDGNEROBVQPLLMVNPOUUHGVGRPMKAOAOMVYXKMGMUHNYWZGPRAWPYLU.

截面数据为0.83。附加部分文本是:PUFTZQFCYJFNENMAAUDPTWIKLBSVKWMJWODFHSPRJRROTVDGNEROBVQPLLMVNPOUUHGVGRPMKAOAOMVYXKMGMUHNYWZGPRAWPYLU。

This is section B

Section data is 0.49. Additional section text is: PFTYCGFSGSMAYSSCZXWLNLDOQEBJYEVSJIYDJPEPSWQBNWJVRUKBTYIUSTOICFKJFEJCWCAYBCQSRTXUDEQLLXCZNPUKNLJIQJXE.

截面数据为0.49。附加部分文本是:PFTYCGFSGSMAYSSCZXWLNLDOQEBJYEVSJIYDJPEPSWQBNWJVRUKBTYIUSTOICFKJFEJCWCAYBCQSRTXUDEQLLXCZNPUKNLJIQJXE。

This is section C

Section data is 0.58. Additional section text is: FCJDDDMNLBUSJMCZVSBPYWCKSFJEARBXXFPAGBTKCWKHPEDGYWYTNGLVGQGJAFZRUMNSDCHKTTMGRFNSUZKFLOUGNWHUBNLVMGDB.

截面数据为0.58。附加部分文本是:FCJDDDMNLBUSJMCZVSBPYWCKSFJEARBXXFPAGBTKCWKHPEDGYWYTNGLVGQGJAFZRUMNSDCHKTTMGRFNSUZKFLOUGNWHUBNLVMGDB。

This is section D

Section data is 0.52. Additional section text is: YQIXHABFVQUAAYZNWTZXJDISSLTZJJAZOLJMJSXEENFTUOFOTYKDNNUMFDXLJSWZEVDLCLSYCTSMEXFLBVQYRTBEVZLCTEBPUGTT.

截面数据为0.52。附加部分文本是:YQIXHABFVQUAAYZNWTZXJDISSLTZJJAZOLJMJSXEENFTUOFOTYKDNNUMFDXLJSWZEVDLCLSYCTSMEXFLBVQYRTBEVZLCTEBPUGTT。

#2


3  

Just sharing the approach I've used eventually.

只是分享我最终使用的方法。

I wrote a markdown file for the section. prepared the data for each section in the master document, and looped over all the sections I needed, each time calling to knit_child() with the section Rmd.

我为该部分写了一个降价文件。为主文档中的每个部分准备好数据,并在每次使用Rmd部分调用knit_child()时循环遍历所需的所有部分。

#1


8  

This question is similar to that one, although it is LateX/RNW based. Besides, this answer demonstrates how to generate a rmarkdown document dynamically. However, neither of the questions is a exact duplicate of this one.

这个问题类似于那个问题,尽管它基于LateX / RNW。此外,这个答案演示了如何动态生成rmarkdown文档。但是,这两个问题都不是这个问题的完全重复。

Basically, there are two mental steps to take:

基本上,有两个心理步骤:

  • Figure out the markdown markup needed per section. This could be something along the lines of

    找出每个部分所需的降价标记。这可能是一些事情

    ## This is section <section_name>
    Section data is `<section_data>`.
    Additional section text is: <section_text>.
    
  • Write R code that generates this markup, replacing the placeholders with the appropriate values.

    编写生成此标记的R代码,用适当的值替换占位符。

For step 2, using sprintf is a natural candidate to combine static and dynamic text. Don't forget to use the chunk options results = "asis" to prevent knitr from adding formatting to your output and use cat (instead of print) to prevent R from adding additional stuff like quotes and element numbers.

对于步骤2,使用sprintf是组合静态和动态文本的自然候选者。不要忘记使用块选项results =“asis”来防止knitr在输出中添加格式并使用cat(而不是print)来防止R添加其他内容,如引号和元素编号。

I changed the input data structure a little bit for the sake of clarity (using a data.frame instead of independent vectors section_names and section_data).

为了清楚起见,我稍微改变了输入数据结构(使用data.frame而不是独立的向量section_names和section_data)。

```{r echo = FALSE, results = "asis"}
input <- data.frame(
  name = LETTERS[1:4],
  data = runif(n = 4),
  text = replicate(4, paste(sample(x = LETTERS, size = 100, replace = TRUE), collapse = "")),
  stringsAsFactors = FALSE)

template <- "## This is section %s
Section data is `%0.2f`.
Additional section text is: %s.

" # dont't forget the newline

for (i in seq(nrow(input))) {
  current <- input[i, ]
  cat(sprintf(template, current$name, current$data, current$text))
}
```

Output:

This is section A

Section data is 0.83. Additional section text is: PUFTZQFCYJFNENMAAUDPTWIKLBSVKWMJWODFHSPRJRROTVDGNEROBVQPLLMVNPOUUHGVGRPMKAOAOMVYXKMGMUHNYWZGPRAWPYLU.

截面数据为0.83。附加部分文本是:PUFTZQFCYJFNENMAAUDPTWIKLBSVKWMJWODFHSPRJRROTVDGNEROBVQPLLMVNPOUUHGVGRPMKAOAOMVYXKMGMUHNYWZGPRAWPYLU。

This is section B

Section data is 0.49. Additional section text is: PFTYCGFSGSMAYSSCZXWLNLDOQEBJYEVSJIYDJPEPSWQBNWJVRUKBTYIUSTOICFKJFEJCWCAYBCQSRTXUDEQLLXCZNPUKNLJIQJXE.

截面数据为0.49。附加部分文本是:PFTYCGFSGSMAYSSCZXWLNLDOQEBJYEVSJIYDJPEPSWQBNWJVRUKBTYIUSTOICFKJFEJCWCAYBCQSRTXUDEQLLXCZNPUKNLJIQJXE。

This is section C

Section data is 0.58. Additional section text is: FCJDDDMNLBUSJMCZVSBPYWCKSFJEARBXXFPAGBTKCWKHPEDGYWYTNGLVGQGJAFZRUMNSDCHKTTMGRFNSUZKFLOUGNWHUBNLVMGDB.

截面数据为0.58。附加部分文本是:FCJDDDMNLBUSJMCZVSBPYWCKSFJEARBXXFPAGBTKCWKHPEDGYWYTNGLVGQGJAFZRUMNSDCHKTTMGRFNSUZKFLOUGNWHUBNLVMGDB。

This is section D

Section data is 0.52. Additional section text is: YQIXHABFVQUAAYZNWTZXJDISSLTZJJAZOLJMJSXEENFTUOFOTYKDNNUMFDXLJSWZEVDLCLSYCTSMEXFLBVQYRTBEVZLCTEBPUGTT.

截面数据为0.52。附加部分文本是:YQIXHABFVQUAAYZNWTZXJDISSLTZJJAZOLJMJSXEENFTUOFOTYKDNNUMFDXLJSWZEVDLCLSYCTSMEXFLBVQYRTBEVZLCTEBPUGTT。

#2


3  

Just sharing the approach I've used eventually.

只是分享我最终使用的方法。

I wrote a markdown file for the section. prepared the data for each section in the master document, and looped over all the sections I needed, each time calling to knit_child() with the section Rmd.

我为该部分写了一个降价文件。为主文档中的每个部分准备好数据,并在每次使用Rmd部分调用knit_child()时循环遍历所需的所有部分。