What is R

R is a functional language used to work with statistics and large data sets.  R is used by data scientists from a variety of industries including: sports, forensics, biotech and IT.  This page covers the basics of installing R, an IDE for R and how to install packages/libraries and import them into scripts.

Where can I learn More about R?

I like the Coursea course on it… and it’s free.  https://www.coursera.org/course/rprog

There are also courses on it at infiniteskills.com : http://www.infiniteskills.com/training/learning-to-program-with-r.html

Installing R on OSX

First, make sure you have home-brew installed on OSX.  Homebrew allows OSX users to easily install libraries and applications with just a simple command line in the syntax of: brew install [something]

Second, make sure you have XQuartz installed as well (instructions for installing it are at: http://xquartz.macosforge.org/landing/)

Now, just do the following commands in your terminal window:

brew tap homebrew/science

After running the above command you should get some library installs.  Once it’s finished and you have your command available again, run the following:

brew install R

This will take awhile.  R will require a lot of dependancies to be installed first.  Brew will handle that for you but it will take some time.  For me, on my macbook and internet connection it took about 20min to download and install R.

R Studio

You will want to download the free R Studio for desktops, at: http://www.rstudio.com/products/rstudio/download/

This will provide you with an IDE for your development.  It makes it easy to install packages, find repo’s, etc.

Running R

You can run R in a terminal window, just by typing: r

and hitting return.

You can also run R by launching R Studio.

Installing Packages

To install packages in R, you first enter R (either in the terminal or you use R Studio.)  From the terminal you run R and at the prompt >

you type:

install.packages(“”) and in the double quotes you would put your library/package you want to install. i.e. install.packages(“devtools”)

You can install multiple packages at once with:

install.packages(c(“somepackge”,”another package”, “a third package”))

Requiring/Importing Libraries and Packages

If you want to use a R library/package in your code, you first have to install it (mentioned above.)  Then you have to import it into your script.  The import is done through the library function:

library(package)

Notice we don’t use quotes.  Unlike Ruby, Python, Java, the library isn’t put in quotes.  So if you installed the devtools package, you would import it in with:

library(devtools) at the top of your script.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *