4

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?

1 Answer 1

9

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

Sign up to request clarification or add additional context in comments.

5 Comments

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.
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.
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.
I guess you need "results: 'asis' if you use Quarto, i. e. a colophon instead of an equal sign
how about when working in a .qmd in python? I think the cat() will not work there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.