I want to archive two files stored in a temporary directory to the root of a tar.gz archive. I only found this ugly workaround (changing and resetting the working directory within the function). If I didn't do that, the files would be archived within their entire path to their temporary directory, which I don't want.
tar_files <- function(file, data) {
# create tmp dir
tmp <- tempdir()
# write data file
readr::write_csv(x = data,
file = file.path(tmp, "data.csv"),
na = "NA",
append = FALSE,
)
# write xml file
readr::write_file(x = "test",
file = file.path(tmp, "meta.xml"),
append = FALSE
)
# working dirs
where_i_was <- getwd()
on.exit(setwd(where_i_was))
setwd(tmp)
# export as tar.gz
tar(tarfile = file.path(where_i_was, file),
files= c("data.csv",
"meta.xml"),
compression = "gzip",
tar = "tar"
)
unlink(tmp)
}
Is there a more elegant way to achieve the same result?