I am trying to write simulated testdata to a temporary directory. I will have to write lots of files and I want to write the loops with a function from the purrr package, as these seem to have concise designed syntax. But I cannot get purrr::pwalk to work? What am I doing wrong?
library(data.table)
library(purrr)
# define temporary directory
pp <- withr::local_tempdir()
# define function to write simulated files
f <-
function(.x,.y) {
dt <- data.table(country = .x, school_id = 1:2, school_type = c("a","b"))
fwrite(dt, withr::local_tempfile(pattern = paste0("school.",.x), tmpdir = .y))
}
# deploy function to write two files
purrr::walk2(.x = c("at","de"), .y = pp, .f = f)
# why no files in pp?
list.files(pp)
I have referenced these:
https://www.tidyverse.org/blog/2023/05/purrr-walk-this-way/
https://adv-r.hadley.nz/functionals.html?q=purrr#purrr-style
withr::local_tempfilegives us a full path, not just a file name. So, the function works as expected, it just writes to another temp folder (created bylocal_tempfile, not the one that you assigned topp).Created on 2024-02-15 with reprex v2.0.2