Skip to content

Instantly share code, notes, and snippets.

@favstats
Created May 21, 2022 23:08
Show Gist options
  • Save favstats/2faca357aa2bfd10623bd76c16adcbde to your computer and use it in GitHub Desktop.
Save favstats/2faca357aa2bfd10623bd76c16adcbde to your computer and use it in GitHub Desktop.
save_csv <- function(d, path) {
if(file.exists(path)){
readr::write_csv(d, append = T, file = path)
} else {
dirs_to_create <- stringr::str_split(path, "\\/") %>%
unlist() %>%
purrr::discard(~stringr::str_detect(.x, "\\.")) %>%
paste0(collapse = "/")
if(stringr::str_count(dirs_to_create) != 0){
dir.create(dirs_to_create, recursive = T)
}
readr::write_csv(d, file = path)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment