class: title-slide, center, middle # Basics of R & RStudio --- background-image: url(images/rstudio_blank.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_editor.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_console.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_environment.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_plots.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_labelled.png) background-size: 900px # RStudio IDE -- <br><br><br><br><br><br><br><br><br><br><br><br><br><br><br><br> *** Let's take a look at the IDE in RStudio Cloud...
--- # Basic computing ```r # Addition 2 + 3 ``` ``` ## [1] 5 ``` ```r # Subtraction 2 - 3 ``` ``` ## [1] -1 ``` ```r # Multiplication 2 * 3 ``` ``` ## [1] 6 ``` ```r # Division 10/2 ``` ``` ## [1] 5 ``` --- # Basic computing ```r # Exponents 4^2 ``` ``` ## [1] 16 ``` ```r # Roots 4^(1/2) ``` ``` ## [1] 2 ``` -- *** Thankfully, R follows the order of operations (PEMDAS). ```r 2^3 + 4 * 1/2 ``` ``` ## [1] 10 ``` -- *** These values haven't been stored anywhere though... --- # Storing values in objects To keep a value in memory, we need to assign it to an **object**. -- To assign a value to an object, use the **assignment operator**. It looks like a left-pointing arrow: .center[ ### `<-` ] -- *** Let's use `<-` to create an object called x, that is assigned the number 8. ```r x <- 8 ``` -- *** Now we can call `x` by name, and it will print the value. ```r x ``` ``` ## [1] 8 ``` --- # Style Technically, both `<-` and `=` work as assignment operators. But we'll strictly use `<-` from here on out. -- *** This brings up a general coding principle of the day... -- **Style is important!** You want to use a consistent style so that others (including your future self) can easily and quickly read your code. -- I highly recommend following the [tidyverse style guide](https://style.tidyverse.org/). We'll talk more about the tidyverse tomorrow -- *** Hint: You will type `<-` a LOT. The keyboard shortcut `Alt`+`-` or `Option`+`-` can be used to insert a `<-`. --- # Storing values in objects Now, let's take a look at the variable `y`... -- ```r # call y y ``` ``` ## Error in eval(expr, envir, enclos): object 'y' not found ``` -- *** Whoops! We didn't assign anything to `y`. Calling a variable that doesn't exist leads to an **error message**. -- You'll see a LOT of error messages when using R. Don't worry when this happens. This is *100% normal*, and it happens to *everyone all the time*. -- *** ```r # Need to assign something to y! y <- 2 y ``` ``` ## [1] 2 ``` --- # Storing values in objects Now, we can use these variables in calculations -- Remember, `x <- 8` and `y <- 2` ```r # multiply them x * y ``` ``` ## [1] 16 ``` -- *** But remember, if we want to save the output of that calculation, we need to assign it to an object! ```r # define the product as z z <- x * y # print z z ``` ``` ## [1] 16 ``` --- # Naming variables You may be wondering how to name variables in R. There are just a few rules... -- 1. object names must start with a letter 2. can contain alphanumeric characters, "_", and "." 3. R is case sensitive, so `A` and `a` would be different variables -- *** This is another element of *style*. Using a consistent style when naming variables makes your life easier. There are several to choose from. --- # Naming variables .footnote[Artwork by [@allison_horst](https://twitter.com/allison_horst)] .pull-left[ ### Some examples + this_is_snake_case + thisIsCamelCase + avoid.using.periods I'll be using snake_case from here on, but you can change to another option if you'd like. ] .pull-right[ <img src="images/coding_cases.png" width="5335" /> ] --- class: inverse # Your turn 1
01
:
30
Open the file `your_turn.Rmd` inside the project `01_rstudio_basics` on RStudio Cloud. 1. Create a variable called `var_1` that is equal to 4. 1. Then create a variable called `var_2` that is `var_1` raised to the power of 6. Print the results (i.e. check what value is assigned to `var_2`). --- class: solution # Solution .panelset[ .panel[.panel-name[Q1] ```r # Q1. var_1 <- 4 ``` ] .panel[.panel-name[Q2] ```r # Q2. var_2 <- var_1^6 var_2 ``` ``` ## [1] 4096 ``` ] ] --- # R Markdown .footnote[Artwork by [@allison_horst](https://twitter.com/allison_horst)] R Markdown is a magical thing. It allows you to combine R code and plain text together in the same document. R Markdown documents end with the extension `.Rmd`. Let's take a look at how they work...
.center[ <img src="images/rmarkdown_wizards.png" width="70%" /> ] --- # R Markdown <img src="images/rmd_annotated.png" width="70%" /> .footnote[Image from [Kieran Healy](https://socviz.co/gettingstarted.html)] --- class: inverse # Your turn 2
02
:
00
1. Open the file `rmarkdown_example.Rmd` and click `knit`. Take a look at the resulting file called `rmarkdown_example.html`. 2. Go back to `rmarkdown_example.Rmd`. Change the title to `"[Your name]'s example R Markdown Document"` and re-knit. --- class: solution # Solution .panelset[ .panel[.panel-name[Q1] <img src="images/rmarkdown_example.png" width="40%" /> ] .panel[.panel-name[Q2] <img src="images/rmarkdown_example_title_yaml.png" width="40%" /> <img src="images/rmarkdown_example_title.png" width="40%" /> ] ] --- class: inverse, center, middle # Q & A
05
:
00
--- class: inverse, center, middle # Next up... ## Data Types & Structures --- class: inverse, center, middle # Break!
10
:
00