Working with Files and Folders
When you import or export objects to disk using a script, you don’t have the luxury of an ‘Open…’ or ‘Save As…’ dialog. You have to specify where it’s coming from (or going to).
All file paths are relative to the working directory. You can view the working directory with getwd()
:
‘directory’ = ‘folder’
To see a list of files in the current working directory, use list.files(x)
, where x is a directory.
list.files()
also lists directories
Optional arguments let you specify a pattern, recurse subdirectories, etc.
To list files in a subdirectory of the current directory:
list.files("./mysubfolder")
Here ‘.
’ tells R to start at the working directory, and then go into ‘mysubfolder
’.
R always requires forward slashes (/
), not back slashes (\
). Sorry Windows users!
one way to change them in your code:
ctrl-f
(Find)
An absolute file path starts with '/'
, which indicates the root directory. It may also include a volume (drive letter).
Examples:
## Define a file name that begins with the root directory (on the current drive / volumne)
x <- "/temp/test.csv"
## See if it exists
file.exists(x)
## [1] FALSE
## [1] FALSE
To ensure you didn’t type a filename incorrectly, use file.exists()
File names on Windows are not case sensitive, but for compatibility across platforms make it a practice to match the case.
You can import a csv file using read.csv(x)
, where x
is the name of a file.
You can change the working directory with setwd()
.
You can also set the working directory from the RStudio Session menu.
If you create a RStudio Project, the working directory defaults to the Project folder.
The ‘Home’ directory is usually your Documents folder. Example;
C:/Users/Andy/Documents
You can tell R to start in the Home directory with the ~
character:
file.choose()
allows you to pick a file using a file selection dialog, then returns the name.
file.path()
lets you concatenate folders and files to construct a syntactically valid name:
## Construct a complete file name from a set of directories and a file name
file.path("~", "BaseLayers", "California", "Parks.shp")
## [1] "~/BaseLayers/California/Parks.shp"
file_path_as_absolute()
(from the tools
package) shows you the absolute path for any folder:
You can save individual objects (variables) to disk with save()
.
.RData and .Rda are common extensions for R data files, but you can name your file anything you want.
To save multiple objects to the same file, use the list argument:
save(list = c(“x”, “y”), file = “~/xy.RData”)
To save your entire workspace (all variables in memory), use save.image()
. RStudio also has a ‘Save Workspace’ button on the ‘Environment’ window, and will probably ask if you want to save your workspace when you quit the program.
save()
stores objects in a binary, compressed file format that R understands. This makes it very flexible and easy to bring back into R using the sister function load()
.
In RStudio, use the Files tab to browse your files, open R files, and set the working directory.
You can open many files by clicking on them in Files pane:
*.R
, *.txt
, *.css
- clicking will open for editing
*.RData
, *.rda
- clicking will import them into RStudio
You can save and open scripts, R Markdown files, etc. from the RStudio File menu.
If you have a set of commands that you want to rerun repeatedly, you can save them as a *.R file and then run them all at once with source()
function.
Example: to run all the commands in a script named import_clean_data.R:
source()
automatically runs all the commands in a script, so only use it with scripts that are ‘ready-to-go’.
To run commands one-by-one, open the script in RStudio and use the ‘run’ button.
nb_files-folders_scgis21.Rmd
Learn how to work with files and folders
Importing and exporting data typically requires passing a file name and/or a directory name.
File names are in reference to the current Working Directory, which you specify with the ‘.
’
The ‘Home’ Directory ('~'
) is a good place to store things you use frequently.
Convenience functions like file.exists()
and list.files()
can make your life easier.