Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix printing for some tibbles #372

Merged
merged 5 commits into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 8 additions & 33 deletions R/multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ combine_pillar_distributions <- function(max_fit, min_fit_rev, tier_widths) {
#' 3. We consult the column offsets. The last column where the minimum assignment
#' has a greater or equal offset than the maximum assignment is our latest
#' cut point.
cut_point_candidate_idx <- which(max_fit$offset[cut_point_candidates] <= min_fit_rev$offset[cut_point_candidates])
cut_point_candidate_idx <- which(max_fit$offset_after[cut_point_candidates] <= min_fit_rev$offset_after[cut_point_candidates])
if (length(cut_point_candidate_idx) > 0) {
cut_point <- cut_point_candidates[max(cut_point_candidate_idx)]
} else {
Expand All @@ -435,7 +435,7 @@ combine_pillar_distributions <- function(max_fit, min_fit_rev, tier_widths) {
#' @aliases NULL
distribute_pillars <- function(widths, tier_widths) {
tier <- rep(NA_integer_, length(widths))
offset <- rep(NA_integer_, length(widths))
offset_after <- rep(NA_integer_, length(widths))
current_tier <- 1L
current_x <- 0L

Expand All @@ -458,11 +458,11 @@ distribute_pillars <- function(widths, tier_widths) {

tier[[i]] <- current_tier
current_x <- current_x + widths[[i]]
offset[[i]] <- current_x
offset_after[[i]] <- current_x
current_x <- current_x + 1L
}

data_frame(id = seq_along(widths), width = widths, tier = tier, offset = offset)
data_frame(id = seq_along(widths), tier = tier, width = widths, offset_after = offset_after)
}

distribute_pillars_rev <- function(widths, tier_widths) {
Expand All @@ -474,41 +474,16 @@ distribute_pillars_rev <- function(widths, tier_widths) {
splits <- unname(split(seq_along(tier), tier))
tier_widths <- tier_widths[seq_along(splits)]

new_offset <- unlist(map2(splits, tier_widths, function(.x, .y) {
offsets <- ret$offset[.x]
new_offset <- max(offsets) - offsets
new_offset - max(new_offset) + .y
new_offset_after <- unlist(map2(splits, tier_widths, function(.x, .y) {
new_offset_after <- cumsum(ret$width[.x] + 1)
new_offset_after - max(new_offset_after) + .y
}))

ret$offset <- c(new_offset, rep(NA_integer_, sum(is.na(tier))))
ret$offset_after <- c(new_offset_after, rep(NA_integer_, sum(is.na(tier))))

ret
}

distribute_pillars_offset <- function(widths, tier_widths,
widths_offset, tier_widths_offset) {
tier_widths <- tier_widths[seq2(tier_widths_offset, length(tier_widths))]
if (length(tier_widths) == 0) {
# Work around corner case
return(distribute_pillars(integer(), integer()))
}

widths <- widths[seq2(widths_offset, length(widths))]
fit_cut <- distribute_pillars(widths, tier_widths)
add_pillars_offset(fit_cut, widths_offset, tier_widths_offset)
}

add_pillars_offset <- function(fit_cut, widths_offset, tier_widths_offset) {
if (tier_widths_offset == 1) {
# Work around corner case
return(fit_cut)
}

fit_cut$id <- fit_cut$id + (widths_offset - 1L)
fit_cut$tier <- fit_cut$tier + (tier_widths_offset - 1L)
fit_cut
}

all_pillars_fit <- function(tier_df) {
rows <- nrow(tier_df)
rows == 0 || !safe_any_na(tier_df$tier[[nrow(tier_df)]])
Expand Down
222 changes: 132 additions & 90 deletions tests/testthat/_snaps/multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,151 +3,193 @@
Code
colonnade_compute_tiered_col_widths_df(rep(30, 4), rep(15, 4), rep(60, 4))
Output
id width tier offset
1 1 30 1 30
2 2 30 2 30
3 3 30 3 30
4 4 30 4 30
id tier width offset_after
1 1 1 30 30
2 2 2 30 30
3 3 3 30 30
4 4 4 30 30
Code
colonnade_compute_tiered_col_widths_df(rep(30, 4), rep(15, 4), rep(60, 3))
Output
id width tier offset max_widths
1 1 30 1 30 30
2 2 30 2 30 30
3 3 30 3 30 30
4 4 15 3 60 30
id tier width offset_after max_widths
1 1 1 30 30 30
2 2 2 30 30 30
3 3 3 30 30 30
4 4 3 15 60 30
Code
colonnade_compute_tiered_col_widths_df(rep(30, 4), rep(15, 4), rep(60, 2))
Output
id width tier offset max_widths
1 1 30 1 30 30
2 2 15 2 28 30
3 3 15 2 44 30
4 4 15 2 60 30
id tier width offset_after max_widths
1 1 1 30 30 30
2 2 2 15 28 30
3 3 2 15 44 30
4 4 2 15 60 30
Code
colonnade_compute_tiered_col_widths_df(rep(30, 5), rep(15, 5), rep(60, 3))
Output
id width tier offset max_widths
1 1 30 1 30 30
2 2 30 2 30 30
3 3 15 3 28 30
4 4 15 3 44 30
5 5 15 3 60 30
id tier width offset_after max_widths
1 1 1 30 30 30
2 2 2 30 30 30
3 3 3 15 28 30
4 4 3 15 44 30
5 5 3 15 60 30
Code
colonnade_compute_tiered_col_widths_df(rep(30, 5), rep(15, 5), rep(60, 4))
Output
id width tier offset max_widths
1 1 30 1 30 30
2 2 30 2 30 30
3 3 30 3 30 30
4 4 30 4 30 30
5 5 15 4 60 30
id tier width offset_after max_widths
1 1 1 30 30 30
2 2 2 30 30 30
3 3 3 30 30 30
4 4 4 30 30 30
5 5 4 15 60 30
Code
colonnade_compute_tiered_col_widths_df(rep(30, 10), rep(15, 10), rep(60, 5))
Output
id width tier offset max_widths
1 1 30 1 30 30
2 2 30 2 30 30
3 3 30 3 30 30
4 4 15 3 60 30
5 5 15 4 28 30
6 6 15 4 44 30
7 7 15 4 60 30
8 8 15 5 28 30
9 9 15 5 44 30
10 10 15 5 60 30
id tier width offset_after max_widths
1 1 1 30 30 30
2 2 2 30 30 30
3 3 3 30 30 30
4 4 3 15 60 30
5 5 4 15 28 30
6 6 4 15 44 30
7 7 4 15 60 30
8 8 5 15 28 30
9 9 5 15 44 30
10 10 5 15 60 30
Code
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 21)
Output
id tier width offset_after
1 1 1 5 5
2 2 1 5 11
3 3 NA 10 NA
Code
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 22)
Output
id tier width offset_after max_widths
1 1 1 5 5 6
2 2 1 5 11 5
3 3 1 10 22 10
Code
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 23)
Output
id tier width offset_after
1 1 1 6 6
2 2 1 5 12
3 3 1 10 23
Code
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 21)
Output
id tier width offset_after
1 1 1 5 5
2 2 1 5 11
3 3 NA 10 NA
Code
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 22)
Output
id tier width offset_after max_widths
1 1 1 5 5 5
2 2 1 5 11 6
3 3 1 10 22 10
Code
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 23)
Output
id tier width offset_after
1 1 1 5 5
2 2 1 6 12
3 3 1 10 23

# distribute_pillars()

Code
distribute_pillars(1:3, 10)
Output
id width tier offset
1 1 1 1 1
2 2 2 1 4
3 3 3 1 8
id tier width offset_after
1 1 1 1 1
2 2 1 2 4
3 3 1 3 8
Code
distribute_pillars(1:3, 5)
Output
id width tier offset
1 1 1 1 1
2 2 2 1 4
3 3 3 NA NA
id tier width offset_after
1 1 1 1 1
2 2 1 2 4
3 3 NA 3 NA
Code
distribute_pillars(1:3, c(5, 5))
Output
id width tier offset
1 1 1 1 1
2 2 2 1 4
3 3 3 2 3
id tier width offset_after
1 1 1 1 1
2 2 1 2 4
3 3 2 3 3
Code
distribute_pillars(1:5, 7:9)
Output
id width tier offset
1 1 1 1 1
2 2 2 1 4
3 3 3 2 3
4 4 4 2 8
5 5 5 3 5
id tier width offset_after
1 1 1 1 1
2 2 1 2 4
3 3 2 3 3
4 4 2 4 8
5 5 3 5 5
Code
distribute_pillars(3:5, 8:10)
Output
id width tier offset
1 1 3 1 3
2 2 4 1 8
3 3 5 2 5
id tier width offset_after
1 1 1 3 3
2 2 1 4 8
3 3 2 5 5
Code
distribute_pillars(5:3, 9:8)
Output
id width tier offset
1 1 5 1 5
2 2 4 2 4
3 3 3 2 8
id tier width offset_after
1 1 1 5 5
2 2 2 4 4
3 3 2 3 8

# distribute_pillars_rev()

Code
distribute_pillars_rev(1:3, 10)
Output
id width tier offset
1 1 1 1 5
2 2 2 1 7
3 3 3 1 10
id tier width offset_after
1 1 1 1 3
2 2 1 2 6
3 3 1 3 10
Code
distribute_pillars_rev(1:3, 5)
Output
id width tier offset
1 1 1 NA 5
2 2 2 NA NA
3 3 3 1 NA
id tier width offset_after
1 1 NA 1 5
2 2 NA 2 NA
3 3 1 3 NA
Code
distribute_pillars_rev(1:3, c(5, 5))
Output
id width tier offset
1 1 1 1 3
2 2 2 1 5
3 3 3 2 5
id tier width offset_after
1 1 1 1 2
2 2 1 2 5
3 3 2 3 5
Code
distribute_pillars_rev(1:5, 7:9)
Output
id width tier offset
1 1 1 1 5
2 2 2 1 7
3 3 3 2 4
4 4 4 2 8
5 5 5 3 9
id tier width offset_after
1 1 1 1 4
2 2 1 2 7
3 3 2 3 3
4 4 2 4 8
5 5 3 5 9
Code
distribute_pillars_rev(3:5, 8:10)
Output
id width tier offset
1 1 3 2 8
2 2 4 3 4
3 3 5 3 9
id tier width offset_after
1 1 2 3 8
2 2 3 4 3
3 3 3 5 9
Code
distribute_pillars_rev(5:3, 9:8)
Output
id width tier offset
1 1 5 1 9
2 2 4 2 3
3 3 3 2 8
id tier width offset_after
1 1 1 5 9
2 2 2 4 4
3 3 2 3 8

6 changes: 6 additions & 0 deletions tests/testthat/test-multi.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ test_that("colonnade_compute_tiered_col_widths_df()", {
colonnade_compute_tiered_col_widths_df(rep(30, 5), rep(15, 5), rep(60, 3))
colonnade_compute_tiered_col_widths_df(rep(30, 5), rep(15, 5), rep(60, 4))
colonnade_compute_tiered_col_widths_df(rep(30, 10), rep(15, 10), rep(60, 5))
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 21)
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 22)
colonnade_compute_tiered_col_widths_df(c(6, 5, 10), c(5, 5, 10), 23)
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 21)
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 22)
colonnade_compute_tiered_col_widths_df(c(5, 6, 10), c(5, 5, 10), 23)
})
})

Expand Down