Home Directory

The meaning of ~, the home directory, is dependent on the operatoring system:

The command Sys.getenv("HOME") will find the value of the environmental variable HOME which is often the path of the home directory. The command normalizePath("~") will help interpet the meaning of ~.

[1] "C:/Users/Brian.Pattiz/Documents"
[1] "C:\\Users\\Brian.Pattiz\\Documents"



Working Directory

The working directory is the path on the computer that sets the location of the files that is read or saved into R. The default working directory is the home directory.

The Drawbacks of using setwd()

  • Using the setwd() will only work for the author
    • Can’t be use if the goal is to have others check your work or use your script
  • The script is not self-contained or portable. What happens if the folder structure on a shared drive changes?
  • Use of setwd() makes it hard to use reuse the script for other projects
  • Alternatives is to use Rstudio projects and/or the here package



Paths and Files

Symbols

  • ~: home directory
  • .: current directory
  • ..: parent of current directory
  • / as first character: root directory, e.g. "/folder"
  • / with path: seperator between directories in path, e.g. "/folder/subfolder")
  • / Windows and DOS operating systems only, backslash \ is equivalent to /. If you use this format in R, you will need to use double backslash \\ use as an escape character, e.g. "C:\\folder\\subfolder".
    • To maintain compatibility between platforms, it is recommended to stick to using a forward-slash / even on Windows systems

Functions

  • list.files(path): List the files in a directory/folder
  • list.dirs(path): List the folders and files in a directory/folder, use the argument recursive=FALSE to only list files
  • file.path(): Construct a path to a file from the components
  • normalizePath(path): Convert file path to a platform specific file path
  • path.expand(path): Expand a path name
  • basename(path): Removes the path of the file and returns the file name
  • dirname(path): Removes the file name and returns the directory informath
[1] "Paths_and_Directories.html" "Paths_and_Directories.Rmd" 
 [1] "../.matplotlib"             "../Adobe"                  
 [3] "../AIR Forum Workshop"      "../cache"                  
 [5] "../Custom Office Templates" "../Data"                   
 [7] "../Documentation"           "../DU"                     
 [9] "../Forms"                   "../Hiring"                 
[11] "../Jupyter Notebooks"       "../libs"                   
[13] "../My Music"                "../My Pictures"            
[15] "../My Videos"               "../OneNote Notebooks"      
[17] "../Outlook Files"           "../Papers"                 
[19] "../PowerShell"              "../Presentations"          
[21] "../Programming"             "../R Training"             
[23] "../Receipts"                "../Tableau"                
[25] "../Tidy_Data_files"         "../University Endowments"  
[1] "~/R Training"
[1] "C:/Users/Brian.Pattiz/Documents/New Folder"
[1] "Paths_and_Directories.Rmd"
[1] "C:/Users/Brian.Pattiz/Documents/R Training"



Workspace

The workingspace (working environment) is all the objects, packages, and user created functions that has been saved in the current session or loaded from a previous session. A workspace may be saved into a .Rdata file. These are R specific files that can store workspace.

Functions

  • ls(): Display all objects in the workspace
  • rm(object): Remove object(s) from the workspace
  • save(object, file) Save objects to a RData file
  • save.image(file): Save all objects in the workspace to an RData file

Restaring R

Often times you will want to restart fresh in R. Many users will run the command rm(list = ls()) at the beginning of their script. It is advised that you do not.

  • The command does not create a new R process. It just deletes all the objects from the workspace. Packages and functions are still loaded.
  • Your script is vulernable to hiddlen dependencies on processes that were run prior to executing that command.
  • If placing the command in a script, rerunning the script might not work and anyone else doing so might lose their saved work.
  • If in RStudio: Session > Restart R or the keyboard shortcut Ctrl+Shift+F10 (Windows and Linux) or Command + Shift + F10 (Mac OS)
  • mise package:

Clears the workspace. Useful for the beginnings of R scripts, to avoid potential problems with accidentally using information from variables or functions from previous script evaluations, too many figure windows open at the same time, packages that you don’t need any more, or a cluttered console