Help with loop to write out all data frames in memory to csv files

Task: Write out all data frames in memory to csv files.
Test data: Created six data frames all from the Iris dataset loaded into memory

dataframe_env = names(which(unlist(eapply(.GlobalEnv,is.data.frame)))) env.
count <- 0
for(i in dataframe_env){
count <- 1 + count
name <- paste(directory_out, dataframe_env[count]) # works correctly
name <- paste0(name, ".csv") # works correctly
write.csv(noquote((dataframe_env[count])), name ) # csv file giving unexpected results
}

Results: the name variable of the csv is working as expected.
the noquote((dataframe_env[count])) function is resolving correctly as df1 - df6
However when I open the csv files that was created instead of seeing the Iris data in a
csv format I get this for df2.csv:
"","x"
"1","df2"
....and this for df3.csv:
"","x"
"1","df3"
and so on for the rest.

Thanks for the help!
Jade

I would use get() rather than noquote()

library(tidyverse)
dfs <- list(df1 = iris, df2 = iris, df3 = iris)
walk2(dfs, paste0(names(dfs), ".csv"), write_csv)

Created on 2021-06-03 by the reprex package (v1.0.0)

1 Like

@arthur.t 's code will handle the general case of having a short list of data frame to write. The following code also uses readr::write_csv, which has better default arguments than write.csv but addresses the occasions in which it may be inconvenient to manually build a list of data frames df1 \dots df99.

# write_bulk_csv.R
# example to write data frames to csv programmatically
# author: Richard Careaga
# Date: 2021-06-02
# 
require(readr)
require(here)

# data frames, named in a way that can be specified with a regex

df1 <- head(iris)
df2 <- df1
df3 <- df1

# directory in which to save csv files

target_directory <- here("data/")

# collect the data frames in namespace matching pattern,
# in this case `df` into a list and make vector of their
# names

name_pattern <- grep("df.",names(.GlobalEnv),value=TRUE)
obj_list     <- do.call("list",mget(name_pattern))
f_names      <- make_file_names(names(obj_list))

# functions to create the file names and write to file
make_file_names <- function(x) paste0(target_directory,x,".csv")
save_csv <- function(x) write_csv(obj_list[[x]],f_names[[x]])

# loop
for (i in seq_along(names(obj_list))) save_csv(i)

# confirm

dir(target_directory,pattern = "^[d]")
[1] "df1.csv" "df2.csv" "df3.csv"


Thanks Nir, Arthur and Richard for your help. Nir's solution worked and required one change to my existing code base. I'm going to go back and study Richard's and Arthur's solution as they look interesting and something I can learn from.

Best,
Jade

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.