R总监-将数据框嵌套到分组列表中,并将表导出到具有组头的Word中 [英] R officer - Nest Dataframe Into Grouped List And Export Tables to Word With Group Headers

查看:126
本文介绍了R总监-将数据框嵌套到分组列表中,并将表导出到具有组头的Word中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读了一个类似的问题,可以帮助我达到现在的目的,但是正在为下一步做着努力.我有一个类似于下面的数据框-

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) 

我的目标是为每个产品创建一个带表格的Word报告.为此,我创建了一个包含数据和弹性表的列表,如下所示-

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.

我试图寻找方法在嵌套后重新引入分组字段,还尝试在头的for循环中加倍,但最终只是重复每个头下的所有表-

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?

推荐答案

在您的第二次循环尝试中,不要添加第一个循环,只需将命令添加到您的链中即可.

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")

这篇关于R总监-将数据框嵌套到分组列表中,并将表导出到具有组头的Word中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆