Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
416 views
in Technique[技术] by (71.8m points)

r - Use loop to generate section of text in rmarkdown

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.

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

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

<$php for(i=0;i<10;i++) { ?>
   ## some html template + code chunks here
<$php } ?>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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.

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.

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

```{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.

This is section B

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

This is section C

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

This is section D

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...