R-如何在purrr :: map()样式中编写多个docx文件,并在shinny to temp目录中in dlown



我正在尝试从downloadhandler((中编写多个DOCX文件。当purrr :: map((到达映射函数的一部分时,在其中编写了指定临时目录的唯一名称的简历时,然后将其拉开并返回到下载handler((的文件参数。有一个错误,指出指定目录不存在。单个简历的示例目录是/Var/folders/vv/57K257G531B889LQWCGJ2Z3M0000GP/T//rtmpqhdbg1/resumes/someone_name_resume.docx

我尝试了许多方法将文件写入不同位置或更改端目录的方法。我正在从R项目中工作。我一直遇到错误:"警告:错误:/var/folders/vv/57K257G531B8889LQWCGJ2Z3M0000GP/t/rtmpqhdbg1/resumes/some_name_resume.docx不存在。

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

我想将简历写入temp目录,该目录被拉开并返回到下载handler((的文件参数。非常感谢!

我找到了解决方案。而不是创建我的tempdir(在应用程序之外(写为

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

我将其创建为

temp_path <- file.path(tempdir())

一旦将DOCX文件写入temp_path,我就会使用list.files((和Regex的组合来zip并仅返回我想要的文件,因为还有其他一些不需要的目录与docx文件一起创建。我在purr:map((之后使用以下内容,将docx文件写入我的temp目录:

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

不起作用。