-
Notifications
You must be signed in to change notification settings - Fork 16
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
- What directory are you working in?
- What is the path to your file?
- 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:
- Create a new folder for that day's observations (ex: "obs_YYYY-MM-DD").
- Drop that day's observations into that folder.
- Create a summary file from those observations.
- 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.
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
- https://cran.r-project.org/web/packages/fs/vignettes/function-comparisons.html
- https://epirhandbook.com/en/directory-interactions.html
- https://cfss.uchicago.edu/notes/safe-filepaths/
- https://bash-intro.rsquaredacademy.com/
- http://jenrichmond.rbind.io/post/how-to-use-the-here-package/
- https://www.anyamemensah.com/blog/check-for-file