Available interfaces for using R
There are essentially two ways to use or interact with R: RStudio or by using the R Console. The code or syntax you will write or use will be the same for either interface, however RStudio provides you with a more interactive experience. This is the interface that I will use for these workshops. I will demonstrate the R Console to show you the basic differences.
Let’s take a tour and become familiar with the windows in RStudio
When you first open RStudio you’ll see 4 windows or 4 sections on your screen: editor, console, history, and environments with tabs. Let’s start with the environments window – you should see 6 tabs: Environment, Files, Plots, Packages, Help, and Viewer. The Environment tab lists the files/datasets that are being used during the current project. The Files tab allows you to view all the files that are available in your working directory. The Plots tab will show any plots that are created during your session. The Packages tab will list all packages that you have loaded. The Help tab is self-explanatory. A quick sidenote, the Help window is great! Please take advantage of it by using the search function in the Help tab.
The History window will list all the lines of code that you have run until you clear it out. A great way to see what you have done – especially if you encounter troubles along the way.
That leaves the editor and the console. The editor is where you open an R script file and the console is where you run your code as you type it in. To run code that is in your editor – select the bits of code and hit Ctrl-Enter to run it. In the console, you type the line, hit enter and it runs immediately. I use these two windows in tandem. To move between these two windows – Ctrl-2 moves you to the Console window and Ctrl-1 brings you back to the editor window. Of course, a mouse works great too!
One more quick tip – the console window can fill up quite quickly and to me, can feel very cluttered. Remember the History window will keep a history of your code, so it would be ok to clear out the console as you see fit. In order to do this, use Ctrl-L to clear it out.
Sometimes having your program always refer to the same directory, when saving files or when opening files, can be very handy. You’ll always know where your files are! R makes it very easy to accomplish it.
First, let’s do it the long way. To see what the current working directory of your RStudio is by typing in your editor window:
To change the working directory for the current project you are working on type:
Of course, you’ll want to make this a directory on your computer 😉 But as you look at this – do you notice anything odd about this statement??? You’ll notice that the slashes / are the opposite direction than you normally see on a Windows machine. Changing these manually can be a time consuming effort. One way around this is to add an extra \ after everyone in your location. See below:
Always double-check your working directory by checking getwd() Are the results what you were expecting? If not, try it again.
There are easier ways to accomplish this as well:
- In RStudio, Session in the File Menu provides 3 options for setting your working directory:
- To Source File location (the directory where you save your R script and program files). If you try this when you first open RStudio you will get a message that says: “The currently active source file is not saved so doesn’t have a directory to change into.” In other words you haven’t opened any files yet, so R has NO idea where it is working from. This option works only after you have opened a file.
- To Files Pane Location – in the Files Pane – navigate to the location you want to have as your Working Directory. Once you have it selected in the Files Pane, then choose Session -> Set Working Directory -> Files Pane location. You will see the new working directory appear in your console and it should match what you select in the Files Pane.
- Choose Directory – will open a windows dialogue box where you navigate and select the directory of choice. This option is probably the best option once you have opened RStudio and have not opened a file.
- While you are in the Files Pane location – navigate to the directory that you would like to set as your working directory, then in the Files Pane – select More -> Set Working Directory. This option is very similar to the Files Pane Location option under the Session menu of RStudio.
As mentioned in the first section of this workshop, R is made up of a number of packages. Remember that a package is a collection of functions, data, and documentation on a specific topic or analysis. There are 2 types of packages: standard packages – those that came with your Base R OR a package that you downloaded from CRAN or elsewhere.
To view a complete list of R packages available on CRAN, please visit https://cran.r-project.org/web/packages/available_packages_by_name.html
Once you download a package of interest, you need to install it and load it before the functions within are available to you in the R environment.
There are a couple of ways, that I am aware of – at the moment, of downloading and installing new packages.
RStudio file menu – Tools – Install Package
Notice that the default is to search for the package on the CRAN website
If you are using this method – please ensure that the Install dependencies box is selected.
Typing a command in the Editor window
You will notice that with either method that in you RStudio Console – there will be a series of operations that occur during the installation.
Once you have the package installed, you will still need to load the package in order to let R know that you are ready to use the functions within the package. Without loading the package, R will not be aware of the package or of the functions you may be trying to use within a package. To load a package please type:
As we work through the workshop, I will try to have the packages we are using listed at the top of each R script we will be using. I will ask you to install the package and then load it to make it available for your session.
For anyone who relies more on their keyboard than their mouse, here are a few keyboard shortcuts that may be helpful.
Keyboard Shortcut Function
Ctrl-Enter Submit code
Ctrl-1 Move to Source window
Ctrl-2 Move to Console window
Ctrl-L Clear Console window
Alt – <-
Let’s start playing with our RStudio by learning different ways of getting the data in. If you are not attending the workshop and would like a copy of the notes, please send me an email.