Appendex 1
13 Installing R and Rstudio
- R is free
- Often, less codes are needed in R to plot an elegant graph
- for/while loops (basic concepts in programming) are not necessary in R to make a production-quality graph
- R is the best software for statistical analysis
13.1 installing R
- You can download R from the Comprehensive R Archive Network (CRAN). Search for CRAN on your browser:
- Select the version for your operating system from the CRAN page: Linux, Mac OS X, or Windows.
- You have numerous options once you get at the CRAN download page. The base subfolder is what you want to install. This sets up the foundational packages you’ll need to get started. Instead of using this URL, we’ll learn how to install additional required programs from within R.
- To begin the download, click the link for the most latest version.
- When the installer file has finished downloading, click that tab to begin the installation process. As a result, you’ll need to locate where they store downloaded files and click on them to begin the process.
- To complete the installation, navigate through the various options. All of the default options are recommended.
- Select the default even when you get an ominous warning.
8. When selecting the language, consider that it will be easier to follow this book if you select English
- Continue to select all the defaults:
13.2 Installing RStudio
RStudio is an interactive desktop environment, but it is not R, and when you download and install it, it does not include R. As a result, before we can utilize RStudio, we must first install R.
Download requirements for RStudio
- Download R: http://cran.r-project.org/
- RStudio: http://www.rstudio.org/
- For Mac users only : XQuartz: https://www.xquartz.org/
13.3 Open RStudio
When you start RStudio for the first time, you will see three panes. The left pane shows the R console. On the right, the top pane includes tabs such as Environment and History, while the bottom pane shows five tabs: File, Plots, Packages, Help, and Viewer (these tabs may change in new versions). You can click on each tab to move across the different features.
To start a new script, you can click on file, then New File, then R Script
Rstudio is where people do R programming. You can type codes (commands) into the console (bottom-left panel).
>
means that the console is ready to receive more code.+
means your code is not complete.
You can also write (longer) codes in the script within the code editor (top-left panel).
- The code editor will run the script into the console.
- A new script can be opened by clicking: File -> New -> R Script.
You can run a script by clicking “Run” with the green arrow or by typing ctrl + enter. It is labeled with the red circle.
- Or you can just type your codes directly into the console.
How to set working directory
13.4 Installing R packages
A fresh install of R only provides a small portion of the capability available. In reality, what you get after your initial setup is referred to as base R. Developer-created add-ons provide the additional capabilities. Hundreds of these are presently available through CRAN, with many more provided via other sources such as GitHub. R, on the other hand, makes distinct components available via packages because not everyone requires all of the available capabilities. R makes installing packages from within R very simple. To install the tidyverse package, for example, which we use to share datasets and code for this book, type:
install.packages("tidyverse")
Navigate to the Tools menu in RStudio and click Install Packages. We can then use the library function to load the package into our R sessions:
library(tidyverse)
Until we exit the R session, the package remains loaded. If you encounter an error when trying to load a package, it implies you need to install it first. By passing a character vector to this function, we can also install multiple packages at once:
install.packages(c("tidyverse","ggpubr"))
It’s worth noting that tidyverse installs multiple packages. When a package contains dependencies or uses functions from other packages, this happens frequently. When you use library to load a package, you also load its dependencies.