[英]How to write multiple docx files in purrr::map() style with officer from within downloadHandler() in Shiny to temp directory?
I am trying to write multiple docx files from within downloadHandler(). 我正在尝试从downloadHandler()中编写多个docx文件。 When purrr::map() arrives at the part of the mapped function where the resume is written with a unique name to the specified temp directory, that is then zipped and returned to the file argument of downloadHandler().
当purrr :: map()到达映射函数的一部分时,其中使用指定临时目录的唯一名称写入resume,然后将其压缩并返回到downloadHandler()的file参数。 There is an error stating that the specified directory does not exist.
有一个错误表明指定的目录不存在。 An example directory for a single resume is /var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx
单个简历的示例目录是/var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx
I have tried many ways of writing the files to different locations or altering the end directory. 我尝试了很多方法将文件写入不同的位置或更改结束目录。 I am working from within an R project.
我在一个R项目中工作。 I keep getting the error: "Warning: Error in : directory of /var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx does not exist."
我一直收到错误:“警告:错误:目录/var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_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"
)
I want to have the resumes written to a temp directory that is zipped and returned to the file argument of downloadHandler(). 我希望将简历写入压缩的临时目录并返回到downloadHandler()的文件参数。 Thank you so much!
非常感谢!
I found the solution. 我找到了解决方案。 Instead of creating my tempdir (outside of the app) to write to as
而不是创建我的tempdir(在应用程序之外)写入
temp_path <- file.path(tempdir(), "sub_directory")
I created it as just 我把它创建为公正
temp_path <- file.path(tempdir())
Once the docx files were written to temp_path, I used a combination of list.files() and regex to zip up and return only the files I wanted, as there are some other unwanted directories created along with the docx files. 将docx文件写入temp_path后,我使用了list.files()和regex的组合来压缩并仅返回我想要的文件,因为还有一些其他不需要的目录与docx文件一起创建。 I used the following inside the downloadHandler() content function after purr:map() wrote the docx files to my temp directory:
在purr:map()将docx文件写入temp目录后,我在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)
I am still not sure why 我仍然不确定为什么
temp_path <- file.path(tempdir(), "sub_directory")
does not work though. 虽然不起作用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.