4.2 RStudio
We will be working in RStudio. The easiest way to get started is to go to RStudio Cloud and create a new project.
The main way of working with R is the console, where you enter commands and view results. RStudio surrounds this with various conveniences. In addition to the console panel, RStudio provides panels containing:
- A text editor, where R commands can be recorded for future reference.
- A history of commands that have been typed on the console.
- An “environment” pane with a list of variables, which contain values that R has been told to save from previous commands.
- A file manager.
- Help on the functions available in R.
- A panel to show plots.
For these instructions code will appear in the gray box as follows:
fake code
To run the code you can copy and paste the code and run it in your
RStudio session console at the prompt >
which looks like a greater
than symbol.
> fake code
The code can also be added to an R Script to be run.
When the code is run in RStudio the console prints out results like so:
[1] Result
In this tutorial results from code will appear like so:
[1] Result
4.2.1 Installing and Loading Packages
In R, the fundamental unit of shareable code is the package. A package bundles together code, data, documentation, and tests, and is easy to share with others. As of July 2018, there were over 14,000 packages available on the Comprehensive R Archive Network, or CRAN, the public clearing house for R packages. This huge variety of packages is one of the reasons that R is so successful.
4.2.2 Installing Packages - RStudio
Installing a package using RStudio requires selecting the Install Packages Button irstudin the Files, Plots, Packages Pane
In the pop up box that results simply type the name of the package and check “install dependencies” and click Install
Its also possible for you to install and load packages from the console.
Always make sure to put the package name in quotes when installing and
setting dependencies = True
install.packages("tidyverse", dependencies = TRUE) library(tidyverse)
You only need to install a package once, but you need to reload it every time you start a new session.
4.2.3 Getting Help
To get help about a particular package or function you can access the help pane in RStudio and type its name in the search box.
The help()
function and ?
help operator in R provide access to the
documentation pages for R functions, data sets, and other objects, both
for packages in the standard R distribution and for contributed
packages. To do so type as follows
help({function})
help(package = {package name})
?{function}
?"{package name}"
Working in the console in R you can run all the commands necessary to perform a data analysis. However one of the great features of R is that its a programming language. So as you are running the commands to perform an analysis you can also be creating a record of what you have run. Creating a script is a way to do this which can ensure reproducibility.