Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 } ?>
like image 528
amit Avatar asked Apr 17 '16 09:04

amit


2 Answers

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.

like image 160
CL. Avatar answered Nov 16 '22 20:11

CL.


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.

like image 38
amit Avatar answered Nov 16 '22 20:11

amit