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

Add n/max_extra_cols/max_footer_lines args to print.dtplyr_step #466

Merged
merged 3 commits into from
Feb 19, 2024
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
15 changes: 13 additions & 2 deletions R/step.R
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,11 @@ pull.dtplyr_step <- function(.data, var = -1, name = NULL, ...) {
}

#' @export
print.dtplyr_step <- function(x, ...) {
print.dtplyr_step <- function(x,
...,
n = 6,
max_extra_cols = NULL,
max_footer_lines = NULL) {
dt <- as.data.table(x)

cat_line(cli::style_bold("Source: "), "local data table ", dplyr::dim_desc(dt))
Expand All @@ -193,7 +197,14 @@ print.dtplyr_step <- function(x, ...) {
cat_line(cli::style_bold("Call: "), expr_text(dt_call(x)))
}
cat_line()
cat_line(format(as_tibble(dt, .name_repair = "minimal"), n = 6)[-1]) # Hack to remove "A tibble" line
cat_line(
format(
as_tibble(dt, .name_repair = "minimal"),
n = n,
max_extra_cols = max_extra_cols,
max_footer_lines = max_footer_lines
)[-1] # Hack to remove "A tibble" line
)
cat_line()
cat_line(cli::col_silver(
"# Use as.data.table()/as.data.frame()/as_tibble() to access results"
Expand Down
56 changes: 56 additions & 0 deletions tests/testthat/_snaps/step.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,3 +56,59 @@

# Use as.data.table()/as.data.frame()/as_tibble() to access results

# can print using n/max_extra_cols/max_footer_lines, #464,

Code
dt <- letters %>% lapply(function(.x) tibble(!!.x := 1:10)) %>% bind_cols() %>%
lazy_dt("DT")
print(dt, n = 3)
Output
Source: local data table [10 x 26]
Call: DT

a b c d e f g h i j k l m
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3
# i 7 more rows
# i 13 more variables: n <int>, o <int>, p <int>, q <int>, r <int>, s <int>,
# t <int>, u <int>, v <int>, w <int>, x <int>, y <int>, z <int>

# Use as.data.table()/as.data.frame()/as_tibble() to access results
Code
print(dt, max_extra_cols = 3)
Output
Source: local data table [10 x 26]
Call: DT

a b c d e f g h i j k l m
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6
# i 4 more rows
# i 13 more variables: n <int>, o <int>, p <int>, ...

# Use as.data.table()/as.data.frame()/as_tibble() to access results
Code
print(dt, max_footer_lines = 1)
Output
Source: local data table [10 x 26]
Call: DT

a b c d e f g h i j k l m
<int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int> <int>
1 1 1 1 1 1 1 1 1 1 1 1 1 1
2 2 2 2 2 2 2 2 2 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3 3 3 3 3
4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6
# i 4 more rows

# Use as.data.table()/as.data.frame()/as_tibble() to access results

12 changes: 12 additions & 0 deletions tests/testthat/test-step.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ test_that("has useful display methods", {
})
})

test_that("can print using n/max_extra_cols/max_footer_lines, #464, ", {
expect_snapshot({
dt <- letters %>%
lapply(function(.x) tibble(!!.x := 1:10)) %>%
bind_cols() %>%
lazy_dt("DT")
print(dt, n = 3)
print(dt, max_extra_cols = 3)
print(dt, max_footer_lines = 1)
})
})

test_that("can evaluate to any data frame type", {
dt <- lazy_dt(mtcars, "DT")

Expand Down
Loading