When you click the Source button,
source('~/.active-rstudio-document')
appears in the console. What RStudio actually does here is save your script in a temporary file and then use the R function source()
to call that script in the console. Remember this function; you’ll meet it again.
If you click on the little arrow next to the Source button in RStudio, you see two different source options, as shown in Figure 2-6. By clicking the Source button before, you used the option without echo. This means that R will run the complete script at once, but won't send any output to the console.
Figure 2-6: Sourcing your code with or without echo in RStudio
If you click on the second option, R runs again the complete script in one go, but this time it will show every individual line in the console. So both options differ only in the output you see. You can safely try out both options to compare.
You can use the echo option also outside RStudio by using the source()
function with the argument echo
set to TRUE
. We explain functions and arguments in Chapter 3, and far more detailed again in Chapter 8.
Whether you source with or without echo doesn't make any difference regarding the results of your code. You can use the echo option if you want to source a long script and keep track of which part of the script R is currently carrying out.
Finding help on functions
We discuss R’s built-in help system in Chapter 11, but for now, to get help on any function, type ? in the console. To get help with the paste()
function, for example, type the following:
> ?paste
This code opens a Help window. In RStudio, this Help window is in the bottom-right corner of your screen by default. In other editors, the Help window sometimes appears as a local web page in your default web browser.
You also can type help, but remember to use parentheses around your search term:
> help(paste)
Navigating the Environment
So far in this chapter, you’ve created several variables. These form part of what R calls the global environment. The global environment refers to all the variables and functions (collectively called objects) that you create during the session, as well as any packages that are loaded.
Often, you want to remind yourself of all the variables you’ve created in the environment. To do this, use the ls()
function to list the objects in the environment. In the console, type the following:
> ls()
[1] "h" "hw" "x" "y" "yourname" "z"
R tells you the names of all the variables that you created.
One very nice feature of RStudio lets you examine the contents of the environment at any time without typing any R commands. By default, the top-right window in RStudio has two tabs: Environment and History. Click the Environment tab to see the variables in your global environment, as well as their values. For example, in Figure 2-5 you see that the global environment contains one object called
h
that contains the value "hello"
.
If you decide that you don’t need some variables anymore, you can remove them. Suppose that the object z
is simply the sum of two other variables and no longer needed. To remove it permanently, use the rm()
function and then use the ls()
function to display the contents of the environment, as follows:
> rm(z)
> ls()
[1] "h" "hw" "x" "y" "yourname"
Notice that the object z
is no longer there.
You have several options for saving your work:
✔ You can save individual variables with the save()
function.
✔ You can save the entire environment with the save.image()
function.
✔ You can save your R script file, using the appropriate save menu command in your code editor.
Suppose you want to save the value of yourname
. To do that, follow these steps:
1. Find out which working directory R will use to save your file by typing the following:
> getwd()
[1] "c:/users/andrie"
The default working directory should be your user folder. The exact name and path of this folder depend on your operating system. (In Chapter 12, you get more familiar with the working directory.)
If you use the Windows operating system, the path is displayed with slashes instead of backslashes. In R, similar to many other programming languages, the backslash character has a special meaning. The backslash indicates an escape sequence, indicating that the character following the backslash means something special. For example,
\t
indicates a tab, rather than the letter t. (You can read more about escape sequences in Chapter 12.) Rest assured that, although the working directory is displayed differently from what you’re used to, R is smart enough to translate it when you save or load files. Conversely, when you type a file path, you have to use slashes, not backslashes.
2. Type the following code in your console, using a filename like yourname.rda
, and then press Enter.
> save(yourname, file = "yourname.rda")
R silently saves the file in the working directory. If the operation is successful, you don’t get any confirmation message.
3. To make sure that the operation was successful, use your file browser to navigate to the working directory, and see whether the new file is there.
You have a file browser in the lower-right panel of RStudio.
To retrieve saved data, you use the load()
function. Say you want to retrieve the value of yourname
that you saved previously.
First, remove the variable yourname
, so you can see the effect of the load process:
> rm(yourname)
If you’re using RStudio, you may notice that yourname
is no longer displayed in the Environment pane.
Next, use load
to retrieve your variable. Type load followed by the filename you used to save the value earlier:
> load("yourname.rda")
Notice that yourname
reappears in the Environment pane of RStudio.
Chapter 3
The Fundamentals of R
▶ Using functions and arguments
▶