首页 > 解决方案 > 如何在 R 中迭代以同时保存多个文件并避免“absolute_path(target) 中的错误:'x' 必须是单个字符串”?

问题描述

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

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

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

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

标签: rfor-loopprintingiterationofficer

解决方案


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

推荐阅读