Here we discuss how to save, load, and quit R environments; see list and structure of objects, and remove objects; save, load, display, and clear commands list.

After installing R, it is important to learn the concepts on this page and also see the page on the working directory.

Summary of Functions for the R Environment
Function Usage
save.image() Save all the objects in your workspace
save() Save certain objects in your workspace
load() Load objects saved from an earlier session
q() Quit session
quit() Quit session
ls() List all objects in a session
str() See the structure of an object
ls.str() See the structure of all the objects
rm() Remove an object
rm(list = ls()) Remove all the objects in a session
savehistory() Save all the commands in a session
loadhistory() Load the commands from an earlier session
history() Display all the commands in a session
CTRL + L Clear all the commands in a session

1 Save, Load, and Quit R Sessions or Workspaces

To save all objects in your workspace for the current session to the working directory, use:

save.image("filename.RData")

For example:

save.image("homework.RData")

Or save to a specific location:

# Windows
save.image("C:/Users/Public/Statscodes/Rdata/files/filename.RData")
# macOS
save.image("/Users/Public/Statscodes/Rdata/files/filename.RData")

Alternatively, to save only certain objects, use:

save(hmw_data, output_table, file = "filename.RData") # List as many objects as you want.

To load the objects saved from a workspace in an earlier session from the working directory, use:

load("filename.RData")

Or from a specific location:

# Windows
load("C:/Users/Public/Statscodes/Rdata/files/filename.RData")
# macOS
load("/Users/Public/Statscodes/Rdata/files/filename.RData")

To quit your R session, use:

q()

Or:

quit()

2 See List and Structure of Objects, and Remove Objects in R

To see list of objects in your environment, use:

ls()

To see the structure of a specific object, use:

str(object)

For example:

str(BOD)
'data.frame':   6 obs. of  2 variables:
 $ Time  : num  1 2 3 4 5 7
 $ demand: num  8.3 10.3 19 16 15.6 19.8
 - attr(*, "reference")= chr "A1.4, p. 270"

To see the structure of all objects in your environment, use:

ls.str()

To remove specific objects, use:

rm(hmw_data, output_table) # List as many objects as you want.

To remove all objects in your environment, use:

rm(list = ls())

3 Save, Load, Display, and Clear Commands List in R

To save all commands in your session, use:

savehistory(".Rhistory")

If saved as above with no filename and to the default working directory, your commands list will be automatically loaded each time you launch R.

Or specify a filename, which will not be automatically loaded:

savehistory("filename.Rhistory")

# Or to specific locations
# Windows
savehistory("C:/Users/Public/Statscodes/Rdata/files/homework.Rhistory")
# macOS
savehistory("/Users/Public/Statscodes/Rdata/files/homework.Rhistory")

To load commands list from an earlier session, use:

loadhistory("filename.Rhistory")

# Or from specific locations
# Windows
loadhistory("C:/Users/Public/Statscodes/Rdata/files/homework.Rhistory")
# macOS
loadhistory("/Users/Public/Statscodes/Rdata/files/homework.Rhistory")

To display all commands in your session, use:

history(max.show = Inf)

Or for limited number of commands, for example, the 10 most recent (default = 25), use:

history(max.show = 10)

Or for the default size, which is the 25 most recent, use:

history()

To clear commands and results from your console, use:

CTRL + L. Note that you can still access the commands with the "up arrow" (\(\uparrow\)) on your keyboard and objects are still available.

Copyright © 2020 - 2024. All Rights Reserved by Stats Codes