class: center, middle, inverse, title-slide .title[ # PADP 7120 Data Applications in PA ] .subtitle[ ## RLab 1: Intro to R ] .author[ ### Alex Combs ] .institute[ ### UGA | SPIA | PADP ] .date[ ### Last updated: January 09, 2024 ] --- class: inverse, middle, center # Does everyone have access to RStudio? --- # Objectives - Open and navigate RStudio - Create an R project to organize our work - Load R packages - Start an R markdown document and run code - Export your R markdown document to HTML --- # How to Follow RLab Slides - Bullets provide background, concepts, etc. - Block quotes in bold like below contain a prompt > **Do this thing in this block** - Example R code is in blocks like below ```r summary(cars) ``` - Or `in-line code like this` - You can copy-paste code from slides to your RStudio --- # RLab Process - I will complete RLabs along with you to help show what to do - This involves switching between slides and RStudio, blocking one from your view of the screen - Helps to have the slides open on your computer - If you need to see something I did previously in RStudio, maybe wait until I switch back to RStudio before asking unless you feel yourself getting lost. --- # RLab Process - Highly likely that you will encounter errors. This is normal. This lab will deliberately cause a few errors. - If so and you cannot figure out how to fix, let me know - Troubleshooting can help you learn - First few weeks may involve frequent stopping as I assist students. - If you are waiting while I assist others, your patience is appreciated. Try to resist working ahead. Maybe help a neighbor. - Possible I can't solve an issue during class and need to resolve after class. --- # Starting RStudio > **Launch RStudio** - Should see 3 Panes - Console (left) - Environment (top-right) - Files and other tabs (bottom-right) --- # Working Directory - Working directory is the folder RStudio is currently using to import and export files > **Type `getwd()` into the console (bottom-left pane) and hit Enter** - R should return a file path, such as "/Home/Documents/" - When you tell R to look for or save a file, this is the location R is using --- # RStudio Projects - RStudio Projects are a good way to organize your work - Creates a folder for RStudio and sets it as the working directory - Places a `.Rproj` in the folder you can open to return to your project -- > **Create a project called "rlab1" in a location of your choice (I will demonstrate)** -- - All of our R Labs will begin with this process - R Demo 1 on eLC demonstrates this process in case you forget --- # Changing settings - Let's change some RStudio settings that should reduce the chance of errors > **Go to "Tools" in the RStudio menu bar at the top. Click on "Global Options" at the bottom of the menu.** > **Uncheck all boxes above "Other" and change the "Save workspace to .RData on exit" to Never.** --- # Loading Packages - Many functions in R require us to load a package - All assignments will include a list of packages you need to complete the work - The following code loads a package ```r library(name_of_package) ``` - We almost always need to load a package named `tidyverse` > **Run the following code in your console** ```r library(tidyverse) ``` --- # Installing packages - You must have a package installed in your version of RStudio in order to load it - If RStudio failed to load tidyverse, likely due to not having it installed (or some other package it requires). Error message will tell you a package "does not exist" - The following code will install a package - Make sure you include quotes around the name ```r install.packages("name_of_package") ``` --- # Loading vs. Installing packages - Every time you open RStudio, you need to re-load the packages you need - You only need to install a package once per version of RStudio - Only install packages using the console. Never install packages inside the R Markdown documents we will use for assignments. --- # Importing CSV data > **Download the file titled "states1.csv" from eLC. Leave it wherever your computer saved it (probably Downloads folder).** -- > **Provided tidyverse loaded successfully, run the following code in your console:** ```r states1 <- read_csv("states1.csv") ``` - You must use quotes anytime you tell R to load or save a file --- # A common error - Should now see an error message like the one below ```r Error in load(states1.RData) : object 'states1.csv' not found Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> load Execution halted ``` - R tries to tell you what went wrong in the error message. This says file (object) cannot be found. --- # Moving a file to our project - Remember, R looks for files in your project folder or your current working directory (`getwd()`) if not working in a project - R is saying it can't find the file we just told it to import because it is in our downloads folder, not our project folder -- > **Using your computer's file manager, move states1.csv to your project folder** > **Try to run `states1 <- read_csv("states1.csv")` again in your console** --- # Data in your R session - The environment pane (top-right) will list all data currently loaded - You should see `states1` in your environment pane - You can click the dropdown arrow to see a list of variables - Or click on `states1` to see the data in spreadsheet format --- # Starting an R Markdown Document - R Markdown: allows you to fluidly combine content and code to export to all sorts of things [video](https://rmarkdown.rstudio.com/lesson-1.html) - We will use R Markdown documents exclusively in this course -- > **Start an R Markdown (.Rmd) document. When the dialog box appears, click OK.** - By default, an Rmd loads with a template. The template information is informative for beginners. --- # Knit - Knit refers to taking the combination of text and code and knitting it into a HTML, PDF, Word doc, slides, etc. - Should see a knit button at the top of the editor pane. Clicking it will knit your document. - Every time you knit, R saves your Rmd file -- > **Knit your Rmd document. R will ask you to name it first. Name it "test".** --- # YAML header - The YAML header tells R how to format the output > **Change the YAML to the following:** ```r --- title: 'Test' author: 'Your Name' output: html_document --- ``` > **Knit to HTML again** --- # Code Chunks - Code chunks separate text from R code - To add a code chunk, you can use point-and-click or the keyboard shortcut `Cmd+Opt+I` on Mac, `Ctrl+Alt+I` on Windows -- > **At the bottom of your Rmd, insert a code chunk and add some arbitrary math inside it.** > **Click `Enter` a few times and type another line of math inside the same code chunk** --- # Running Code Chunks - Several options to run code in an Rmd - To run a particular code chunk, click on the play button. This runs ALL of the lines of code in the chunk. - To run a single line of code, place your cursor on that line and click `Cmd+Enter` on Mac or `Ctrl+Enter` on Windows - To run multiple code chunks, use the "Run" menu at the top of the editor pane --- # Creating an object in environment pane - Anytime you use the **assignment operator,** `<-`, the result will be saved in your environment pane. You already saved `states1` in your environment using `<-`. -- > **Insert another code chunk. Add and run the following code:** ```r states2 <- select(states1, state, pop_1990) ``` - Now we have two datasets. We can use states2 for whatever we needed it for while preserving states1 if we need that too. -- > **Knit your Rmd again. (this should cause an error)** --- # Another error - R cannot find function `select` when knitting Rmd. But it worked when we ran the single line of code. -- - When you knit, R runs the code in your Rmd from beginning-to-end and ignores anything you did in the console to load a package or import data. Your Rmd needs to be self-contained. - If a code chunk in your document depends on something you did in the console, it won't knit unless we add that code to the Rmd and in the correct order. -- > **In the first code chunk of your Rmd, add a new line of code: `library(tidyverse)`. Try to knit again.** --- # Another error - R can't find object `states1`, but `states1` is in our environment. -- - We imported `states1` using the console. It is in our environment. We can use it. - But there is no code in our Rmd document that imports `states1`. When we knit, we do not have `states1` imported -- > **Add `states1 <- read_csv("states1.csv")` in the first code chunk BELOW loading tidyverse (because read_csv depends on tidyverse). Try to knit again.** --- # Returning to a project > **Since you just knitted, everything is saved. Close RStudio.** > **To re-open your project, find the .Rproj file in your file manager and open it.** --- class: inverse, middle, center # Let's create our first RLab Rmd to upload to eLC --- # Set-up > **Download the file "rlab1_profile" from eLC and add to your project folder** -- > **Rename the file "firstname_lastname_profile" which you can do via your file manager (as with any file) or inside RStudio (I will demonstrate).** -- > **In your file pane, click on the file and it will open in the top-left pane.** > **Complete the instructions until you reach the section titled "Some of my R skills"** --- # Some of my R skills - Under "I know how to include and execute code chunks." > **Add a code chunk** > **Inside the code chunk, add some arbitrary arithmetic** > **Save the result of the arithmetic as an object named `math` using the assignment operator** --- # Some of my R skills - Under "I know how to load an R package." > **Add another code chunk** > **Write code that will load the tidyverse package** --- # Some of my R skills - Under "I know how to import a data set." > **Add another code chunk** > **Write code that will import states1.csv** --- # Some of my R skills - Under "I know one way to print a data set." > **Add another code chunk and add the following code** ```r states1 ``` > **Knit your Rmd.** --- # Upload to eLC > **Upload your Rmd file to the RLab1 assignment dropbox on eLC.**