3

I want to generate with R / Quarto a Word document (docx) in which, inside an item, I have sub-items created by looping. I’m using R version 4.2.2, R Studio 2022.12.0 Build 353, Quarto 1.2.280 and Ubuntu 20.04.

For example, after an introductory text given an overview on a topic, I want to produce sub-items with details of each item.

Without a looping the code would be:

---
title: "Data by County"
format:
  docx:
    number-sections: true
---

```{r}
#| echo=FALSE,
#| include=FALSE

dat.county <- data.frame(
  county = LETTERS[1:5],
  pop_num = round(runif(5,100,500)),
  gdp = runif(5,1000,5000)
)
```

# Identifying county characteristics

A total of `r nrow(dat.county)` counties, with a total population of `r sum(dat.county$pop_num)` thousand people were characterized as follows:

## County `r dat.county[1,1]`

County `r dat.county[1,1]` has a population of `r dat.county[1,2]` thousand people with a real gross domestic product of `r dat.county[1,3]`.

## County `r dat.county[2,1]`

County `r dat.county[2,1]` has a population of `r dat.county[2,2]` thousand people with a real gross domestic product of `r dat.county[2,3]`.

and so on.

I tried to insert a looping like the one below, but it didn't work. "##" are not recognized as a header. Also I had problems with line breaks ans paragraphs. Finally, the code using cat is not as elegant as the text above.

```{r}
#| echo=FALSE

  for (i in 1:nrow(dat.county)) {
  
  cat("## County",dat.county[i,1],"\n")
  
  cat("County ",dat.county[i,1]," has a population of ",dat.county[i,2]," thousand people with a real gross domestic product of ",dat.county[i,3],"\n")
  }
```

My question is, how can I generate some thing like

## County `r dat.county[i,1]`

County `r dat.county[i,1]` has a population of `r dat.county[i,2]` thousand people with a real gross domestic product of `r dat.county[i,3]`

inside a looping?

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
Antonio
  • 563
  • 1
  • 5
  • 12

1 Answers1

5

You just need to add the chunk option results='asis' and use a double newline when calling cat():

---
title: "Data by County"
format:
  docx:
    number-sections: true
---

```{r}
#| echo=FALSE,
#| include=FALSE

dat.county <- data.frame(
  county = LETTERS[1:5],
  pop_num = round(runif(5,100,500)),
  gdp = runif(5,1000,5000)
)
```

```{r}
#| echo=FALSE,
#| results='asis'

  for (i in 1:nrow(dat.county)) {
  
  cat("## County",dat.county[i,1],"\n\n")
  
  cat("County ",dat.county[i,1]," has a population of ",dat.county[i,2]," thousand people with a real gross domestic product of ",dat.county[i,3],"\n\n")
  }
```

Gives:

enter image description here

Ritchie Sacramento
  • 29,890
  • 4
  • 48
  • 56
  • Thanks! But I would still like to know if there is a way to use the form of the first example, without cat(), inside a looping. I ask this because the real text to be generated is much longer and the code of the first example, where I didn't even try to do the looping, is much cleaner. – Antonio Jan 03 '23 at 01:56
  • 1
    I don't know of a way to achieve what you want without using `cat()` as the text needs to be printed and interpreted. You could avoid a loop in this example though (by using `sprintf()`) which you might find more appealing. Can edit in an example if you like. – Ritchie Sacramento Jan 03 '23 at 02:54
  • Thanks again, no need to edit your answer. It works perfectly. As the code has to be interpreted, this way seems to be the best one. – Antonio Jan 03 '23 at 12:08
  • I guess you need "results: 'asis' if you use Quarto, i. e. a colophon instead of an equal sign – Irazall May 31 '23 at 11:36
  • how about when working in a .qmd in python? I think the cat() will not work there – Amun_Re Jun 12 '23 at 21:19