class: center, middle, inverse, title-slide # > 05 👨🎨
> the programming of aRt ## 🔗
robust-tools.djnavarro.net
### danielle navarro --- layout: true <div class="my-footer"> <span> <a href="https://robust-tools.djnavarro.net" target="_blank">robust-tools.djnavarro.net</a> </span> </div> --- class: middle background-image: url("img/scrawl_2-500-80-200-70.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[1] ] .pull-right-wide[ <!-- <br><br><br><br><br><br><br><br><br><br> --> <!-- .larger[🎨] --> <!-- <br><br><br><br><br><br> --> <!-- .larger[.embolden[Scrawl]] --> ] --- class: inverse .hand[Key concepts] - lists, tibbles and the `$` operator - generating pseudorandom numbers - how to use a `while` loop - how to use an `if` statement - making vectors with `c()` - finessing your pipelines with `.` - "dplyr" and "ggplot2" revision/extension - introducing the "here" package - a teaser for the "ambient" package --- class: middle, inverse .hand[Our studio...] ## https://rstudio.cloud/project/1112041 --- class: middle background-image: url("img/scrawl_10-1000-400-100-5.png") background-size: cover opacity: .4 .pull-left-narrow[ .huge-hotpink-number[2] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Values and Vectors]] ] --- class: middle .hand[From the art class...] <img src="img/scr_vector_annotated.png" width="782" /> --- class: inverse .hand[Four types of value] - **numeric** values: `1.53`, `11.6`, `1.0`, etc - **integer** values: `0L`, `1L`, `2L`, `100L`, etc - **character** values: `"pride"`, `"and"`, `"prejudice"`, - **logical** values: `TRUE`, `FALSE` --- class: inverse .hand[Three kinds of "absence" for a value] - `NA` means "information not available" (e.g., missing data) - `NULL` means "programming variable doesn't exist" - `NaN` means "not a number" (e.g., `0/0` is `NaN`) -- .hand[Two sides to infinity] - `Inf` is positive infinity (e.g., `1/0` is `Inf`) - `-Inf` is negative infinity (e.g., `-1/0` is `-Inf`) --- class: inverse .pull-leftish[ .hand[Topic:] - vectors with `c()`, numeric - vectors with `c()`, character - get one using `[ ]` - get two using `[ ]` - negative indices drop items - elementwise operations - the recycling rule ] -- .pull-rightish[ .hand[Example:] - `fib <- c(1,1,2,3,5,8)` - `pet <- c("bat","cat","dog")` - `pet[3]` is `"dog"` - `pet[c(1,3)]` is `c("bat","dog")` - `pet[-2]` is `c("bat","dog")` - `fib + 1` is `c(2,2,3,4,6,9)` - `fib + c(0,1)` is `c(1,2,2,4,5,9)` ] --- class: middle background-image: url("img/scrawl_11-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[3] ] .pull-right-wide[ <br><br><br> .larger[.embolden[Class and Coercion]] ] --- class: middle .hand[From the art class...] <img src="img/scr_mixed_annotated.png" width="428" /> --- class: inverse .pull-leftish[ .hand[Topic:] - objects have a **class** - possibly multiple classes - you can coerce objects... ] -- .pull-rightish[ .hand[Example:] - `class(fib)` is `"numeric"` - `class(mpg)` shows three classes - `as.character(3)` is `"3"` - `as.character(TRUE)` is `"TRUE"` - `as.numeric(TRUE)` is `1` - `as.numeric("cat")` is `NA` ] --- class: middle background-image: url("img/scrawl_17-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[4] ] .pull-right-wide[ <br><br><br><br><br><br> .larger[.embolden[ Logical<br> Operations<br>]] ] --- class: inverse .pull-left-narrow[ .hand[Operator:] - equal to, `==` - less than, `<` - less than or equal to, `<=` ] -- .pull-right-wide[ .hand[Example:] - `"cat" == "cat"` is `TRUE` - `1 < 10` is `TRUE` - `10 <= 10` is `TRUE` ] --- class: inverse .pull-left-narrow[ .hand[Operator:] - and, `&` - or, `|` - not, `!` ] -- .pull-right-wide[ .hand[Example:] - `(1+1 == 2) & (2+1 == 2)` is `FALSE` - `(1+1 == 2) | (2+1 == 2)` is `TRUE` - `!TRUE` is `FALSE` ] --- class: middle background-image: url("img/scrawl_12-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[5] ] .pull-right-wide[ <br><br><br> .larger[.embolden[ Lists...]] ] --- class: middle .hand[From the art class...] <img src="img/scr_artpar_annotated.png" width="631" /> --- class: inverse .hand[Lists are "containers" for arbitrary objects] .pull-left[ ```r art_par <- list( seed = c(2, 10, 19), n_paths = 100, n_steps = 20, palette = "bilbao", save = TRUE ) ``` ] -- .pull-right[ ``` ## $seed ## [1] 2 10 19 ## ## $n_paths ## [1] 100 ## ## $n_steps ## [1] 20 ## ## $palette ## [1] "bilbao" ## ## $save ## [1] TRUE ``` ] --- class: middle background-image: url("img/scrawl_13-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[6] ] .pull-right-wide[ <br><br><br><br><br><br> .larger[.embolden[ ... Listish<br> Things?]] ] --- class: inverse .hand[Tibbles (data frames) are secretly lists] .pull-left[ ```r list( pet = c("cat", "dog"), count = c(3, 1) ) ``` ``` ## $pet ## [1] "cat" "dog" ## ## $count ## [1] 3 1 ``` ] -- .pull-right[ ```r tibble( pet = c("cat", "dog"), count = c(3, 1) ) ``` ``` ## # A tibble: 2 x 2 ## pet count ## <chr> <dbl> ## 1 cat 3 ## 2 dog 1 ``` ] --- class: inverse .hand[Tibbles (data frames) are secretly lists] .pull-left[ ```r list( pet = c("cat", "dog"), count = c(3, 1) ) ``` ``` ## $pet ## [1] "cat" "dog" ## ## $count ## [1] 3 1 ``` ] .pull-right[ ```r data.frame( pet = c("cat", "dog"), count = c(3, 1) ) ``` ``` ## pet count ## 1 cat 3 ## 2 dog 1 ``` ] --- class: middle background-image: url("img/scrawl_14-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[7] ] .pull-right-wide[ <br><br><br> .larger[.embolden[ Loops]] ] --- class: inverse .hand[iterate code WHILE some condition remains true] .pull-left[ ```r value <- 0 while(value < .8) { value <- runif(n = 1) print(value) } ``` ] -- .pull-right[ ``` ## [1] 0.3977455 ## [1] 0.1156978 ## [1] 0.06974868 ## [1] 0.2437494 ## [1] 0.7920104 ## [1] 0.3400624 ## [1] 0.9720625 ``` ] --- class: inverse .hand[iterate code FOR each element in a vector] .pull-left[ ```r pet <- c("bat","cat","dog") for(p in pet) { msg <- str_c( "------", p, "------" ) print(msg) } ``` ] -- .pull-right[ ``` ## [1] "------bat------" ## [1] "------cat------" ## [1] "------dog------" ``` ] --- class: middle background-image: url("img/scrawl_15-1000-400-100-5.png") background-size: cover .pull-left-narrow[ .huge-hotpink-number[8] ] .pull-right-wide[ <br><br><br><br><br><br> .larger[.embolden[ If]] ] --- class: inverse .hand[Executing code IF a condition is true] ```r pet <- c("bat","cat","dog") for(p in pet) { if(p == "cat") { msg <- str_c("A ", p, " is the best!!!") } } print(msg) ``` -- ``` ## [1] "A cat is the best!!!" ``` --- class: inverse .hand[Executing code IF a condition is true] ```r pet <- c("bat","cat","dog") for(p in pet) { if(p == "cat") { msg <- str_c("A ", p, " is the best!!!") } else { msg <- str_c("A ", p, " is okay I guess") } print(msg) } ``` -- ``` ## [1] "A bat is okay I guess" ## [1] "A cat is the best!!!" ## [1] "A dog is okay I guess" ``` --- background-image: url("img/scrawl_15-10000-400-100-5.png") background-size: cover <br><br><br><br> <br><br><br> .pull.right[.hand[.larger[Thanks!]]]