Skip to content

Commit

Permalink
Close #3
Browse files Browse the repository at this point in the history
  • Loading branch information
strengejacke committed Nov 3, 2017
1 parent 2c7d27f commit 23efade
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion R/lbl_df.R
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@
#' set_label(c("Relationship", "Elder's gender", "Elder's age")) %>%
#' lbl_df()
#'
#' @importFrom tibble as_tibble
#' @export
lbl_df <- function(x) {
# add class attribute, if necessary
if (!"tbl_df" %in% class(x)) x <- tibble::as_tibble(x)

This comment has been minimized.

Copy link
@krlmlr

krlmlr Nov 3, 2017

In tibble > 1.3.4 you can simply do new_tibble(x, "lbl_df"), but your code also works with older versions of tibble, so I wouldn't change yet.


# add class attribute, if necessary
if (!"lbl_df" %in% class(x)) class(x) <- c("lbl_df", class(x))

Expand All @@ -40,12 +44,32 @@ format.lbl_df <- function(x, ..., n = NULL, width = NULL, n_extra = NULL) {
NextMethod()
}

#' @importFrom dplyr slice
#' @export
head.lbl_df <- function(x, n = 10L, ...) {
stopifnot(length(n) == 1L)

n <- if (n < 0L)
max(nrow(x) + n, 0L)
else
min(n, nrow(x))

rows <- seq_len(n)

dplyr::slice(x, !! rows)
}

label_type_sum <- function(x) {
class(x) <- c("label_type_sum", class(x))
x
}

#' @export
type_sum.label_type_sum <- function(x) {
attr(x, "label")
lab <- attr(x, "label")

This comment has been minimized.

Copy link
@krlmlr

krlmlr Nov 3, 2017

attr(x, "label") %||% "no label" is shorter, %||% is in rlang.

if (is.null(lab))
"no label"
else
lab
}

0 comments on commit 23efade

Please sign in to comment.