我正在尝试从downloadHandler()中写入多个docx文件。当purrr :: map()到达映射函数的部分时,将用唯一名称将简历写入指定的temp目录,然后将其压缩并返回到downloadHandler()的文件参数。出现错误,指出指定的目录不存在。单个简历的示例目录是/var/folders/vv/57k257g531b889lqwcgj2z3m0000gp/T//RtmpqhDbg1/resumes/someones_name_resume.docx
我尝试了多种方法将文件写入不同的位置或更改结束目录。我正在R项目中工作。我不断收到错误消息:“警告:/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"
)
我想将简历写入临时目录,该临时目录已压缩并返回到downloadHandler()的文件参数。非常感谢!
答案 0 :(得分:0)
我找到了解决方案。而不是创建我的临时目录(在应用程序外部)以
写入temp_path <- file.path(tempdir(), "sub_directory")
我只是创建
temp_path <- file.path(tempdir())
将docx文件写入temp_path后,我将使用list.files()和regex的组合来压缩并仅返回我想要的文件,因为还会与docx文件一起创建一些其他不需要的目录。在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)
我仍然不确定为什么
temp_path <- file.path(tempdir(), "sub_directory")
虽然不起作用。