0

Is there a way to insert tables from a list of data frames into seperate word documents? I am building off of the previous help I received in a previous post: R: Automate generating word documents based on dataframe

Here is an example data frame:

DF <- data.frame(Company=c('Alpha','Alpha', 'Beta', 'Beta', 'Gamma'),
              Name= c('John Doe', 'John Doe','Jane Doe','Jane Doe', 'Jack Smith'),
              Address=c('123 Main St','123 Main St', '234 Maple St','234 Maple St', '456 30th St' ),
              Address_Line_2=c('Suite A','Suite A', '', '', 'Suite B'),
              City_State_Zip=c('Los Angeles, CA 12345', 'Los Angeles, CA 12345','New York City, NY 23456','New York City, NY 23456', 'Chicago, IL 3456'),
              Charge_Date = c('01-01-2023', '01-31-2023','02-01-2023',  '02-01-2023', '03-01-2023'),
              Total_Charge = c(100,150,100,200,300),
              SKU_Number = c('#1234','#2345', '#5678', '#4567', '#5678'))

I then generate a list of dataframes based on Company name selecting only a few columns and create a total row for each new dataframe:

    List <- DF  %>% 
  dplyr::select(Company,Charge_Date,SKU_Number,Total_Charge,)%>%
  base::split(., .$Company)%>%
  janitor::adorn_totals(.,"row")

With that part finished, I focus on generating letters to the individual companies

DF2 <- DF  %>% 
.[!duplicated (.[c('Company')]), ] 
# Function to replace text
replace_all_text <- function(DF2, replacement_list, doc_path = "C:\\Mock_Path\\Letter.docx") {
  document <- read_docx(doc_path)
  Reduce(
    function(doc, repl)
    body_replace_all_text(doc, repl["pattern"], DF2[[repl["repl"]]], ignore.case = TRUE),
    replacement_list,
    init = document
  )
}

# Create list of replacements
replacement_list <- list(
  c(pattern = "Today’s Date", repl = "date"),
  c(pattern = "Company Name", repl = "Company"),
  c(pattern = "First Name/Last Name", repl = "Name"),
  c(pattern = "Company Address", repl = "Address"),
  c(pattern = "Address Line 2", repl = "Address_Line_2"),
  c(pattern = "City, State Zip", repl = "City_State_Zip")
)

# Add date to the data.frame
DF2$date <- format(Sys.Date(), "%B %d, %Y")
df_split <- split(DF2, seq_len(nrow(DF2)))


# Apply the function to each document
replaced_docs_list <- lapply(df_split, function(DF2) replace_all_text(DF2, replacement_list)) %>%
  setNames(DF2$Company)

purrr::iwalk(replaced_docs_list, function(doc, company) {
  outfile <- paste0(company, ".docx")
  message("Saving doc: ", outfile)
  print(doc, target = outfile)
})



What I am trying to do is pull from the list of dataframes created earlier and insert the dataframe as a table into the second page of each letter based on the company. I believe the key components are "body_add_table" and "body_add_break" from the officer package but I am struggling to come up with a function that can work down the list of data frames. I have attached examples of what I am hoping each page to look like. Any help or direction would be greatly appreciated. Thanks!

enter image description here enter image description here

Mark
  • 7,785
  • 2
  • 14
  • 34
  • 1
    I believe you can use `body_add_table` and `body_add_break` to add a new table and a new page respectively – Mark Aug 17 '23 at 06:53
  • 1
    Two things, neither of which are directly responsive to your question: 1) your name, "CuRious Code" is awesome!; and 2). I know you're asking how to do this with a Word.doc, but if you haven't looked at RMarkdown's capabilities, I think you could make an equivalent set of mailers possibly easier and better looking -- at least worth your time to consider if you're not aware. – ScottyJ Aug 17 '23 at 14:46

0 Answers0