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

Posted

技术标签:

【中文标题】如何在 R 中迭代以同时保存多个文件并避免“absolute_path(target) 中的错误:\'x\' 必须是单个字符串”?【英文标题】:How to iterate in R to save multiple files simultaneously and avoid "Error in absolute_path(target) : 'x' must be a single character string"?如何在 R 中迭代以同时保存多个文件并避免“absolute_path(target) 中的错误:'x' 必须是单个字符串”? 【发布时间】:2021-11-28 18:37:18 【问题描述】:

我正在使用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)

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

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

【问题讨论】:

【参考方案1】:

在第一个函数中,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)

【讨论】:

以上是关于如何在 R 中迭代以同时保存多个文件并避免“absolute_path(target) 中的错误:'x' 必须是单个字符串”?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Typescript 中保持代码 DRY 的同时解析多个 Json 文件?

如何读取具有动态名称的文件,同时避免在 R 中进行硬编码?

在 R 中保存旋转图像 - 避免背景并保持大小

如何在R中的循环中运行(更改)JavaScript文件?

迭代多个数据帧并执行数学函数保存输出

如何以最大输出大小压缩多个文件,同时将文件对保持在一起