data. engineering. life.

weston platter

R - Dynamically Load CSV Files

I’m creating a set of visualizations using R/RStudio, and I need to load a handful of CSV files into R as data.frames. The hurdle I overcame was the question of how to easily and sustainably load the data into R.

I had the option of statically defining a R variable and loading the corresponding CSV file into it. For example,

temperature <- read.csv("./data_files/temperature.csv")

This isn’t pragmatic since we have to update variable and filename according to changing data files. The seeming better option is to search for all CSV files in a directory, and dynamically load their respective data into dynamically assigned variable names. For example,

# get all files in the data_files directory
csv_files <- Sys.glob("./data_files/*.csv")

# loop over files
for (file in csv_files) {
  # load the file contents into R
  df = read.csv(file)

  # convert "data_files/temperature.csv" to "temperature" for the variable name
  full_path = strsplit(file, "/")
  paths = full_path[[1]]
  file_name = paths[length(paths)]
  parts = strsplit(file_name, ".", fixed = TRUE)
  var_name = parts[[1]][1]

  # dynamically assign value to to R variaible.
  # ie, "temperature" is a R data.frame
  assign(var_name, df)


I used to prepare each high school morning by visually scanning Kipling’s famous poem, “If-”.

Self Teaching

I’m pulling an all nighter preparing to teach an “Intro to Servers” class with Girl Develop IT – Boulder.