Installing R and RStudio

This tutorial will aid you in:

Installing R

R is the free software that will be used for running the different “apps” created. It is able to be run on Windows, Macs, and Linux machines. For more information about R, go to the R homepage.

If you are going to install R yourself from the internet, go to https://cran.r-project.org/. From that page, you can choose to install it for Windows, Mac, or Linux machines. Go to the page for your machine and, download, and run the installer.

Once you have installed R, you can launch the the software by searching in your operating system for the program and opening the R icon. Should the installation be successful, you should see the main R window which is called the console (see figure below). This basic graphic user interface (GUI) is were you can type commands and get results of executing these commands immediately.

Installing R Studio

RStudio is an integrated development environment (IDE) for R, which makes it easier to write, edit, and run code in R. It also makes it easier to run R Shiny apps locally on your own machine.

If you are installing RStudio from the internet, go to their website for downloading the product (https://www.rstudio.com/products/rstudio/download/). Click the button to download the free Desktop version. This will automatically scroll the webpage down to choose which version to install. Choose the installer that is compatible with your machine (most likely the Windows or the Mac version). Download the and run the installer.

Once it is downloaded and installed, find the program RStudio on your machine and run it.

R Studio 101

R Studio is a very powerful, freely available, integrated development environment (IDE) developed to facilitate code development and execution through the inclusion of an R console and syntax editing capabilities. Additionally, the platform includes tools for plotting, debugging, work-space management, and code history tracking.

Once installed, proceed to open R Studio. The first time you start R Studio you will see three regions in the interface (see below):

  1. Console: This is the same R console we looked at previously; however, it is integrated into the R Studio environment. Much like the standalone R console, this is where you can type and execute commands.
  2. The upper right hand corner containing the Environment and History tabs:
    • The Environment tab is where R Studio displays all the data sets, objects, functions, etc. in the R environment. In other words, all items in R’s working memory will be displayed in this window. Thus, the first time you open an R Studio work-space, this tab should be empty as you have not yet imported any values into the software for analysis.
    • The History tab represents the database maintained by R Studio of all commands which you have ever entered into the console(https://support.rstudio.com/hc/en-us/articles/200526217-Command-History).
  3. The lower right hand corner containing various tabs, such as:
    • Files tab
    • Plots tab
    • Packages tab
    • Help tab
    • Viewer tab

Installing R Packages

The final part that this tutorial will cover is how to install packages for R, using R Shiny.

First, open RStudio. Then click File -> New File -> R Script. This should open up an untitled file in the editor window. In the editor you can write and run code, the code will run in the console located below the editor.

Packages allow you to download work that other people have done, so that you can build on top of them by using the various functions that other people have created. They only need to be installed once on a computer (as long as it is successfully installed) and they will become part of your library on your computer. From your library, you will then be able to load the package and use it.

If you are operating a machine that does not allow you to save things to your default drive (for example on a Windows machine, your C drive) you might have to do another step. This is common when working on machines where you are using a roving profile. If this is a case, please go to this next section which will show you how to choose where your library will be installed.

To install the package Shiny copy and past the following code into your R File you created in the text editor above. The code below is telling R to install a packaged called “shiny” and will also install any other packages that are needed for it to run.

install.packages("shiny", dep=T)

To run the code either highlight it, or click on that line. Then click on the “Run” button in the upper right of the text editor.

Running an R Shiny App

To check to see if the code is running correctly. We will first want to load the “shiny” packages. This can be done by copying and pasting the code below into the editor. Then, either highlight or click on the line and click the run button like you did previously.

library(shiny)

To see if the packages are working correctly, you can launch an example Shiny app by copying and pasting the below code into the editor and running the code.

runExample("01_hello")

What should happen is a new window will open up, similar to what is seen in the video below. This window that opens up allows you to use interact with the example data and manipulate it. Once it is up and running, you can close the window that opens up in order to stop the app from running.

If everything worked correctly and you were able to get the app to run, then that means you were able to successfully install R, RStudio, and R packages, as well as launch an app.

Setting your library location

If you are unable to install R packages on your computer, that might be because the default location for the the libraries is a location that you do not have permission to write to. If that is the case, you will need to create a new location to install the libraries and also tell R where that location is.

For example, if you want to install the libraries to a folder in your documents, you will need to first create a new folder in your documents, you can all it “my_r_libs”.

Then you will need to run the code below to set the path to that location.

.libPaths("C:/My Documents/my_r_libs")

Ideally that should set the path for your packages that you install to the new folder, where you will be able to install libraries.

If you are doing this, then, whenever you start R, you will need to run that line as well in order to remind R where the libraries are located.