diff --git a/images/embed_jpg.R b/images/embed_jpg.R new file mode 100644 index 0000000..ce35b08 --- /dev/null +++ b/images/embed_jpg.R @@ -0,0 +1,28 @@ +is_latex <- function() { + identical(knitr::opts_knit$get('rmarkdown.pandoc.to'), "latex") +} + +embed_jpg <- function(path, dpi) { + dim <- jpg_dim(path) + + if (is_latex()) { + width <- round(dim[2] / dpi, 2) + + knitr::asis_output(paste0( + "\\includegraphics[", + "width=", width, "in", + "]{", path, "}" + )) + } else { + knitr::asis_output(paste0( + "" + )) + } +} + +jpg_dim <- function(path) { + dim(jpeg::readJPEG(path, native = TRUE)) +} diff --git a/images/pepper-1.jpg b/images/pepper-1.jpg new file mode 100644 index 0000000..f95130a Binary files /dev/null and b/images/pepper-1.jpg differ diff --git a/images/pepper-2.jpg b/images/pepper-2.jpg new file mode 100644 index 0000000..cd69ace Binary files /dev/null and b/images/pepper-2.jpg differ diff --git a/images/pepper-3.jpg b/images/pepper-3.jpg new file mode 100644 index 0000000..e8c4a77 Binary files /dev/null and b/images/pepper-3.jpg differ diff --git a/images/pepper.jpg b/images/pepper.jpg new file mode 100644 index 0000000..c974d3a Binary files /dev/null and b/images/pepper.jpg differ diff --git a/lists.Rmd b/lists.Rmd index 649712e..87d8e15 100644 --- a/lists.Rmd +++ b/lists.Rmd @@ -1,6 +1,6 @@ --- layout: default -title: String manipulation +title: List manipulation output: bookdown::html_chapter --- @@ -8,27 +8,27 @@ output: bookdown::html_chapter library(purrr) set.seed(1014) options(digits = 3) +source("images/embed_jpg.R") ``` # Lists -In this chapter, you'll learn how to handle lists, R's primarily hierarchical data structure. Lists are sometimes called recursive data structures, because they're one of the few datastructures in R than can contain themselves; a list can have a list as a child. +In this chapter, you'll learn how to handle lists, the data structure R uses for complex, hierarchical objects. You've already familiar with vectors, R's data structure for 1d objects. Lists extend these ideas to model objects that are like trees. Lists allow you to do this because unlike vectors, a list can contain other lists. -If you've worked with list-like objects in other environments, you're probably familiar with the for-loop. We'll discuss for loops a little here, but we'll mostly focus on a number functions from the __purrr__ package. The purrr package is designed to make it easy to work with lists by taking care of the details and allowing you to focus on the specific transformation, not the generic boilerplate. +If you've worked with list-like objects before, you're probably familiar with the for loop. I'll talk a little bit about for loops here, but the focus will be functions from the __purrr__ package. purrr makes it easier to work with lists by eliminating common for loop boilerplate so you can focus on the specific details. This is the same idea as the apply family of functions in base R (`apply()`, `lapply()`, `tapply()`, etc), but purrr is more consistent and easier to learn. -The goal is to allow you to think only about: +The goal of using purrr functions instead of for loops is to allow you break common list manipulation challenges into independent pieces: -1. Each element of the list in isolate. You need to figure out how to - manipulate a single element of the list; purrr takes care of generalising - that to every element in the list. +1. How can you solve the problem for a single element of the list? Once + you've solved that problem, purrr takes care of generalising your + solution to every element in the list. -1. How do you move that element a small step towards your final goal. - Purrr provides lots of small pieces that you compose together to - solve complex problems. +1. If you're solving a complex problem, how can you break it down into + bite sized pieces that allow you to advance one small step towards a + solution? With purrr, you get lots of small pieces that you can + combose together with the pipe. -Together, these features allow you to tackle complex problems by dividing them up into bite size pieces. The resulting code is easy to understand when you re-read it in the future. - -Many of the functions in purrr have equivalent in base R. We'll provide you with a few guideposts into base R, but we'll focus on purrr because its functions are more consistent and have fewer surprises. +This structure makes it easier to solve new problems. It also makes it easier to understand your solutions to old problems when you re-read your old code.