R officer - Nest Dataframe Into Grouped List And Export Tables to Word With Group Headers
Asked Answered
V

1

1

I read a similar question that helped me get to the point I'm at now, but am struggling with the next step. I have a dataframe similar to below -

Product = c("Apple", "Apple", "Banana", "Banana", "Banana", "Carrot", 
            "Carrot") 
Category = c(1, 2, 1, 2, 3, 1, 2) 
Slope = c(2.988, 2.311, 2.181, 6.387, 2.615, 7.936, 3.267) 
df = data.frame(Product, Category, Slope) 

My objective is to have a Word report with a table for each product. To do this, I create a list with the data and flextables, as below -

library(tidyverse)
library(flextable)
library(officer)
test <- df %>%
          group_by(Product) %>%
          nest() %>%
          mutate(data$Product)
          mutate(ftables = map(data, flextable)) %>%
          mutate(ftables = map(ftables, ~ bg(.,
                               bg = '#bfbfbf',
                               part = 'header')))

I can then pass this into word like this -

my_doc <- read_docx()
for (i in seq_along(test$ftables)) {
     body_add_flextable(my_doc, test$ftables[[i]]) %>%
     body_add_par(value = "")
}

The output is great, but unfortunately I lose any method of identifying which product each table belongs to.

I would either like to have the Product name as a title or within the tables. Doesn't matter which, but I can't figure out how to do either.

I've tried to look for ways to reintroduce the grouped field post-nest and also tried doubling up on my for loop for the headers, but ended up just repeating all the tables under each header -

for (i in seq_along(test$ProductLine)) {
  my_doc <- body_add_par(my_doc, value = test$ProductLine[[i]], style = 
                         'heading 1') %>%
  body_add_par(value = "")
  for (j in seq_along(test$ftables)) {
    my_doc <- body_add_flextable(my_doc, test$ftables[[i]])
  }
}

Does anybody know a way that I can accomplish this?

Voroshilov answered 28/5, 2018 at 14:36 Comment(0)
O
2

in your second try of a loop, don't add the first loop, just add the command to your chain...

Product = c("Apple", "Apple", "Banana", "Banana", "Banana", "Carrot", 
            "Carrot") 
Category = c(1, 2, 1, 2, 3, 1, 2) 
Slope = c(2.988, 2.311, 2.181, 6.387, 2.615, 7.936, 3.267) 
df = data.frame(Product, Category, Slope) 

library(tidyverse)
library(flextable)
library(officer)

test <- 
  df %>%
  group_by(Product) %>%
  nest() %>%
  mutate(ftables = map(data, flextable)) %>%
  mutate(ftables = map(ftables, ~ bg(.,
                                     bg = '#bfbfbf',
                                     part = 'header')))

my_doc <- read_docx()

for (i in seq_along(test$Product)) {
  my_doc <- 
    my_doc %>% 
    body_add_par(value = test$Product[[i]], style = 'heading 1') %>% 
    body_add_par(value = "") %>% 
    body_add_flextable(test$ftables[[i]]) %>% 
    body_add_par(value = "")
}

print(my_doc, target = "my_doc.docx")
Oestriol answered 28/5, 2018 at 16:0 Comment(1)
That works! But I tried a similar method that returned a 'body_add_xml' error. The only difference is that I didn't start the pipe with just 'my_doc'. I called it out on each line. sigh Its the small things. Well, thank you very much CJVoroshilov

© 2022 - 2025 — McMap. All rights reserved.