Skip to content

Commit

Permalink
Merge branch 'f-#121-doc-print'. Fixes #121.
Browse files Browse the repository at this point in the history
- `print.tbl_df()` gains `n_extra` method and will have the same interface as `trunc_mat()` from now on.
- Added more examples for `print.tbl_df()`, now using data from `nycflights13` instead of `Lahman`.
  • Loading branch information
krlmlr committed Aug 16, 2016
2 parents 927ae40 + 73f0edb commit ff2c700
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 12 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Suggests:
withr,
knitr (>= 1.5.32),
rmarkdown,
Lahman (>= 3.0.1),
nycflights13,
microbenchmark
LinkingTo: Rcpp
LazyData: yes
Expand Down
5 changes: 1 addition & 4 deletions R/glimpse.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,7 @@
#' @export
#' @examples
#' glimpse(mtcars)
#'
#' if (require("Lahman")) {
#' glimpse(Lahman::Batting)
#' }
#' glimpse(nycflights13::flights)
glimpse <- function(x, width = NULL, ...) {
UseMethod("glimpse")
}
Expand Down
4 changes: 2 additions & 2 deletions R/tbl-df.r
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ as.data.frame.tbl_df <- function(x, row.names = NULL, optional = FALSE, ...) {

#' @rdname formatting
#' @export
print.tbl_df <- function(x, ..., n = NULL, width = NULL) {
print(trunc_mat(x, n = n, width = width))
print.tbl_df <- function(x, ..., n = NULL, width = NULL, n_extra = NULL) {
print(trunc_mat(x, n = n, width = width, n_extra = n_extra))
invisible(x)
}

Expand Down
6 changes: 6 additions & 0 deletions R/utils-format.r
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@
#' \code{getOption("width")}; the latter displays only the columns that
#' fit on one screen. You can also set \code{options(tibble.width = Inf)} to
#' override this default and always print all columns.
#' @param n_extra Number of extra columns to print abbreviated information for,
#' if the width is too small for the entire tibble. If \code{NULL}, the
#' default, will print information about at most \code{tibble.max_extra_cols}
#' extra columns.
#' @seealso \link{tibble-package}
#' @keywords internal
#' @examples
Expand All @@ -19,6 +23,8 @@
#' print(as_tibble(mtcars), n = 3)
#' print(as_tibble(mtcars), n = 100)
#'
#' print(nycflights13::flights, n_extra = 2)

This comment has been minimized.

Copy link
@hadley

hadley Aug 16, 2016

Member

Should technically be surrounded in if(requireNamespace("nyclights13")) {}

This comment has been minimized.

Copy link
@krlmlr

krlmlr Aug 16, 2016

Author Member

Not necessarily. CRAN checks pass, because nycflights13 is suggested. Example will fail at the user's end if package is missing, hinting that package needs to be installed to run it. I'd rather add this as a comment above the example.

This comment has been minimized.

Copy link
@hadley

hadley Aug 17, 2016

Member

Examples are supposed to run without error even if suggested packages aren't installed

This comment has been minimized.

Copy link
@krlmlr

krlmlr Aug 17, 2016

Author Member

The problem can be seen in the ggplot2 examples (tidyverse/ggplot2#1603): Anything inside braces will not be auto-printed (except for the last expression). You'd have to call print() explicitly, this makes the examples less readable. (This is not much of an issue with the current tibble examples, because they print explicitly if nycflights13 is required.)

I think we can have either readable or always-error-free examples, but not both. I still prefer readable examples that may require some action from the user. We could even offer a prepare_examples() function that installs everything that is needed, to minimize user's effort.

#' print(nycflights13::flights, width = Inf)
#' @name formatting
NULL

Expand Down
9 changes: 8 additions & 1 deletion man/formatting.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 1 addition & 4 deletions man/glimpse.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions tests/testthat/test-trunc-mat.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
context("Truncated matrix")

test_that("interface of print() identical to trunc_mat()", {
print_arg_names <- names(formals(print.tbl_df))
print_arg_names_without_ellipsis <- setdiff(print_arg_names, "...")

trunc_mat_arg_names <- names(formals(trunc_mat))

expect_equal(print_arg_names_without_ellipsis, trunc_mat_arg_names)
})

test_that("print() returns output invisibly", {
expect_output(ret <- withVisible(print(as_tibble(mtcars))))
expect_false(ret$visible)
Expand Down

0 comments on commit ff2c700

Please sign in to comment.