代码之家  ›  专栏  ›  技术社区  ›  J.Sabree

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

  •  1
  • J.Sabree  · 技术社区  · 4 年前

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

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

    1 回复  |  直到 4 年前
        1
  •  2
  •   akrun    4 年前

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