Console
install.packages('medisr', repos = c('https://danmazjen.r-universe.dev', 'https://cloud.r-project.org'))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 course. 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 course), 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 course, 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 medisr helper package that we use for this course to help you install all the needed packages.
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 7.1) to install the medisr package. Hit “Enter” and the medisr helper package will be installed.:
Console
install.packages('medisr', repos = c('https://danmazjen.r-universe.dev', 'https://cloud.r-project.org'))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.
To test whether it worked, try loading the hd_data dataset from the medisr package:
Console
medisr::medisr_install_packages()When you see R code like something::something(), for example with medisr::medisr_install_packages(), you would “read” this as:
R, can you please use the
medisr_install_packages()function from the medisr package.
The most common way of using functions from packages is to load the package with library(), for example like library(medisr). Then you can use the functions from the package directly, like medisr_install_packages(). 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 medisr_install_packages() and not load all the other functions from the medisr package into R. We will be using :: often in this course.
Most of the packages we will be using in this course 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 course, copy and paste this command into the R Console:
Console
medisr::medisr_install_packages()This step sometimes fails for some people because their 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", "broom", "survival", "parameters",
"yardstick", "probably", "marginaleffects", "shiny",
"survminer", "tinytable", "prodigenr"
)
)Same as when you installed the medisr 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.
We will be using two packages from tidyverse for nearly all sessions: 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.