Data prep useful functions

Code
library(here)
library(tidyverse)
# library(targets)
library(sf)
library(gt)
library(kableExtra)
# library(lubridate)

clrs_light <- MetBrewer::met.brewer("Tiepolo") %>% colorspace::lighten(0.8)

# Generated via random.org
set.seed(2385)
Code


starwars <- readRDS(here("data","input_data","starwars.Rds"))
Note

In many cases

Here’s an overview of these variables:

starwars2 <- starwars |> 
  select(1, 4,5,8,10,11,12) |> 
  sample_n(10) |> 
  glimpse()
## Rows: 10
## Columns: 7
## $ name       <chr> "Wat Tambor", "BB8", "Jocasta Nu", "Owen Lars", "Tion Medon", "Poggle the Lesser", "Leia Organa", "…
## $ hair_color <chr> "none", "none", "white", "brown, grey", "none", "none", "brown", "black", "none", "brown"
## $ skin_color <chr> "green, grey", "none", "fair", "light", "grey", "green", "light", "brown", "mottled green", "fair"
## $ sex        <chr> "male", "none", "female", "male", "male", "male", "female", "male", "male", "male"
## $ homeworld  <chr> "Skako", NA, "Coruscant", "Tatooine", "Utapau", "Geonosis", "Alderaan", "Iridonia", "Cato Neimoidia…
## $ species    <chr> "Skakoan", "Droid", "Human", "Human", "Pau'an", "Geonosian", "Human", "Zabrak", "Neimodian", "Human"
## $ films      <list> "Attack of the Clones", "The Force Awakens", "Attack of the Clones", <"A New Hope", "Attack of the …

And here’s an extract of the data from June 11, 2020, the day that Argentina filed a derogation notice:

Code
starwars2 %>%
  gt() %>%
cols_width(name ~ px(60),
           homeworld ~ px(100),
           films ~ px(150),
           ) %>%
data_color(columns = contains("color"),
           fn = scales::col_factor(c(clrs_light[1], clrs_light[8]),
                                   domain = c(FALSE, TRUE))) %>%
opt_interactive(use_compact_mode = TRUE, use_highlight = TRUE)
## Warning: Some values were outside the color scale and will be treated as NA
## Some values were outside the color scale and will be treated as NA
Code
saveRDS(starwars2, here("data","output_data","starwars2.Rds"))