[[1]]
[1] "apple"
[[2]]
[1] "orange"
[[3]]
[1] "banana"
2025-03-11
How can we make our life simpler and avoid massive code duplication?
.data
and .env
We can use pronouns to distinguish data columns from variables:
.data$species
is a column in the data frame
.env$species
is a variable in the local environment
This concept is also called: Avoiding magic numbers
make_plot <- function(species) {
penguins |>
filter(.data$species == .env$species) |>
ggplot() +
aes(bill_length_mm, body_mass_g) +
geom_point() +
ggtitle(glue("Species: {species}")) +
xlab("bill length (mm)") +
ylab("body mass (g)") +
theme_minimal_grid() +
theme(plot.title.position = "plot")
}
make_plot("Adelie")
make_plot <- function(species) {
penguins |>
filter(.data$species == .env$species) |>
ggplot() +
aes(bill_length_mm, body_mass_g) +
geom_point() +
ggtitle(glue("Species: {species}")) +
xlab("bill length (mm)") +
ylab("body mass (g)") +
theme_minimal_grid() +
theme(plot.title.position = "plot")
}
make_plot("Chinstrap")
make_plot <- function(species) {
penguins |>
filter(.data$species == .env$species) |>
ggplot() +
aes(bill_length_mm, body_mass_g) +
geom_point() +
ggtitle(glue("Species: {species}")) +
xlab("bill length (mm)") +
ylab("body mass (g)") +
theme_minimal_grid() +
theme(plot.title.position = "plot")
}
make_plot("Gentoo")
We need a brief detour to talk about lists and the map()
pattern
In R, lists are a data structure that can store multiple elements of various types
In R, lists are a data structure that can store multiple elements of various types
In R, lists are a data structure that can store multiple elements of various types
In R, lists are a data structure that can store multiple elements of various types
In R, lists are a data structure that can store multiple elements of various types
map()
patternThe map()
function applies a function to all elements of a vector or list and returns the result in a list
This pattern can be used instead of loops
map()
patternWe can define the function to be applied on the fly:
map()
patternmap()
patternSimilarly:
map_chr()
returns a vector of stringsmap_int()
returns a vector of integersmap_lgl()
returns a vector of logicalsmap
takes each element of the vector species
and uses it as input for make_plot()
map
takes each element of the vector species
and uses it as input for make_plot()
map
takes each element of the vector species
and uses it as input for make_plot()
make_plot <- function(species) {
penguins |> # hard-coded dataset!
filter(.data$species == .env$species) |>
ggplot() +
aes(bill_length_mm, body_mass_g) +
geom_point() +
ggtitle(glue("Species: {species}")) +
xlab("bill length (mm)") +
ylab("body mass (g)") +
theme_minimal_grid() +
theme(plot.title.position = "plot")
}
make_plot2 <- function(data, species) {
data |>
# filter no longer needed
ggplot() +
aes(bill_length_mm, body_mass_g) +
geom_point() +
ggtitle(glue("Species: {species}")) +
xlab("bill length (mm)") +
ylab("body mass (g)") +
theme_minimal_grid() +
theme(plot.title.position = "plot")
}
data_adelie <- penguins |>
filter(species == "Adelie")
make_plot2(data_adelie, species = "Adelie")
map2()
is like map()
but for functions with 2 arguments
Note: This pipeline automatically processes all species in the dataset, whatever they are called
for
loops?for
loops