thisyear <- 2024Class 2
Foundations in R and RStudio
RGui
R doesn’t require RStudio - we can use the Rgui! But it’s easier to use a more comprehensive IDE.
R is an interpreted language - we can have a “conversation” with R using the console. R is usually used via the console, or by writing the same code into scripts which allow you to save the conversation for future use.
Why have the conversation in R instead of English, or another natural language? For one, natural languages are ambiguous! R, like other programming languages, is not.
Interpreted languages like R are different from compiled languages like C++ or Java, where you usually write programs in code and then compile them into an application (app) or “executable”. Think of those more like creating books, rather than conversations.
If you aren’t able to use RStudio on your computer (temporarily), you can try to use Posit Cloud, but you will not be able to work with files in the same way on your computer.
Join our class Posit Cloud Workspace (linked on Canvas) using Google to login with your UM account for future use (or if you do not have a working install on your computer currently).
Working interactively with the console
Open up RStudio. We’ll get acquainted with the different areas first and then talk to the R console.
Check out and customize your Tools > Global Options.
Also check out the Session menu and the different panels. Each R session is like a separate conversation with R, and anything remembered in that session will be shown in the environment panel. You can ask R to remember something by using the assignment operator <- (or, technically but not recommended, =).
Try out assignment:
R doesn’t care about spacing or indentation (as opposed to languages like Python). Indentation is usually used to assist with human readability.
Getting Ready to Script
Now create a new file. Save it as class2.R somewhere on your computer where you’ll find it later (we’ll talk more about organizing files soon).
The code below will go in your new R script file.
Installing some packages
Packages are collections of R functions that help to solve problems and also provide access to datasets. We’re going to install two packages for now - tidyverse and babynames. You can do this from the Tools menu, or with the following code (uncomment by removing the hashtag at the beginning):
# anything on a line following a hashtag is a "comment" - these are not run as code
#install.packages("tidyverse")
#install.packages("babynames")Loading packages
library(babynames)
library(tidyverse)When you run these, you will see some output in your console. This is normal!
Various kinds of messages show up in the console - some are errors, but most are feedback, messages, and warnings.