Bläddra i källkod

Update joins after refactoring

pull/6/head
Garrick Aden-Buie 7 år sedan
förälder
incheckning
052f542540
11 ändrade filer med 104 tillägg och 76 borttagningar
  1. +6
    -1
      R/00_base_join.R
  2. +3
    -2
      R/02_functions.R
  3. +37
    -30
      R/anti_join.R
  4. +7
    -6
      R/full_join.R
  5. +12
    -9
      R/inner_join.R
  6. +10
    -7
      R/left_join.R
  7. +3
    -2
      R/left_join_extra.R
  8. +10
    -8
      R/right_join.R
  9. +13
    -8
      R/semi_join.R
  10. +2
    -2
      R/union.R
  11. +1
    -1
      R/union_all.R

+ 6
- 1
R/00_base_join.R Visa fil

@@ -16,6 +16,11 @@ if (!getOption("tidy_verb_anim.functions_loaded", FALSE)) {

if (!dir.exists(here::here("images"))) dir.create(here::here("images"))

plot_data_join <- function(x, title = "", xlims = xlim(0.5, 5.5), ylims = ylim(-3.5, -0.5)) {
plot_data(x, title) +
xlims + ylims
}

# Data ----
x <- data_frame(
id = 1:3,
@@ -27,6 +32,6 @@ y <- data_frame(
y = paste0("y", (1:4)[-3])
)

initial_dfs <- proc_data(x, "x") %>%
initial_join_dfs <- proc_data(x, "x") %>%
bind_rows(mutate(proc_data(y, "y"), .x = .x + 3)) %>%
mutate(frame = 1)

+ 3
- 2
R/02_functions.R Visa fil

@@ -41,6 +41,7 @@ colorize_row_id <- function(df, n_colors, key_col = "id") {

plot_data <- function(x, title = "") {
if (!"alpha" %in% colnames(x)) x$alpha <- 1
if (!".width" %in% colnames(x)) x$`.width` <- 1
ggplot(x) +
aes(.x, .y, fill = color, label = value) +
geom_tile(aes(width = .width, alpha = alpha), color = "white", size = 3) +
@@ -54,9 +55,9 @@ plot_data <- function(x, title = "") {
guides(fill = FALSE)
}

animate_plot <- function(x) {
animate_plot <- function(x, transition_length = 2, state_length = 1) {
x +
transition_states(frame, transition_length = 2, state_length = 1) +
transition_states(frame, transition_length, state_length) +
enter_fade() +
exit_fade() +
ease_aes("sine-in-out")

+ 37
- 30
R/anti_join.R Visa fil

@@ -1,41 +1,48 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

step2 <- initial_dfs %>%
initial_join_dfs <- initial_join_dfs %>%
arrange(.x, .y) %>%
mutate(.obj = row_number(), .obj = .obj + 90 * as.integer(.id == "y"))

aj_step2 <- initial_join_dfs %>%
filter(.id == "x" | value %in% paste(1:2)) %>%
mutate(frame = 2,
.x = ifelse(.id == "y", 2.5, .x + 1.5),
alpha = case_when(
.x > 3 && .id == "x" ~ 0.5,
.y > -2.5 ~ 0.25,
TRUE ~ 1
))

aj_step3 <- aj_step2 %>%
filter(alpha == 1) %>%
mutate(frame = 3)

aj_step4 <- aj_step2 %>%
filter(alpha == 1) %>%
mutate(frame = 4, .y = -1)

aj <- bind_rows(
initial_join_dfs,
aj_step2,
aj_step3,
aj_step4
) %>%
mutate(
frame = 2,
.x = ifelse(.id == "y", 1, .x),
.x = .x + 1
)

step3 <- step2 %>%
filter(grepl("3", value)) %>%
ungroup() %>%
mutate(frame = 3, .y = -1)

aj <- initial_dfs %>%
mutate(removed = .id == "y", removed = as.integer(removed)) %>%
bind_rows(step2, step3) %>%
mutate(removed = ifelse(is.na(removed), 0, removed)) %>%
arrange(removed, .y, .x, desc(frame)) %>% #View()
mutate(alpha = case_when(
grepl("3", value) ~ 1,
frame == 2 & label == "id" ~ 0.25,
frame == 2 ~ 0.65,
TRUE ~ 1
)) %>%
{
plot_data(., "anti_join(x, y)") +
aes(alpha = alpha) +
scale_alpha_identity()
} %>%
animate_plot()
alpha = ifelse(is.na(alpha), 1, alpha),
.obj = ifelse(value == 4, 0, .obj)
) %>%
arrange(.obj, frame) %>%
plot_data("anti_join(x, y)") %>%
animate_plot(transition_length = c(2, 1, 2),
state_length = c(1, 0, 0, 1))

aj <- animate(aj)
anim_save(here::here("images", "anti-join.gif"), aj)

aj_g <- anti_join(x, y, by = "id") %>%
proc_data() %>%
plot_data("anti_join(x, y)")
mutate(.x = .x + 1.5) %>%
plot_data_join("anti_join(x, y)")

save_static_plot(aj_g, "anti-join")

+ 7
- 6
R/full_join.R Visa fil

@@ -1,17 +1,17 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

joined_df <- full_join(x, y, "id") %>%
fj_joined_df <- full_join(x, y, "id") %>%
proc_data("x") %>%
mutate(.id = ifelse(value %in% c("4", "y4"), "y", .id)) %>%
mutate(frame = 2, .x = .x + 1)

extra_blocks <- inner_join(x, y, "id") %>%
fj_extra_blocks <- inner_join(x, y, "id") %>%
select(id) %>%
proc_data("y") %>%
mutate(frame = 2, .x = .x + 1)

fj <- initial_dfs %>%
bind_rows(joined_df, extra_blocks) %>%
fj <- initial_join_dfs %>%
bind_rows(fj_joined_df, fj_extra_blocks) %>%
plot_data("full_join(x, y)") +
transition_states(frame, transition_length = 2, state_length = 1) +
enter_appear() +
@@ -23,6 +23,7 @@ anim_save(here::here("images", "full-join.gif"), fj)

fj_g <- full_join(x, y, "id") %>%
proc_data() %>%
plot_data("full_join(x, y)")
mutate(.x = .x + 1) %>%
plot_data_join("full_join(x, y)", ylims = ylim(-4.5, -0.5))

save_static_plot(fj_g, "full-join")

+ 12
- 9
R/inner_join.R Visa fil

@@ -1,18 +1,20 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

joined_df <- inner_join(x, y, "id")
joined_df <- bind_rows(
proc_data(joined_df, "x"),
proc_data(joined_df, "y")
ij_joined_df <- inner_join(x, y, "id")
ij_joined_df <- bind_rows(
proc_data(ij_joined_df, "x"),
proc_data(ij_joined_df, "y")
) %>%
filter(!(label == "x" & .id == "y") & !(label == "y" & .id == "x")) %>%
mutate(frame = 2, .x = .x + 1)

ij <- initial_dfs %>%
bind_rows(joined_df) %>%
ij <- bind_rows(
initial_join_dfs,
ij_joined_df
) %>%
mutate(removed = value %in% c("3", "4", "x3", "y4"),
removed = as.integer(removed)) %>%
arrange(desc(frame), removed) %>%
arrange(desc(frame), removed, desc(.id)) %>%
plot_data("inner_join(x, y)") %>%
animate_plot()

@@ -21,6 +23,7 @@ anim_save(here::here("images", "inner-join.gif"), ij)

ij_g <- inner_join(x, y, by = "id") %>%
proc_data() %>%
plot_data("inner_join(x, y)")
mutate(.x = .x + 1) %>%
plot_data_join("inner_join(x, y)")

save_static_plot(ij_g, "inner-join")

+ 10
- 7
R/left_join.R Visa fil

@@ -1,24 +1,27 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

joined_dfs <- left_join(x, y, "id") %>%
lj_joined_dfs <- left_join(x, y, "id") %>%
proc_data("x") %>%
mutate(frame = 2, .x = .x + 1)

extra_blocks <- inner_join(x, y, "id") %>%
lj_extra_blocks <- inner_join(x, y, "id") %>%
select(id) %>%
proc_data("y") %>%
mutate(frame = 2, .x = .x + 1)

lj <- initial_dfs %>%
bind_rows(joined_dfs) %>%
bind_rows(extra_blocks) %>%
lj <- bind_rows(
initial_join_dfs,
lj_joined_dfs,
lj_extra_blocks
) %>%
mutate(color = ifelse(is.na(value), "#ffffff", color)) %>%
arrange(value) %>%
plot_data("left_join(x, y)") %>%
animate_plot()

lj <- animate(lj)
anim_save(here::here("images", "left-join.gif"), lj)

lj_g <- plot_data(joined_dfs, "left_join(x, y)")
lj_g <- plot_data_join(lj_joined_dfs, "left_join(x, y)")

save_static_plot(lj_g, "left-join")

+ 3
- 2
R/left_join_extra.R Visa fil

@@ -1,4 +1,4 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

y_extra <- bind_rows(y, data_frame(id = 2, y = "y5"))

@@ -65,6 +65,7 @@ save_static_plot(g_input, "left-join-extra-input")

lj_g <- left_join(x, y_extra, by = "id") %>%
proc_data() %>%
plot_data("left_join(x, y)")
mutate(.x = .x + 1) %>%
plot_data_join("left_join(x, y)", ylims = ylim(-4.5, -0.5))

save_static_plot(lj_g, "left-join-extra")

+ 10
- 8
R/right_join.R Visa fil

@@ -1,28 +1,30 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

joined_dfs <- right_join(x, y, "id") %>%
rj_joined_dfs <- right_join(x, y, "id") %>%
proc_data("y") %>%
mutate(frame = 2, .x = .x + 1)

extra_blocks <- inner_join(x, y, "id") %>%
rj_extra_blocks <- inner_join(x, y, "id") %>%
select(id) %>%
proc_data("x") %>%
mutate(frame = 2, .x = .x + 1)

rj <- initial_dfs %>%
bind_rows(joined_dfs, extra_blocks) %>%
rj <- bind_rows(
initial_join_dfs,
rj_joined_dfs,
rj_extra_blocks
) %>%
filter(!is.na(value)) %>%
mutate(
.id = ifelse(label == "x", label, .id),
removed = as.integer(grepl("3", value))
) %>%
arrange(removed, .id, frame, value) %>%
arrange(removed, value, .id, frame) %>%
plot_data("right_join(x, y)") %>%
animate_plot()


rj <- animate(rj)
anim_save(here::here("images", "right-join.gif"), rj)

rj_g <- plot_data(joined_dfs, "right_join(x, y)")
rj_g <- plot_data(rj_joined_dfs, "right_join(x, y)")
save_static_plot(rj_g, "right-join")

+ 13
- 8
R/semi_join.R Visa fil

@@ -1,16 +1,20 @@
source(here::here("R/00_base.R"))
source(here::here("R/00_base_join.R"))

joined_df <- semi_join(x, y, "id") %>%
sj_joined_df <- semi_join(x, y, "id") %>%
proc_data("x") %>%
mutate(frame = 2, .x = .x + 1)
mutate(frame = 2, .x = .x + 1.5)

extra_blocks <- inner_join(x, y, "id") %>%
sj_extra_blocks <- inner_join(x, y, "id") %>%
select(id) %>%
proc_data("y") %>%
mutate(frame = 2, .x = .x + 1)
mutate(frame = 2, .x = .x + 1.5)

sj <- initial_dfs %>%
bind_rows(joined_df, extra_blocks) %>%
sj <- bind_rows(
initial_join_dfs,
sj_joined_df,
sj_extra_blocks
) %>%
arrange(value) %>%
plot_data("semi_join(x, y)") %>%
animate_plot()

@@ -20,6 +24,7 @@ anim_save(here::here("images", "semi-join.gif"), sj)
# Static Images
sj_g <- semi_join(x, y, "id") %>%
proc_data() %>%
plot_data("semi_join(x, y)")
mutate(.x = .x + 1.5) %>%
plot_data_join("semi_join(x, y)")

save_static_plot(sj_g, "semi-join")

+ 2
- 2
R/union.R Visa fil

@@ -32,11 +32,11 @@ uyx <- bind_rows(

uyx <- animate(uyx)

anim_save(here::here("images", "union.gif"), uyx)
anim_save(here::here("images", "union-rev.gif"), uyx)

uyx_g <- union(y, x) %>%
proc_data_set() %>%
mutate(.x = .x + 1.5) %>%
plot_data_set("union(x, y)", ylims = ylim(-0.5, -4.5))

save_static_plot(uyx_g, "union")
save_static_plot(uyx_g, "union-rev")

+ 1
- 1
R/union_all.R Visa fil

@@ -13,7 +13,7 @@ ua <- bind_rows(

ua <- animate(ua)

anim_save(here::here("images", "union_all.gif"), ua)
anim_save(here::here("images", "union-all.gif"), ua)

ua_g <- union_all(x, y) %>%
proc_data_set() %>%

Laddar…
Avbryt
Spara