Skip to content

File System

Cghlewis edited this page Jan 27, 2023 · 22 revisions

Any time you are programming in R you will need to know how to interface with your file system. At minimum you will need to know things like

  1. What directory are you working in?
  2. What is the path to your file?
  3. What files exist in your directory?

And at a more functional level, working with packages/functions that interface with your file system allows you to programmatically work with objects in your system, rather than manually doing things like create folders, copy files, and rename files.

Consider a scenario where you collect classroom observations every day.

Each day you do the following:

  1. Create a new folder for that day's observations (ex: "obs_YYYY-MM-DD").
  2. Drop that day's observations into that folder.
  3. Create a summary file from those observations.
  4. Then copy that summary file into another folder (ex: "all_obs) that houses all the summary files from each day, and rename each summary file with a date attached (ex: "summary_obs_YYYY-MM-DD").

We could programmatically do many of these steps including creating folders, copying files, and renaming files.

Review files

Create folders

Rename files

Copy files

Check files exist

Delete files


Main functions used in examples

Package Functions
here here()
fs dir_ls(); file_info(); dir_create(); file_move(); file_copy(); file_delete()
base basename(); file.exists(); exists()

Other functions used in examples

Package Functions
dplyr select(); slice(); arrange(); slice_head(); pull()
readxl read_excel()
stringr str_replace(); str_subset(); str_extract_all(); str_replace()
base which.max(); paste0(); as.character(); length()
lubridate today()
purrr map_dfr()

Resources

Clone this wiki locally