Skip to content

Commit

Permalink
Merge pull request #466 from tidyverse/print-args
Browse files Browse the repository at this point in the history
Add `n`/`max_extra_cols`/`max_footer_lines` args to `print.dtplyr_step`
  • Loading branch information
markfairbanks authored Feb 19, 2024
2 parents fc92809 + 66b22ef commit e3786a6
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 2 deletions.
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

0 comments on commit e3786a6

Please sign in to comment.