R and RStudio can be found on the LCAM server.
nycflgihts13 packagenycflights13 package using the library( ) commandhead( )tail( )Packages are ‘add-ons’ that let you do different things more easily in R. For example, the nycflights13 package includes 5 dataframes that do not come pre-installed in R. dataframes are basically the R version of an excel spreadsheet, and installing this package is helpful because it gives us data to play around with without us having to download the files somewhere on the computer and then load them into R.
To install a package, type install.packages("PackageName") into the console and hit “enter” on the keyboard.
For example, to install the nycflights13 package, copy the code below into the console and hit “enter” on the keyboard:
install.packages("nycflights13")Packages must be “loaded” in order to use them. R does not automatically load all the packages because there are so many that if it did, the program would run very slowly. In most cases, a package only needs to be loaded once each session (i.e., every time you open RStudio again after closing it). Packages can be loaded a few ways, but we are going to mainly use the library( ) command.
To load any package, type library("PackageName") into the console and hit “enter” on the keyboard.
To load nycflights13, copy the code below into the console and hit “enter” on the keyboard:
library("nycflights13")Functions are an incredibly important part of coding, as almost everything we type is a function! A function is a preset that preforms a task within R. They often, but not always, have inputs called “arguments” that provide more information for the function to use. Here, we will use some generic functions to look at some of the data we loaded into R
We are going to use the functions head( ) and tail( ). Both these functions take the name of a dataframe as an argument. The dataframe we are going to look at is called flights, and it is part of the nycflights13 package that we just loaded.
An example of using these functions is shown below, followed by their respective outputs.
head(flights)
## # A tibble: 6 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 1 1 517 515 2 830 819
## 2 2013 1 1 533 529 4 850 830
## 3 2013 1 1 542 540 2 923 850
## 4 2013 1 1 544 545 -1 1004 1022
## 5 2013 1 1 554 600 -6 812 837
## 6 2013 1 1 554 558 -4 740 728
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>
tail(flights)
## # A tibble: 6 x 19
## year month day dep_time sched_dep_time dep_delay arr_time sched_arr_time
## <int> <int> <int> <int> <int> <dbl> <int> <int>
## 1 2013 9 30 NA 1842 NA NA 2019
## 2 2013 9 30 NA 1455 NA NA 1634
## 3 2013 9 30 NA 2200 NA NA 2312
## 4 2013 9 30 NA 1210 NA NA 1330
## 5 2013 9 30 NA 1159 NA NA 1344
## 6 2013 9 30 NA 840 NA NA 1020
## # … with 11 more variables: arr_delay <dbl>, carrier <chr>, flight <int>,
## # tailnum <chr>, origin <chr>, dest <chr>, air_time <dbl>, distance <dbl>,
## # hour <dbl>, minute <dbl>, time_hour <dttm>R. In almost all our exercises moving forward we will use the library( ) function, and in a few classes we will use the install.packages( ) function as we add on to our package base in R. Understanding the basics of how to use functions is also very important, and we will talk about that more this week.nycflights13 package in R during this weeks class, and I wanted to add some troubleshooting tips for anyone who had difficulties. For windows users having this issue, skip to step 2
repository (often abbreviated to “repo”) is a central location where files are stored. Now adays, most of them are in the cloud. There are hundreds of R repos around the world, each containing the data for all the R packages and other files that R programmers might need. When we use the install.packages( ) command, we are telling R to try and download the package from whatever repo you have set. It might be that the repo your RStudio is trying to connect to just isn’t working, (for example, some of them are in India, and would not be much use to us trying to download in the US). In many cases, issues like this can be solved by changing the default repository to one that is geographically closer to your location. Because all the repos are the same, RStudio often refers to them as mirrors.RStudio RTools40 toolchain to compile packages (techy words, I know). This specific toolchain only works with R versions of R4.0.0 or newer. So, before downloading it, check that yourR version is updated by using the command below.
R version
Type R.version.string into the console and hit “enter” on the keyboard, as show below. As you can see, my R version is R4.0.3 from October of 2020.
R.version.string
## [1] "R version 4.0.3 (2020-10-10)"R version less than R4.0.0, install the newest version of R from https://cloud.r-project.org/R version GREATER than R4.0.0, install the RTools40 toolchain from https://cran.rstudio.com/bin/windows/Rtools/
In the R console, copy and paste the code below and hit “enter” on the keyboard (I know it looks all complicated, its just telling R where the Rtools toolchain is)
writeLines('PATH="${RTOOLS40_HOME}\\usr\\bin;${PATH}"', con = "~/.Renviron")Now, restart R. After you restart, you should be able to download packages without any warnings!