Console
install.packages("pak")Now that you have RStudio and R on your computer and have read about them, you need to install the R packages we’ll use in the workshop. R packages are bundles of R code that other people have written. There are so many R packages available that there is likely an R package for anything you’d like to do in R. Making use of R packages can help you immensely when doing your research.
Before we continue, let’s briefly explain what some terms mean.
mean() is a complete piece of text that tells R to calculate the mean of some numbers.
() at the end (at least in this workshop), that means it is a function (an action).mean() above..R that contains R code that completes tasks in a sequence (from the top of the file to the bottom).For this workshop, we will be focusing on R packages that are powerful and general-purpose enough to help you in multiple aspects of your research. To install these packages, you’ll need to install our custom r3 helper package that we use for this workshop to help you install all the needed packages and to run some basic checks during the pre-workshop setup. To install it, you’ll first need to install the pak package.
When running R code in the Console, sometimes it may show up as red text. Usually this means there is an error or a warning. Unfortunately, the Console sometimes shows text in red even when there is no error or warning. Don’t be alarmed if you see red text. Take a moment and read the text to see if it says “error” or “warning”. If it doesn’t, then ignore it!
Open your RStudio and copy and paste the below code into the Console (panel “B” in Figure 8.1):
Console
install.packages("pak")Next, copy and paste (or type) the function below into the RStudio Console (panel “B” in Figure 8.1) to install the r3 package. Hit “Enter” and the r3 helper package will be installed.
You might encounter an error when running the below code. That’s ok, if you restart R by going to Sessions -> Restart R and re-running this code, it should work. If it still doesn’t, try to complete the other tasks, complete the survey, and let us know you have a problem in the survey.
The problem usually happens because some institutions block or restrict what types of software get installed. If you run into issues, try to install in administrator mode, or ask IT to allow you to install the package.
Console
pak::pak("rostools/r3", upgrade = TRUE)When you see R code like something::something(), for example with pak::pak(), you would “read” this as:
R, can you please use the
pak()function from the pak package.
The most common way of using functions from packages is to load the package with library(), for example like library(pak). Then you can use the functions from the package directly, like pak(). When you use the code :: (pronounced “colon colon”), you are telling R to directly use a function from a package, without needing to load the whole package and all of its other functions too. We use this trick because we only want to use the one function pak() and not load all the other functions from the pak package into R. We will be using :: often in this workshop.
Most of the packages we will be using in this workshop are bundled together into one package called tidyverse. This package is a collection of packages that are designed for common tasks in data science, ranging from data exploration to data visualization. As the name suggests, tidyverse is an attempt to organize the “universe” of data analysis by providing packages that guide workflows and lead to more reproducible analysis projects. To install all the packages we will use for this workshop, copy and paste this command into the R Console:
Console
r3::install_packages_introduction()This step sometimes fails for some people because their work computer restricts how certain software are installed. If you do have issues, you can manually install the necessary packages with this code:
Console
install.packages(c("tidyverse", "here", "hexbin", "prodigenr", "styler", "usethis"))Same as when you installed the r3 package, this may take some time to install all the packages. We have you use this function to install everything rather than install all the packages with install.packages() individually, as this makes it a bit easier for you to get set up.
The specific packages from tidyverse that we will use are ggplot2 and dplyr. These packages provide a set of tools for the most common data analysis tasks and have excellent documentation and tutorials on how to use them.
dplyr (along with a complementary package tidyr) is a package that is very popular and contains important data manipulation functions, including functions that select and/or create variables depending on certain conditions. dplyr is built to work directly with data frames (rectangular data like those found in spreadsheets).
ggplot2 is a data visualization package that can be used to create bar charts, pie charts, histograms, scatterplots, error charts, and more. It uses a “grammar” as a way to construct and customize your graphs in a layered, descriptive approach.