class: title-slide, center, middle .top-left[ <img src="images/uo-logo2.png" width="100%" /> ] .top-right[ <img src="images/psy-logo.png" width="100%" /> ] # Basics of R, RStudio, & R Markdown UO R Bootcamp 2025 --- background-image: url(images/rstudio_blank2.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_editor2.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_console2.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_environment2.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_plots2.png) background-size: 900px # RStudio IDE --- background-image: url(images/rstudio_labelled2.png) background-size: 900px # RStudio IDE --- # 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 # modulo 20 %% 10 ``` ``` ## [1] 0 ``` -- ``` r # roots 4^(1/2) #or sqrt(4) ``` ``` ## [1] 2 ``` -- ``` r # absolute value abs(-3) ``` ``` ## [1] 3 ``` --- # Basic computing R follows the order of operations (PEMDAS). ``` r 2 + 2 * 3 ``` ``` ## [1] 8 ``` -- ``` r (2 + 2) * 3 ``` ``` ## [1] 12 ``` -- *** All of the values computed thus far haven't been stored anywhere though... --- # Storing values in objects To store a value, 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 assign the number 8 to an object called `x`. ``` 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. Note: We highly recommend following the [tidyverse style guide](https://style.tidyverse.org/). We'll talk more about the tidyverse tomorrow. ??? You want to use a consistent style so that others (including your future self) can easily and quickly read your code. -- *** Tip: 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 # print y y ``` ``` ## Error: 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. --- # Storing values in objects ``` r # assign 2 to y y <- 2 # print y y ``` ``` ## [1] 2 ``` --- # Storing values in objects Now, we can use these variables in calculations. -- *** Remember, `x <- 8` and `y <- 2`? We can multiply them: ``` r x * y ``` ``` ## [1] 16 ``` --- # Storing values in objects But, remember, if we want to save the output of that calculation, we need to assign it to an object! ``` r # assign the the product of x and y to 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 should start with a letter. 2. Object names can contain alphanumeric characters, underscores (`_`), and/or periods (`.`). 3. R is case sensitive, so `A` and `a` are different variables. --- # Naming variables .footnote[Artwork by [@allison_horst](https://twitter.com/allison_horst)] .pull-left[ ### Some examples + this_is_snake_case + thisIsCamelCase + maybe.you.like.periods I'll be using snake_case from here on out, but you can change to another option if you'd like. ] .pull-right[ <img src="images/coding_cases.png" width="5335" /> ] --- class: yourturn # Your turn 1
−
+
02
:
00
Open the `01_r_basics.Rmd` file. 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. --- class: solution # Solution .panelset[ .panel[.panel-name[Q1] ``` r var_1 <- 4 ``` ] .panel[.panel-name[Q2] ``` r var_2 <- var_1^6 var_2 ``` ``` ## [1] 4096 ``` ] ] --- # R Markdown .footnote[Artwork by [@allison_horst](https://twitter.com/allison_horst)] R Markdown 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: yourturn # Your turn 2
−
+
02
:
00
1. Go back to `01_r_basics.Rmd` and click `knit`. Take a look at the resulting file called `01_r_basics.html`. 2. Insert `author: [YOUR NAME]` after `title: "Basics of R, RStudio, & R Markdown"` but before `output: html_document`. Re-knit the document. --- class: solution # Solution .panelset[ .panel[.panel-name[Q1] <img src="images/rmarkdown_example.png" width="80%" /> ] .panel[.panel-name[Q2] <img src="images/rmarkdown_example_yaml.png" width="80%" /> <img src="images/rmarkdown_example_author.png" width="80%" /> ] ] --- class: yourturn, center, middle # Q & A
−
+
03
:
00
--- class: yourturn, center, middle # Break!
−
+
05
:
00