The workspace
The R environment keeps track of variables as well as allocates and manages memory as it is requested. One command to list the currently defined variables is the ls
command. A variable can be deleted using the rm
command. In the following example, the a
and b
variables have been changed, and the a
variable is deleted:
> a <- 17.5 > b <- 99/4 > ls() [1] "a" "b" > objects() [1] "a" "b" > rm(a) > ls() [1] "b"
If you wish to delete all of the variables in the workspace, the list option in the rm
command can be combined with the ls
command, as follows:
> ls() [1] "b" > rm(list=ls()) > ls() character(0)
A wide variety of other options are available. For example, there are directory options to show and set the current directory, as follows:
> getwd() [1] "/home/black" > setwd("/tmp") > getwd() [1] "/tmp" > dir() [1] "antActivity.R" "betterS3.R" [3] "chiSquaredArea.R" "firstS3.R" [5] "math100.csv" "opsTesting.R" [7] "probabilityExampleOne.png" "s3.R" [9] "s4Example.R"
Another important task is to save and load a workspace. The save
and save.image
commands can be used to save the current workspace. The save
command allows you to save a particular variable, and the save.image
command allows you to save the entire workspace. The usage of these commands is as follows:
> save(a,file="a.RData") > save.image("wholeworkspace.Rdata")
These commands have a variety of options. For example, the ascii
option is a commonly used option to ensure that the data file is in a (nearly) human-readable form. The help
command can be used to get more details and see more of the options that are available. In the following example, the variable a
is saved in a file, a.RData
, and the file is saved in a human-readable format:
> save(a,file="a.RData",ascii=TRUE) > save.image(" wholeworkspace.RData",ascii=TRUE) > help(save)
As an alternative to the help
command, the ?
operator can also be used to get the help page for a given command. An additional command is the help.search
command that is used to search the help files for a given string. The ??
operator is also available to perform a search for a given string.
The information in a file can be read back into the workspace using the load
command:
> load("a.RData") > ls() [1] "a" > a [1] 19
Another question that arises with respect to a variable is how it is stored. The two commands to determine this are mode
and storage.mode
. You should try to use these commands for each of the data types described in the following subsections. Basically, these commands can make it easier to determine whether a variable is a numeric value or another basic data type.
The previous commands provide options for saving the values of the variables within a workspace. They do not save the commands that you have entered. These commands are referred to as the history within the R workspace, and you can save your history using the savehistory
command. The history can be displayed using the history
command, and the loadhistory
command can be used to replay the commands in a file.
The last command given here is the command to quit, q()
. Some people consider this to be the most important command because without it you would never be able to leave R. The rest of us are not sure why it is necessary.