Installing R

You can get R at a mirror site of CRAN (Comprehensive R Archive Network). Let’s use the mirror at Institute of Statistical Mathematics (ISM) in Tokyo. Download the approprite version for your OS. You should choose the latest version unless you have a special reason not to. As of this writing, R-3.2.2 [Fire Safety] is the latest. Once you download the packages, install R in the same way you install other software packages.

Watch Roger D. Peng’s YouTube videos for more information.

Mac users can install R by Homebrew instead of using the downloader provided by CRAN. With Homebrew installed, type in Terminal:

brew tap homebrew/science
brew install R


XQuartz (for Mac)

If you use Mac, you should install XQuartz (X11) too.

  1. Go to this page
  2. Download the XQuartz-2.7.7.dmg
  3. Open the downloaded and click XQuartz.pkg
  4. Follow the instruction on screen and install XQuartz


Installing RStudio

RStudio is an integrated development environment (IDE) for R. It helps you manage data-analysis projects using R. With RStudio, it is easy to write R commands, view variables in your data sets, compare graphs you make, write reports including R commands and figures, etc.

However, you do not have to install RStudio to use R. If you prefer another IDE or editor, please use it. I usually use Emacs with ESS instead of RStduio. Emacs is more comfortable to me than RStudio at the moment simply because I have used it for a decade. I recommend RStudio to those who do not have any favorite editors or environments.

You can get RStudio here. First, watch the video on the page for overview. Then, download an open source version of RStudio Desktop that is appropriate for your OS. It should be easiest to download an installer. With the installer, you can install RStudio in the same way you install other software packages.


Setting R and RStudio

A Trial Run

Once you install R and RStudio, open RStudio (or R itself if you choose not to use RStudio). Now you can use R by typing a command at the prompt (“>” symbol) on Console [the left panel in RStudio] .

Let’s type an elementray mathematical formula and see if R works.

3 * 2 + 5 - 4
## [1] 7

If you do not get the correct answer, somthing must be wrong: Ask the instructor for help.

Mac users might get the following warning message (or somthing alike) bofore the first prompt.

  • “You’re using a non-UTF8 locale, therefore only ASCII characters will work”

In that case, quit R by typing q('no') on Console (note that ‘no’ means you don’t want to save the working environment). Then, open Terminal and type:

defaults write org.R-project.R force.LANG en_US.UTF-8

Now, reopen RStudio and check if the massage has gone.

Setting Preferences of RStudio

You can set preferences of RStudio as you want. Click “RStudio” on the menu bar, then click “Preferences” to change settings.

I recommend you set the following.

General

Set “Default text encoding” to UTF-8. You should always use UTF-8 for your entire data-analysis projects.

Code Editing

Check the boxs for: - Show line numbers - Insert space for tab (and set tab width to 2 or 4) - Insert matching parens/quotes - Auto-indent code after paste - Vertically align arguments in auto-indent - Show syntax highlighting in console input

Appearance

Choose a fixed-width font you like, set the font-size that fit best to your display, and select the editor theme that is comfortable to your eyes.

I recommend Ricty font to Japanese Mac users. See this and this for more information.

Git/SVN

Check the box at the top.


.Rprofile

You can specify R options by options() function. For instance, Mac users want to use “quartz” for graphic device. Thus, we type:

options(device = "quartz")

However, if you always use the same options, you wouldn’t like to type the same commands every time. In that case, we can save the options in .Rprofile. Dot-files, files beginning with “.” (full stop), are used to keep the settings of software packages. R automatically reads the settings written in .Rprofile every time you start a new R session.

The file name must be exactly .Rprofile without an extension (not “.Rprofile.txt” or “.Rprofile.R”). You should save it in you home directory. On Mac, the home directory is normally /Users/YOUR_USER_NAME/. On Windows (7 or later), it should be C:/Users/YOUR_USER_NAME/ (sorry, but I’m not sure about Windows).

On Mac, dot-files are treated as hidden files and not shown in Finder. To show them, type in the terminal:

defaults write com.apple.finder AppleShowAllFiles TRUE
killAll Finder

Now you can see dot-files in Finder. Do not edit (or even try to open) the dot-files other than .Rprofile at this point.

Once you finish editing .Rprofile, you should make dot-files invisible again by typing in the terminal:

defaults write com.apple.finder AppleShowAllFiles FALSE
killAll Finder

If you would not like to change defaults by terminal commands, use XtraFinder.

This is my .Rprofile. If you’d like to use it, download the file, rename it appropriately, and save it to your home directory. I suggest Japanese writers have .Rprofile to set up Japanese Fonts (see my .Rprofile).


Getting R Packages

In R, you can use a variety of useful functions other people created. Functions are usually provided as R packages. You can download and install packages in R.

Let’s get some packages we frequently use in class by the function install.packages(). To install plyr, type:

install.packages("plyr", dependencies = TRUE)

If you don’t specify the mirror to use in .Rprofile, you can chosse one here as follows.

install.packages("plyr", dependencies = TRUE, repos = "http://cran.ism.ac.jp")

Now you can use the plyr package by typing library("plyr").

Run the following commands to obtain some other packages.

install.packages("dplyr", dependencies = TRUE)
install.packages("reshape2", dependencies = TRUE)
install.packages("ggplot2", dependencies = TRUE)
install.packages("tidyr", dependencies = TRUE)
install.packages("haven", dependencies = TRUE)
install.packages("readr", dependencies = TRUE)
install.packages("knitr", dependencies = TRUE)
install.packages("arm", dependencies = TRUE)
install.packages("coefplot", dependencies = TRUE)
install.packages("xtable", dependencies = TRUE)
install.packages("stargazer", dependencies = TRUE)
install.packages("devtools", dependencies = TRUE)

With devtools package, you can install develoment versions of packages. Let’s get the development version of knitr from GitHub.

library("devtools")
install_github("yihui/knitr")


Now you are ready to play with R on RStudio. Read Introduction to R for starters.



Back to Class Materials