The meaning of ~
, the home directory, is dependent on the operatoring system:
/home/<username>
/Users/<username>
"C:\Users\username\Documents"
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"
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.
getwd()
: The current working directorysetwd(path)
: Change the working directorysetwd()
will only work for the author
setwd()
makes it hard to use reuse the script for other projects
~
: 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"
.
/
even on Windows systemslist.files(path)
: List the files in a directory/folderlist.dirs(path)
: List the folders and files in a directory/folder, use the argument recursive=FALSE
to only list filesfile.path()
: Construct a path to a file from the componentsnormalizePath(path)
: Convert file path to a platform specific file pathpath.expand(path)
: Expand a path namebasename(path)
: Removes the path of the file and returns the file namedirname(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"
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.
ls()
: Display all objects in the workspacerm(object)
: Remove object(s) from the workspacesave(object, file)
Save objects to a RData filesave.image(file)
: Save all objects in the workspace to an RData fileOften 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.
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