In this chapter, we’ll take a quick spin through RStudio. Along the way, you will be creating your first R script. The developers of RStudio provide fantastic documentation on their website, and it is worth exploring this site at your leisure. The intention here is to give you a big overview rather than poking through each and every option (we’ll introduce the various options and settings as we work through our Tauntaun exercise). Show
When you open R Studio for the very first time, you’ll see three panes on your screen. After you’ve written one script (later in this chapter!), you’ll see four panes. These are, starting in the upper left, the (1) source editor, also called the script editor; (2) the workspace and history pane; (3) the R console, and 4) the files, plots, packages, and help pane.
Figure 2.1: The RStudio panes. Let’s look at each of these panels in more detail, starting with the R Console. The R ConsoleThe R console (located in the lower left pane of RStudio) is where commands are submitted to R to execute.
Figure 2.2: The console. When you first open R, the console provides you with information on which version of R you are currently running. R is updated twice a year….more often than this book is updated! It’s generally best to use the most up-to-date program in your work. The version information in the console is followed by some example commands you may wish to submit. Type your commands after the prompt symbol, which looks like this: > You should see a vertical, blinking bar to the right of the prompt, which is where you start typing. What exactly will you be typing? Generally speaking, you’ll enter commands into the console that either:
We’ll cover both of these concepts in great detail in later chapters, but for now type in the sqrt(10) into your Console (or copy and paste it into the Console), and then hit return.
You’ve just used one of R’s functions called Now try typing in Sqrt(10) in your console:
The important take-home point here is that R is case sensitive. Never forget this! The Editor PaneNormally, you would not interact directly with the Console, but instead would type your code into a “script file”, and then “send” the code from the script to the console, where R will execute it. The script file will be your long-term record of what code you ran; the console will only keep a short-term record of it (which can be accessed using the up-arrow).
Let’s create our first script. In RStudio, go to main toolbar, and choose File | New File | R Script. You should see a blank document in the upper left panel of RStudio. Type (or copy) the following two lines of code (shown in the gray box below), and paste it into your new script.
In R, lines of code that are preceded by the # symbol are considered “comments”. So get the square root of 10 is a comment. Make sure to paste in the comments too! Comments are not actually executed in R – they are notes that you (the coder!) type to remind yourself of what the code is intended to do. Notice that this font is green (by default) in RStudio; this helps you quickly differentiate between comments and code. (These color schemes don’t appear in this ebook). To send code to R, place your curser anywhere on the line to be executed, then press the Run button in the upper right hand portion of your screen. You should notice that your curser dropped down to the next line. Instead of running one line at a time, you can select the entire block with your mouse (comment and all), and then press Run. This approach is useful to sending multiple commands to R at once. If you don’t like moving your mouse to the Run button, you can use Ctrl + Enter (PCs) or Command + Return (Macs) to submit a line or selection. Try it! All of the shortcuts in RStudio can be found under Help | Keyboard Shortcuts. Exercise:
Why type your code in script files and not directly in the console? There are two good reasons. First, you’ll make lots of mistakes when you code, and really only want to keep the code that works exactly as intended. In other words, a good practice is to save the script file (cleaned of any coding mistakes), and then just run the script to re-create things when you want them. Second, if you code a lot, you’ll find that you can re-use bits of code from a previous analysis. This saves you from having to re-create the wheel from scratch. When you re-open your document chapter2.R in RStudio, you should see the following in the Script pane:
Figure 2.3: The file, chapter2.R, in RStudio’s editor pane. Notice that the tab with the name chapter2.R is activated, and you can see the code as well. Now you’re free to execute your code once more (you really want that square root of 10!). The Files, Plots, Package, Help PaneThe Files TabNow let’s shift our attention to RStudio’s lower right pane. When you opened chapter2.R again, something new appeared in the Files tab in the lower right hand pane. Do you see the file called chapter2.R? If you don’t see it, try hitting the refresh button, which looks like this:
Figure 2.4: The files tab. Notice the file path C:R_for_Fledglings. This means that our R_for_Fledglings directory is a folder in the C drive….keep that in mind as we go along. Your path may differ. The Files tab lets you create new folders (directories) on your computer, as well as move, delete, and rename files. In RStudio, you can navigate to anywhere on your computer by clicking on the three dots in the upper right hand corner of the Files pane When you open an existing script, the directory that houses your R script file will automatically appear in the Files list. If you add new files, rename files, or delete files, you may need to refresh this list so that RStudio is showing the most up-to-date view of files: The “More”" button in the Files tab is another handy feature. Click on it and you’ll see the following:
Figure 2.5: The More button. When you are working in R, the program needs to know where to find inputs and deliver outputs, and will look first in what is called a “working directory”. You can find your working directory by using the
If you’re using Windows, you may see this response:
If you’re using a Mac or Linux, you may see a response like this:
Generally speaking, when you are working on a project, you will want to organize all of the files for a given project in one folder, and that particular folder should be established as your working directory. This would include the script files, any images that you create in R, the datasets or csv files that you call into R for analysis, etc. It’s a good idea to check what R thinks is your working directory at the beginning of each R session. What if you want to change the working directory? By clicking More button, and then the option Set As Working Directory, you force the folder that contains your file, chapter2.R to be your working directory. Alternatively, you can set the working directory by using the
If you use this option, you’ll need to be able to write out the filepath, which can be hard to remember. R has two handy functions for Windows users that return the filepath to a directory (folder) or file: Copy the code below into your script that pertains to the operating system you use.
The Help TabThe Help tab in the lower right hand pane is another super useful feature of RStudio. If you know the name of the function you want some help with, you can use the
The Help tab now displays the helpfiles for the If there is a function called meatloaf, you can get help on the function by typing in help(meatloaf). R would bring up the help file for the meatloaf function. A shortcut for the word “help” is a question mark. So ?meatloaf does the same thing. But what if you don’t know a function’s name, or are looking for help on something other than a function? First, try using the single question mark notation or
Here, R does not find documentation for meatloaf and suggests that you use the double question mark approach, which would be
??meatloaf. This invokes a keyword search. The double question mark is actually a shortcut for the function Oh well….maybe some day there will be a meatloaf function in R! Exercise: Below are a few common functions that you might have used in Excel if you
are a spreadsheet user. We list the familiar names of the functions Excel uses to take an average (‘average’), compute the standard deviation (‘std’), find a value in a range specified cells (‘index’), calculate the sum (‘sum’) or find the minimum value (‘min’) for a range of cells. R has functions that do these things too, but the names of the functions may be different! Try the
The answers are provided at the end of this chapter (but don’t cheat….you’ll learn a lot more about R by working through exercises on your own!) The Plots TabThe Plots tab holds all of the plots you may create in your R session. To
demonstrate this, let’s use the help function and find more information about a function called If you’ve installed other packages, you may see that the Each helpfile in R contains a section called Examples, and there you can copy code from the helpfile and run it in the console to see an example of the function in action. Scroll down the
Figure 2.6: Plot generated by R’s plot function.
We can run the other examples from R’s When you use the Like other tabs we’ve seen, the Plots tab has several buttons at the top that let you zoom into an image, export the image, delete the image, or clear all images by clicking on the broom icon Save your file before doing the next set of exercises. Exercise:
The source button looks like this: You should see a similar result as below:
Figure 2.7: Sourcing a file will run the full script. In the upper left pane, the
tab for your file chapter2.R is activated and showing the code. As with most things in RStudio, any time you press a button, an R function is invoked. In this case, the “source” button runs the We will use the source button in chapter 9. Exercise:
As a result of sourcing your code or submitting it line by line, R produced some new items of interest. This brings us to to our last pane in our tour of RStudio, the Environment and History Pane (yours may look a bit different than ours depending on what code you have submitted). The Environment and History PaneThe History TabIn the upper right pane of R Studio, you’ll see the Environment and History pane. Click on the History tab, and you should see a history of all of the commands that you have sent to the R console in this session.
Figure 2.8: Notice the code in the History tab. A few things are worth noting about history:
The Environment TabNow let’s click on the Environment tab, which is the Most
Valuable Player (MVP) of RStudio if you are a fledgling. You should see that R has created an object called “x”. Where did this come from? You created this when you copied and ran the code from the Earlier we used the
Now you should see that the object appears in the section on the Environment tab labeled “Global Environment”. A major part of your work involves creating objects, and we’ll be learning about the various ways to create objects of different types in Chapter 4.
Figure 2.9: The Global Environment is shown; it contains two objects. You can see this object by just typing its name:
This action can also be done with the
The objects stored in the Environment and the History together make up the workspace environment. When you close R, it may bring up a window that asks whether you’d like to save the workspace image. Whether it does or not depends on an RStudio setting that you can adjust. Go to Tools | Global Options, and look for the Workspace section in the dialogue box.
Figure 2.10: The workspace settings can be adjusted in Tools | Global Options. The section “Save workspace to .Rdata on exit” prompt allows 3 options: “Always”, “Never”, and “Ask”. If you’ve seen this prompt when you exit R, you likely have the “Ask” option set. If you haven’t been seeing this prompt, select the “Ask” option (you can change it back after this chapter). Now, when you exit R, you’ll be asked whether you should save your workspace or not. Normally, if prompted to save the workspace, you can choose Don’t Save. But let’s just see what happens when you answer that question Save. Make sure the option “Restore .RData into workspace at startup” is also checked. Exercise:
What happened? As before, your script opens in the script pane.
Figure 2.11: The Workspace has been reinstated. The history commands and all objects in the Global Environment were saved in the .RData file.
You may find that you rarely save the workspace if the code can be quickly re-run to generate your objects. In that case, the “Never” option may be right for you. There are times, however, when you will want to save certain elements of the workspace so that you don’t have to re-create the objects. You’ll see how this is done in future chapters. Some things to note about the Global Environment in RStudio:
R Studio SettingsNow that you have a brief introduction to RStudio’s panes, let’s take a quick look at the program’s options, where you can control settings such as font size, etc. Go to Tools | Global Options, and you should see the following box appear:
Figure 2.12: The ‘General’ tab in the Global Options. We’ve already touched on the Workspace section in the dialogue box in the “General” tab section. We won’t go through each and every section here, but rather want to just highlight each section and encourage you to become familiar with the options, even if you don’t touch a thing. Exercise: Click on each section, and explore the various settings available to you.
The R Studio CheatsheetRStudio conveniently points to printable “cheatsheets” that you find useful. Go to Help | cheatsheets, and there you will find the RStudio IDE Cheat Sheet. Clicking on the link will download the cheatsheet pdf to your system, where you can print or save it for future reference. Answers to Chapter 2 ExercisesExercise: Below are a few common functions that you might have used in Excel if you are a spreadsheet user. We list the familiar names of the functions Excel uses to take an average (‘average’), compute the standard deviation (‘std’), find a value in a range specified cells (‘index’), calculate the sum (‘sum’)
or find the minimum value (‘min’) for a range of cells. R has functions that do these things too, but the names of the functions may be different! Try the
Which two parts of RStudio can you execute code select all that apply?Select all that apply. In RStudio, you can execute code in the R console pane and the source editor pane.
In which parts of RStudio can you execute code?RStudio supports the direct execution of code from within the source editor (the executed commands are inserted into the console where their output also appears).
What are the 4 panes in RStudio?RStudio has four main panes each in a quadrant of your screen: Source Editor, Console, Workspace Browser (and History), and Plots (and Files, Packages, Help).
How do I run code in RStudio?To run an R command, put the cursor on the line of the command and then click the Run button at the top of the file window. Or just press CTRL-Enter.
|