+ - 0:00:00
Notes for current slide
Notes for next slide

> 👨‍🎨
>   the programming of aRt

danielle navarro

`

1

`

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
`

2




Values and Vectors

`

From the art class...

`

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
`

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)
`

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)

Two sides to infinity

  • Inf is positive infinity (e.g., 1/0 is Inf)
  • -Inf is negative infinity (e.g., -1/0 is -Inf)
`

Topic:

  • vectors with c(), numeric
  • vectors with c(), character
  • get one using [ ]
  • get two using [ ]
  • negative indices drop items
  • elementwise operations
  • the recycling rule
`

Topic:

  • vectors with c(), numeric
  • vectors with c(), character
  • get one using [ ]
  • get two using [ ]
  • negative indices drop items
  • elementwise operations
  • the recycling rule

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)
`

3




Class and Coercion

`

From the art class...

`

Topic:

  • objects have a class
  • possibly multiple classes
  • you can coerce objects...
`

Topic:

  • objects have a class
  • possibly multiple classes
  • you can coerce objects...

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
`

4







  Logical
  Operations

`

Operator:

  • equal to, ==
  • less than, <
  • less than or equal to, <=
`

Operator:

  • equal to, ==
  • less than, <
  • less than or equal to, <=

Example:

  • "cat" == "cat" is TRUE
  • 1 < 10 is TRUE
  • 10 <= 10 is TRUE
`

Operator:

  • and, &
  • or, |
  • not, !
`

Operator:

  • and, &
  • or, |
  • not, !

Example:

  • (1+1 == 2) & (2+1 == 2) is FALSE
  • (1+1 == 2) | (2+1 == 2) is TRUE
  • !TRUE is FALSE
`

5




  Lists...

`

From the art class...

`

Lists are "containers" for arbitrary objects

art_par <- list(
seed = c(2, 10, 19),
n_paths = 100,
n_steps = 20,
palette = "bilbao",
save = TRUE
)
`

Lists are "containers" for arbitrary objects

art_par <- list(
seed = c(2, 10, 19),
n_paths = 100,
n_steps = 20,
palette = "bilbao",
save = TRUE
)
## $seed
## [1] 2 10 19
##
## $n_paths
## [1] 100
##
## $n_steps
## [1] 20
##
## $palette
## [1] "bilbao"
##
## $save
## [1] TRUE
`

6







  ... Listish
  Things?

`

Tibbles (data frames) are secretly lists

list(
pet = c("cat", "dog"),
count = c(3, 1)
)
## $pet
## [1] "cat" "dog"
##
## $count
## [1] 3 1
`

Tibbles (data frames) are secretly lists

list(
pet = c("cat", "dog"),
count = c(3, 1)
)
## $pet
## [1] "cat" "dog"
##
## $count
## [1] 3 1
tibble(
pet = c("cat", "dog"),
count = c(3, 1)
)
## # A tibble: 2 x 2
## pet count
## <chr> <dbl>
## 1 cat 3
## 2 dog 1
`

Tibbles (data frames) are secretly lists

list(
pet = c("cat", "dog"),
count = c(3, 1)
)
## $pet
## [1] "cat" "dog"
##
## $count
## [1] 3 1
data.frame(
pet = c("cat", "dog"),
count = c(3, 1)
)
## pet count
## 1 cat 3
## 2 dog 1
`

7




  Loops

`

iterate code WHILE some condition remains true

value <- 0
while(value < .8) {
value <- runif(n = 1)
print(value)
}
`

iterate code WHILE some condition remains true

value <- 0
while(value < .8) {
value <- runif(n = 1)
print(value)
}
## [1] 0.3977455
## [1] 0.1156978
## [1] 0.06974868
## [1] 0.2437494
## [1] 0.7920104
## [1] 0.3400624
## [1] 0.9720625
`

iterate code FOR each element in a vector

pet <- c("bat","cat","dog")
for(p in pet) {
msg <- str_c(
"------", p, "------"
)
print(msg)
}
`

iterate code FOR each element in a vector

pet <- c("bat","cat","dog")
for(p in pet) {
msg <- str_c(
"------", p, "------"
)
print(msg)
}
## [1] "------bat------"
## [1] "------cat------"
## [1] "------dog------"
`

8







        If

`

Executing code IF a condition is true

pet <- c("bat","cat","dog")
for(p in pet) {
if(p == "cat") {
msg <- str_c("A ", p, " is the best!!!")
}
}
print(msg)
`

Executing code IF a condition is true

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!!!"
`

Executing code IF a condition is true

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)
}
`

Executing code IF a condition is true

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"
`








Thanks!

`

1

`
Paused

Help

Keyboard shortcuts

, , Pg Up, k Go to previous slide
, , Pg Dn, Space, j Go to next slide
Home Go to first slide
End Go to last slide
Number + Return Go to specific slide
b / m / f Toggle blackout / mirrored / fullscreen mode
c Clone slideshow
p Toggle presenter mode
t Restart the presentation timer
?, h Toggle this help
Esc Back to slideshow