如何使用 officer 从 Shiny 中的 downloadHandler() 到 temp 目录以 purrr::map() 样式编写多个 docx 文件?

How to write multiple docx files in purrr::map() style with officer from within downloadHandler() in Shiny to temp directory?

我正在尝试从 downloadHandler() 中编写多个 docx 文件。当 purrr::map() 到达映射函数的一部分,在该部分中,简历以唯一名称写入指定的临时目录,然后被压缩并返回到 downloadHandler() 的文件参数。有一个错误指出指定的目录不存在。单个简历的示例目录是 /var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx

我尝试了很多方法将文件写入不同的位置或更改结束目录。我在 R 项目中工作。我不断收到错误消息:"Warning: Error in : directory of /var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx does not exist."

resume_temp <- file.path(tempdir(), "resumes")
make_my_resume <- function(my_id) {


  # the empty Word doc that has an applied resume style
  template <- read_docx(here::here("r_scripts", "modern_basic_resume_empty.docx"))


  name_for_file <- str_to_lower(paste(my$first_name, my$last_name, sep = "_"))


  #-----------------------build resume in Word------------------------------------

  word_resume <- template %>%
    cursor_begin() %>% 
    body_remove() %>%
    body_add_par(paste(my$first_name, my$last_name), style = "Title") %>%

    body_end_section_continuous() %>%
    body_add_par(my$address, style = "Contact Info") %>%
    body_add_par(my$phone, style = "Contact Info") %>%
    body_add_par(my$email, style = "Contact Info") %>%

    body_end_section_continuous() %>%
    body_add_par(" ", style = "Title") %>%
    body_add_par(" ", style = "Normal") %>%

    body_end_section_continuous() %>%
    body_add_par("Experience", style = "heading 1") %>%
    body_add_table(my_experience, style = "Plain Table 5") %>%

    body_end_section_continuous() %>%
    body_add_par("Deployments", style = "heading 1") %>%
    body_add_table(my_deployments, style = "Plain Table 5") %>%

    body_end_section_continuous() %>%
    body_add_par("Education", style = "heading 1") %>%
    body_add_table(my_education, style = "Plain Table 5") %>%

    body_end_section_continuous() %>%
    body_add_par("Certifications", style = "heading 1") %>%
    body_end_section_continuous()

  # iterate over each certification
  for (cert in my_certificates$certs) {
    eval(parse(text = (paste0("word_resume <- body_add_par(word_resume, ",
                              "'", cert, "'",
                              ", ",
                              "style = 'List Bullet')",
                              collapse = ""))))
  }

  word_resume <- word_resume %>%  
    body_end_section_columns() %>%
    body_add_par("SKILLS", style = "heading 1") %>%
    body_end_section_continuous()

  # iterate over each skill
  for (skill in my_skills$skills) {
    eval(parse(text = (paste0("word_resume <- body_add_par(word_resume, ",
                              "'", skill, "'",
                              ", ",
                              "style = 'List Bullet')",
                              collapse = ""))))
  }

  message("------starting to write resumes to file------")
  # finish and write to disk

  # browser()

  # resume_empty_dir <- paste0(resume_temp, "/", name_for_file, "_resume.docx")
  # 
  # write_file()

  word_resume <- word_resume %>%  
    body_end_section_columns() %>% 
    print(target = file.path(resume_temp, paste0(name_for_file, "_resume.docx")))


}
output$resume <- downloadHandler(

    filename = "resumes.zip",
    content = function(file, resume_temp) {

      # resume_temp <- here::here(tempdir(), "resumes")

      # file <- NULL

      message("----starting map2()----")

      # browser()

      require(purrr)
      purrr::map(
        .x = selectedId(),  # reactive list for my_id argument in         # make_my_resume
        .f = make_my_resume
      )

      message("----map2() finished----")

      zip::zipr(zipfile = file, files = resume_temp)
      message("----files zipped----")

    },
    contentType = "application/zip"
  )

我想将简历写入一个临时目录,该目录被压缩并返回到 downloadHandler() 的文件参数。非常感谢!

我找到了解决办法。而不是创建我的 tempdir(在应用程序之外)写入

temp_path <- file.path(tempdir(), "sub_directory")

我创建它只是

temp_path <- file.path(tempdir())

将 docx 文件写入 temp_path 后,我使用 list.files() 和正则表达式的组合来压缩 return 只有我想要的文件,因为有与 docx 文件一起创建的其他一些不需要的目录。在 purr:map() 将 docx 文件写入我的临时目录后,我在 downloadHandler() 内容函数中使用了以下内容:

to_keep <- list.files(temp_path, pattern = ".docx$", full.names = TRUE)

all_temp_files <- list.files(temp_path, full.names = TRUE)

to_remove <- setdiff(all_temp_files, to_keep)

unlink(to_remove, recursive = TRUE, force = TRUE)

zip::zipr(zipfile = file, files = temp_path)

我还是不明白为什么

temp_path <- file.path(tempdir(), "sub_directory")

虽然不起作用。