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_tempfile
gives us a full path, not just a file name. So, the function works as expected, it just writes to another temp folder (created by local_tempfile
, not the one that you assigned to pp
).
library(data.table)
library(purrr)
pp <- withr::local_tempdir()
fw <- function(.x, .y) {
dt <- data.table(country = .x, school_id = 1:2, school_type = c("a","b"))
fwrite(dt, paste(.y, paste0("school.", .x), sep = "/"))
}
purrr::walk2(.x = c("a","b"), .y = pp, .f = fw)
list.files(pp)
#> [1] "school.a" "school.b"
Created on 2024-02-15 with reprex v2.0.2