logo

 

Downloading R and RStudio

GreenBlueBar.gif GreenBlueBar.gif

R is a "programming environment" that allows you to perform a huge array of statistical (and non-statistical) tasks with very few commands. You can use it in at least two ways. You can cut and paste programs that I or others have written, make minor edits, and run the program. For this you don't really need to know anything about programming other than intelligently editing the program that is there. Alternatively, you can write extensive programs that will do just about anything you want. The nice thing is that if you want to take the latter course, you will find that someone has been there before you. If you want to calculate confidence limits on a population proportion, you just download the library that someone else (Ken Kelley) has written, provide the necessary information, such as sample size and sample proportion, and click "run."

As I have said elsewhere, if you can download and install iTunes on your laptop, you can download and install R. You can also download and install RStudio, which is both a very nice editor for R and a graphical user interface that loads R when you start it up and provides a frame for R output and another frame for graphics. This document is simply designed to help you download those two programs. For many people this page will be far more than they need, because the discussion in the text should probably be sufficient. However I have done a more complete job here, including screenshots, for those who would like more direction.

R is available for Windows, Macs, and Unix. I originally wrote this with respect to a Windows machine, though I now use an iMac, but installing R and RStudio on a Mac is virtually the same process. You should easily be able to adapt what I say to other environments. The R software is available at http://cran.r-project.org/. Click on that link and it will take you to the site where you will see something like:



Opening Page

On the left are links you might want to use in the future but can ignore for now. In the center top are the links for downloading what you want. Because I am writing for a Windows machine, I would click on the Windows link. Click on your choice now, and you will see the screen shown below. (Ignore the links in the section labeled "Source Code for all Platforms.") For my Mac, all of the steps are almost exactly the same, though I will continue to discuss the Windows version.


R for Windows

At this point click on "base," which will give you:

Download Page

The last link that you need to worry about is the Download link near the top of that window. Just click on that and, when asked, click on "Save File." It will take a while to download, but that's just because it is a huge set of files. When downloading is complete, click on the file you just downloaded and it will install R. You will probably be happy just to take the defaults. Don't worry about any scary messages that you might get from Windows asking if you really want to load this dangerous software. Just tell it to go ahead.


And Now for RStudio

It is much easier to work with R if you have a good editor to use. For several years I used an editor called Tinn-R. It is a very nice editor and I like it a lot. Recently I started using RStudio, and I like it even better. My memory is that RStudio was a bit easier to install than Tinn-R. The nice thing about RStudio is that it keeps track of what you are doing. If you type "print(b", it will automatically supply the closing ")" that you need. Sometimes in R blocks of text are set off with braces, as in {abcd}. If you have a bunch of nested braces, it can be very confusing to figure out which "{" is closed with which "}". The editors will highlight the relevant bits, which can be very very helpful. An example of the RStudio interface is shown below. Notice that there is a place for the code (upper left), a place for responses from the console (the answers) (lower left), a graphic frame (lower right), and information of current variables (upper right). The way that we generally use R, it will echo our commands to the command line. Thus if you run a program that has a line that says "y <- sqrt(5)" , then that line will appear on the console window, followed on the next line by 2.23607. That makes it a bit hard to read everything, but you can do it.




To install RStudio go to the the link RStudio,. You will see:



Click on the link for "Powerful IDE for R -- download now". On the next page click on "Download RStudio Desktop." Select your operating system from the next screen, and the file will download. (Mine took about 4 minutes. Then simply double click on the downloaded file to install it.)

That's all there is to it!

Once you install these two programs, you are ready to go. In fact, you really only have to open RStudio, because it will automatically start R. You can then enter commands into RStudio, highlight them, click on the Run button, and the code will execute, with output appearing in the lower left. However there is one extra step that I would suggest.

If you were to enter a command such as print("This is line 1"), the output will appear in the lower left, but the cursor will remain in the upper left waiting for the next command. That seems as if it would be a good idea, but I find that I often start overtyping important material when I think that I am in the lower left and am free to type. That gets annoying fast! If you have a Windows machine, start RStudio, go to tools/global options/code editing and make sure that there is a check mark on the third line from the bottom, which says "Focus console after executing from source." If you have a Mac, go to the tool bar, click on RSudio, Preferences, code editing, and make sure that same box is checked. You don't have to do this, but I think that you will find that it makes your life easier.

Finally, a few important points you need to know about R.

First, you need to know that R is case sensitive. (I said that before, but it bears repeating.) That is a holdover from the fact that it was written by a bunch of Unix types. Unix people seem to like to make things messy by making them case sensitive. So when you try to run a program and it tells you that the data file doesn't exist, for example, the first thing to do is to check the capitalization in the calling command.

There are a couple of symbols that you will see often, and you need to know what they mean. If you see a bunch of text preceded by a "#", the "#" tells R that this is a comment. R will ignore everything on the line after it. If you look at the output window and see a "<", that is just the start of a command. It is a prompt. Just ignore it. Similarly, a "+" at the beginning of a line just tells you that this is a continuation line. Again, ignore it.

As I have said elsewhere, another thing that you need to know is that when you supply a URL to R, it makes a big difference how you type your "slashes." If you use the right click on your mouse to copy a file path from Windows, it will probably look like "C:\Users\Dave\Documents\Webs\fundamentals9\Supplements\R-Programs". But R won't accept that because the slashes go the wrong way. (Remember, these were Unix types.) What R wants is "C:/Users/Dave/Documents/Webs/fundamentals9/Supplements/R-Programs." There is a way to sneak around that, but there's no point in adding clutter. Just retype the slashes correctly.

One note for Mac and Unix users. In Windows you can tell R to read a file that starts out as "https://www.uvm.edu/...". In fact, I recommend doing that if the site you are reading from accepts secure http notation. (The "s" stands for "secure.") In any browers on the Mac you can do the same thing, and I recommend it. But within R, the program on a Mac or Unix machine will not allow "https." You need plain old "http." I have tried to leave the "s" off in the code I give you, but if I have forgotten, and it says that it can't find the file, just take the "s" off.

One additional fact, until I think up more. If you get lost in R and want help, you can try typing, for example, "help(plot)" I don't think that their help windows are always the clearest, (type help(url) and you will wish that you hadn't), but they are better than nothing. If you want to look further, try something like "RSiteSearch("Student's t") if you are on the Internet. That will send you to a whole bunch of good sources.


Specific Topics

GreenBlueBar.gif GreenBlueBar.gif dch: