Bulk Import/Export Files to/from R

I am trying to learn how to:

  • Import all files (of a certain type) from the working directory and name them accordingly
  • Export all files from R to the working directory and name them accordingly

1) Importing

Suppose I have these files (of a certain type) on my working directory with different names(my_file.csv, my_r_file.csv, my_file.RDS, my_r_file.RDS etc.). The function below can list all of their names:

temp_RDS = list.files(pattern="*.RDS")
temp_CSV = list.files(pattern="*.csv")

But from here, I don't know how to bulk import them and name them accordingly (using an index). For example:


file_1 = read.csv("my_file.csv")
file_2 = read.csv("my_r_file.csv")

file_1 = read.csv("my_file.RDS")
file_2 = read.csv("my_r_file.RDS")

2) Exporting

Suppose I have these files in R with different names - I would like to save all these files as csv files into the working directory. Suppose I have these files in R:

monday = data.frame(name = "john", day = "monday")
october = data.frame(name = "sara", month = "october")
# etc

I found this code that might be able to export (8) files provided they all start with "df":

dat <- do.call(rbind, lapply(1:8, function(x)get(paste0("df", x))))

#CSV Option
 for(i in 1:ncol(dat)) write.csv(dat[,i], paste0("file_",i, ".csv"), row.names=F)

#RDS option
 for(i in 1:ncol(dat)) saveRDS(dat[,i], paste0("file_",i, ".RDS"), row.names=F)
  • Are there more direct/efficient ways to bulk import/export files from/to R?

Thank you!

To import files, I like to use assign to construct the variable names. So to get at the CSVs, you could do something like this:

tmp_csv <- list.files(pattern = '*.csv')
    function(i) {
        # where i is the ith file in the file list
        var_name <- paste('csv', i, sep = '_')
        tmp_df <- read.csv(tmp_csv[i])
        assign(var_name, value = tmp_df)

As far as exporting, I think your method is just fine.

1 Like

Thank you so much! Just a question - can the code you have provided work if you replace "read.csv" with "write.csv"? Can list.files() also be used to get files from the Global R Environment? Thank you so much!

Objects in the global environment are listed with ls() and it can take a pattern argument as well

ls(pattern = "^csv")

Then you can do something like this


ls(pattern = "^csv") %>% 
    walk(~ {
        write_csv(get(.x), paste0(.x, ".csv"))
1 Like

can the code you have provided work if you replace "read.csv" with "write.csv"?

If by this, you mean the body of the function inside lapply would look like this:

        var_name <- paste('csv', i, sep = '_')
        tmp_df <- write.csv(tmp_csv[i]) #instead of read.csv
        assign(var_name, value = tmp_df)

This would not work, because the value of tmp_csv[i] is a string.

Regarding the environment - it's worthwhile to be a stickler about language here. "Files" cannot be stored in the .GlobalEnv (the global environment). Files are found in directories on your machine, whereas objects exist in environments. The reason it's important to be so particular about the language is because when you talk about files, it's not always clear if we mean:

  • The file name
  • The file data
  • The file metadata

Regarding working with the objects in your environment, @andresrcs 's answer is really excellent.

This topic was automatically closed 21 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.