Time for action - managing the R workspace

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.

  1. Use the ls() function to display a list of your R workspace contents:
    > #list the current contents of the R workspace
    > ls()
    
  2. R will display a list of the objects in your workspace:
    Time for action - managing the R workspace
  3. Use the 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")
    
  4. R will save your workspace file. Browse to the working directory on your hard drive to verify that this file has been created.
  5. Use the q() command to quit R. Ignore or decline any messages that you receive.
    > #quit an R session
    > q()
    
  6. R will close.
  7. Relaunch R by double-clicking on its icon. Then use the ls() command to verify that the current workspace is empty:
    > #list the current contents of the R workspace
    > ls()
    
  8. You will be presented with the following result:
    Time for action - managing the R workspace
  9. Use the 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")
    
  10. Use the ls() command to verify that the saved contents are now contained in the R workspace:
    > #list the current contents of the R workspace
    > ls()
    
    Time for action - managing the R workspace

What just happened?

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.

Listing the contents of the 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.

Saving the contents of the 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.

Loading the contents of the R workspace

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.

Quitting R

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.

Distinguishing between the R console and workspace

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).

Saving the R console

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.

Pop quiz

  1. When saving the R workspace, which of the following extensions should you include?

    a. .txt

    b. .R

    c. .RData

    d. No extension is necessary

  2. Which of the following best describes the relationship between the R console and the R workspace?

    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.

  3. Which of the following is not an option for saving the R console?

    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.

Have a go hero

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:

  1. The battleHistory.csv file contains data from 120 previous battles between the Shu and Wei forces. Read these data into an R variable named battleHistory.
  2. Use the data imported in step 1 to answer the following question. What is the average number of soldiers to engage in combat for both the Shu and Wei forces? Save your results into separate variables, named meanSoldiersShu and meanSoldiersWei respectively.
  3. Save the contents of your R workspace into a new file named rBeginnersGuide_ch_04_hero.RData.
  4. Save the contents of your R console into a new text file named 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.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.7.208