Situation: I have several data frames that I like to export as CSV files into the working directory for further processing.
Goal: Export a collection of data frames from the workspace as csv files to the working directory by using a single function (batch export).
Details:
My question is not about changing the content of the data frames, so you can just use any example data provided by R for reproduction.
The function I use for exporting a single data frame is
csvExport <- function(data, enc = 'utf8'){
name <- paste(deparse(substitute(data)), "csv", sep=".")
con <- file(name, encoding = enc)
write.csv(data, file = con, row.names=FALSE, quote = TRUE, sep = ";")
text<-"exported!"
print(paste(name, text, sep=" "))
}
Since I do not like to call the function for each data frame to export explicitly I am looking for a way to export the data frames by indicating which ones from the workspace I like to export with:
export <- ls()[1:20]
I then tried using this function (with some variation of the for loop) with the above list:
multipleCSVExport <- function(export){
for (i in export){
csvExport(i)
}
}
However, I am not able to create the expected result.
csvExportfunction so that thedataargument is a character, which is really what you should have done in the first place, most likely.getwill be useful.