abreums
abreums

Reputation: 196

quartopub Rmarkdown docx output

What function could help me to output data content to a Quarto-pub Docx output document?

I'm trying...

The yaml is:

---
title: "Docx Quarto output"
format:
  docx:
    toc: true
    toc-depth: 2
    number-sections: true
    number-depth: 3
    highlight-style: github
---

And the code:

```{r}
library(tidyverse)
text_data <- tribble(
  ~type,       ~name,        ~color,
  "fruit",     "apple",      "red",
  "vegetable", "cumcumber",  "green",
  "fruit",     "banana",     "yellow",
  "grain",     "rice",       "white"
)

text_data %>%
  split(.$type) %>%  
  map(~ cat('\n\n ##', .$name, '\n###', .$color))
```

What I would like to get is a Rmarkdown like:

# fruit

## apple

### red

## banana

### yellow

# vegetable

## cumcumber

### green

# grain

## rice

### white

Upvotes: 1

Views: 87

Answers (1)

Julian
Julian

Reputation: 9250

You could try the following:

```{r}
#| results: asis
    df <- text_data %>%
      split(text_data$type) |>
      map_dfr(~ .x |>
                # little hack to avoid printing # fruit twice
        mutate(string = ifelse(row_number() == 1, paste0(
          "\n\n# ", type,
          "\n\n## ", name,
          "\n\n### ", color
        ),
        paste0(
          "\n\n## ", name,
          "\n\n### ", color
        )
        )) |>
        select(string))
    
    cat(df$string)
```

Result:

enter image description here

Upvotes: 2

Related Questions