Functions for managing workspace in R with example – R tutorial


The workspace is your current R working environment and includes any user-defined objects (vectors, matrices, functions, data frames, or lists). At the end of an R session, you can save an image of the current workspace that’s automatically reloaded the next time R starts. Commands are entered interactively at the R user prompt. You can use the up and down arrow keys to scroll through your command history. Doing so allows you to select a previous command, edit it if desired, and resubmit it using the Enter key.

The current working directory is the directory R will read files from and save results to by default. You can find out what the current working directory is by using the getwd() function. You can set the current working directory by using the setwd() function. If you need to input a file that isn’t in the current working directory, use the full pathname in the call. Always enclose the names of files and directories from the operating system in quote marks. Some standard commands for managing your workspace are listed below

Functions Description
getwd() List the current working directory
setwd("mydirectory") Change the current working directory to mydirectory.
ls() List the objects in the current workspace.
rm(a, b, ..) Removes the objects ab… from your workspace
rm(list = ls()) Removes all objects in your workspace
list.files() Returns the names of all files in the working directory
help(options) Learn about available options.
options() View or set current options.
savehistory("myfile") Save the commands history to myfile ( default =
loadhistory("myfile") Reload a command’s history (default = .Rhistory).
save.image("myfile") Save the workspace to myfile (default = .RData).
save(objectlist,file="myfile") Save specific objects to a file.
load("myfile") Load a workspace into the current session (default =.RData).
q() Quit R. You’ll get a prompt to save the workspace.


x <- runif(20)

First, the current working directory is set to C:/example/ex1, the current option settings are displayed, and numbers are formatted to print with three digits after the decimal place. Next, a vector with 20 uniform random variates is created, and summary statistics and a histogram based on this data are generated. Finally, the command history is saved to the file .Rhistory , the workspace (including vector x) is saved to the file .RData , and the session is ended.

Note the forward slashes in the pathname of the setwd() command. R treats the backslash (\) as an escape character. Even when using R on a Windows platform, use forward slashes in pathnames. Also, note that the setwd() function won’t create a directory that doesn’t exist. If necessary, you can use the dir.create() function to create a directory, and then use setwd() to change to its location.

It’s a good idea to keep your projects in separate directories. You can typically start an R session by issuing the setwd() command with the appropriate path to a project, followed by the load() command without options. This lets you to a startup where you left off in your last session and keeps the data and settings separate between projects. On Windows and Mac OS X platforms, it’s even easier. Just navigate to the project directory and double-click on the saved image file. Doing so will start R, load the saved workspace, and set the current working directory to this location.

Previous articleWhy R? 9 reasons for using r programming
Next articleDifferent Types of Office
Author and Assistant Professor in Finance, Ardent fan of Arsenal FC. Always believe "The only good is knowledge and the only evil is ignorance - Socrates"
Notify of
Inline Feedbacks
View all comments