Home > OS >  How to iterate in R to save multiple files simultaneously and avoid "Error in absolute_path(tar
How to iterate in R to save multiple files simultaneously and avoid "Error in absolute_path(tar

Time:10-09

I'm using the officer package, and I want to make a one-slide PowerPoint document for each row in my data. My for loop works except for the file name. Whenever I try to give multiple file names, I get this error:

Error in absolute_path(target) : 'x' must be a single character string

I can only get my for loop to work when I have everything go to one file path--which then just overwrites everything.

Here's my data--note, to replicate, you'll need to add in the file path you'll want the documents to save:

library(dplyr)
my_data <- tibble(first_name = c("Justin", "Corey", "Sibley"),
                  full_name = c("Justin S", "Corey X", "Sibley X"),
                  num_cakes = c("6 cakes", "7 cakes", "0 cakes")) %>%
  mutate(file_pathway = paste0("edit/with/your/file/path/", full_name, ".pptx")) #edit here

Here's my code that doesn't work and throws the above error:

library(dplyr)
library(officer)

#Custom function
dessert_slide <- function(file_pathway, data, first_name, num_cakes, left = 0.5, top = 1.2, width = 5.5, height = 3){
  out <- read_pptx() %>%
  add_slide(layout = "Two Content", master = "Office Theme")

subtitle <- paste0(data$first_name, ", let's see what desserts you ate!")
subtitle_properties <- fp_text(color = "#63666A", font.size = 20, bold = FALSE, font.family = "Arial")
normal_properties <- fp_text(color = "#63666A", font.size = 20, bold = TRUE, font.family = "Arial")
cake_properties <- fp_text(color = "#C8102E", font.size = 20, bold = TRUE, font.family = "Arial")
formatted_subtitle <- ftext(subtitle, subtitle_properties)

centered <- fp_par(text.align = "center")


out %>% 
  ph_with(value= fpar(formatted_subtitle), location = officer::ph_location(
    width = 12, height = .5, left = .7, top = 1)) %>%
  ph_with(value= fpar(ftext("You ate ", normal_properties), ftext(data$num_cakes, cake_properties), ftext(".", normal_properties), fp_p = centered), alignment = "c", location = officer::ph_location(
    width = 2.36, height = .67, left = .78, top = 4.43)) %>%
  base::print(target = data$file_pathway) #The issue is here
}

#For loop the runs the custom function

for (row in 1:nrow(my_data)) {
  dessert_slide(data = my_data, file_pathway = data$file_pathway, first_name = first_name,
              num_cakes = num_cakes)
}

I'm sure the issue is on the line I adjust because this below code works when I only allow for 1 filepathway. This is undesirable because now all the information is jammed on one slide:

library(dplyr)
library(officer)

#setting just one file path which overwrites everything :(
just_one_path <- paste0("edit/to/have/your/filepath/, "just one file.pptx")


# Custom function
dessert_slide <- function(file_pathway, data, first_name, num_cakes, left = 0.5, top = 1.2, width = 5.5, height = 3){
  out <- read_pptx() %>%
  add_slide(layout = "Two Content", master = "Office Theme")

subtitle <- paste0(data$first_name, ", let's see what desserts you ate!")
subtitle_properties <- fp_text(color = "#63666A", font.size = 20, bold = FALSE, font.family = "Arial")
normal_properties <- fp_text(color = "#63666A", font.size = 20, bold = TRUE, font.family = "Arial")
cake_properties <- fp_text(color = "#C8102E", font.size = 20, bold = TRUE, font.family = "Arial")
formatted_subtitle <- ftext(subtitle, subtitle_properties)

centered <- fp_par(text.align = "center")


out %>% 
  ph_with(value= fpar(formatted_subtitle), location = officer::ph_location(
    width = 12, height = .5, left = .7, top = 1)) %>%
  ph_with(value= fpar(ftext("You ate ", normal_properties), ftext(data$num_cakes, cake_properties), ftext(".", normal_properties), fp_p = centered), alignment = "c", location = officer::ph_location(
    width = 2.36, height = .67, left = .78, top = 4.43)) %>%
  base::print(target = file_pathway)
}

#For loop running function
for (row in 1:nrow(my_data)) {
  dessert_slide(data = my_data, file_pathway = just_one_path, first_name = first_name,
              num_cakes = num_cakes)
}

Here's the code that runs but puts everything on one slide:

In my dataset, I made a column that contains the file path for each document (e.g., it adds the person's name in the file name).

CodePudding user response:

In the first function, the file_pathway is taking the full rows of 'file_pathway' column resulting in the error. Instead it should be a single path i.e. change the code from file_pathway = data$file_pathway to file_pathway = data$file_pathway[row]

for (row in 1:nrow(my_data)) {
  dessert_slide(data = my_data, file_pathway = data$file_pathway[row], first_name = first_name,
              num_cakes = num_cakes)
}
  • Related