如何在 R 中迭代以同时保存多个文件并避免 "Error in absolute_path(target) : 'x' must be a single character string"?

How to iterate in R to save multiple files simultaneously and avoid "Error in absolute_path(target) : 'x' must be a single character string"?

我正在使用 officer 包,我想为我的数据中的每一行制作一张幻灯片的 PowerPoint 文档。我的 for 循环对文件名有效 except。每当我尝试提供多个文件名时,都会出现此错误:

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

只有当我将所有内容都转到一个文件路径时,我才能让我的 for 循环工作——然后只会覆盖所有内容。

这是我的数据——注意,要复制,您需要添加要保存文档的文件路径:

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

这是我的代码,它不起作用并抛出上述错误:

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

我确定问题出在我调整的那一行,因为下面的代码在我只允许 1 个文件路径时有效。这是不可取的,因为现在所有信息都挤在一张幻灯片上:

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

这是运行但将所有内容都放在一张幻灯片上的代码:

在我的数据集中,我制作了一个列,其中包含每个文档的文件路径(例如,它在文件名中添加了人名)。

在第一个函数中,file_pathway 正在获取 'file_pathway' 列的完整行,导致错误。相反,它应该是单一路径,即将代码从 file_pathway = data$file_pathway 更改为 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)
}