The R workspace stores all user-generated objects (variables in our case) that are created during a session. Its contents can be saved and loaded for future use.
ls()
function to display a list of your R workspace contents:> #list the current contents of the R workspace > ls()
save.image(file)
function to save your R workspace to your working directory. The file
argument should contain a meaningful filename and the .RData
extension:> #save the R workspace to the working directory using save.image(file) > save.image("rBeginnersGuide_Ch_04.RData")
q()
command to quit R. Ignore or decline any messages that you receive.> #quit an R session > q()
ls()
command to verify that the current workspace is empty:> #list the current contents of the R workspace > ls()
load(file)
function to load your saved workspace file. The file
argument should be identical to what you used in step 3:> #load a previously saved R workspace using load(file) > load("rBeginnersGuide_Ch_04.RData")
ls()
command to verify that the saved contents are now contained in the R workspace:> #list the current contents of the R workspace > ls()
You just exercised the primary workspace management functions that you will need to carry your work through multiple R sessions. These included listing, saving, and loading the contents of your R workspace.
The R workspace contains every object that you have created during an R session. Up to this point, our objects have taken the form of variables that either read data from CSV files or store the results of calculations. The ls()
function can be called at any time to list the contents of your R workspace.
To save the R workspace, use the save.image(file)
function. Since we were operating within our R working directory, the file
argument needed only to contain our desired filename and the .Rdata
extension. Alternatively, if you were to save your workspace to a different location on your hard drive, then you would need to enter a complete path in the file
argument. Be sure to always include the .Rdata
extension when saving your workspace, as it is necessary for R to be able to recognize the file when loaded.
To load an R workspace file, use the load(file)
function. Here, the file
argument is identical to the one received by the save.image(file)
function. Hence, if the file you want to load is contained within your working directory, you need only to use the file name and .RData
extension. If it is housed elsewhere, then you will need to use a complete file path.
Note that, depending on your version of R, saving and loading of the R workspace can be automated on launch or quit, or accomplished by clicking through the menu options. You may want to explore the available menu choices and preference settings available to you. This will let you configure R to best suit your workflow. Nevertheless, it is recommended that you continue to use the R console to manage your workspace, because it gives you complete control over your work.
As you have witnessed, the q()
command can be used to exit R. You can, of course, use menu options, keyboard commands, or other methods available on your computer to quit R.
Unless you have specifically told R to save your workspace on exit, all of its contents will be lost. Remember to save your workspace before quitting R.
When you relaunched R and loaded your saved workspace file in the preceding activity, you may have noticed that the contents of your R console were not retrieved. This reveals an important distinction between the R console and the R workspace. Essentially, the workspace stores all of your objects, whereas the console contains a log of everything that has been done to and with those objects.
Consider the act of watching a movie in a theater as an analogy to demonstrate the relationship between the workspace and console. The audience members, movie screen, and chairs are all located within the same room (the workspace). Everything that these entities do sneeze, laugh, chomp, display the movie, get chewing gum stuck to them is recorded in the history of the movie experience (the console).
Thus, the workspace contains objects (such as the people in a movie theater) and the console logs the interactions between them (such as one patron spilling popcorn on the head of another).
Since the console is not saved along with the workspace, you may be wondering how you can preserve the information logged in the R console during a session. While there is no function available in R that allows us to save its contents, we do have other options.
One is to copy and paste the R console into a text editor. Another, depending on your version of R, may be to use the menu to save a copy of your console as a text file. These are the preferred techniques for capturing the R console, although you may be able to think of alternative methods.
In any event, it is highly recommended that you save the R console at the end of every session. Having the log of your previous work can be invaluable to the prevention of rework and to informing your future work. It can also help you organize and remember everything about your current project, especially when you have a large amount of data and many objects to manage.
a. .txt
b. .R
c. .RData
d. No extension is necessary
a. The R workspace and R console can both be saved using the save.image(file)
function.
b. The contents of the R workspace and R console can both be displayed using the ls()
function.
c. The R console stores objects, whereas the R workspace logs the actions related to those objects.
d. The R workspace stores objects, whereas the R console logs the actions related to those objects.
a. Using a built-in R function.
b. Copying the console contents into a text editor.
c. Using the R menu options to save the console as a text file.
d. Taking a screenshot of the R console.
Your final challenge for this chapter will be to collect and organize the remaining portions of Zhuge Liang's resource data. This will entail reading CSV data into R, creating new variables, accessing and manipulating variable data, and saving your R workspace and console. Demonstrate your mastery of these concepts by preparing historic battle data for analysis through the following actions:
battleHistory.csv
file contains data from 120 previous battles between the Shu and Wei forces. Read these data into an R variable named battleHistory
. meanSoldiersShu
and meanSoldiersWei
respectively. rBeginnersGuide_ch_04_hero.RData
. rBeginnersGuide_ch_04_hero.txt
.Feel free to refer back to the previous sections in this chapter for assistance with collecting and organizing this information.