Skip to content

Commit

Permalink
Merge branch 'release/0.3-4' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
Kirill Müller committed Mar 18, 2016
2 parents 0cc8550 + 3a363ec commit bfc394f
Show file tree
Hide file tree
Showing 29 changed files with 622 additions and 518 deletions.
3 changes: 0 additions & 3 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
^\.Rproj\.user$
^\.travis\.yml$
.Rprofile
inst/db
man-roxygen
demo/pandas
^\.httr-oauth$
^cran-comments\.md$
^README\.Rmd$
Expand Down
30 changes: 20 additions & 10 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
Package: tibble
Encoding: UTF-8
Version: 0.3-3
Version: 0.3-4
Title: Simple Data Frames
Description: Provides a 'tbl_df' class that offers better checking and
printing capabilities than traditional data frames.
Authors@R: c( person("Hadley", "Wickham", , "[email protected]",
"aut"), person("Romain", "Francois", ,
"[email protected]", "aut"), person("Kirill", "Müller",
, "[email protected]", c("aut", "cre")), person("RStudio",
role = "cph") )
printing capabilities than traditional data frames.
Authors@R: c(
person("Hadley", "Wickham", , "[email protected]", "aut"),
person("Romain", "Francois", , "[email protected]", "aut"),
person("Kirill", "Müller", , "[email protected]", c("aut", "cre")),
person("RStudio", role = "cph")
)
URL: https://github.com/krlmlr/tibble
BugReports: https://github.com/krlmlr/tibble/issues
Depends: R (>= 3.1.2)
Imports: methods, assertthat, utils, lazyeval (>= 0.1.10), Rcpp
Suggests: testthat, knitr, rmarkdown, Lahman (>= 3.0.1), magrittr,
microbenchmark
Imports:
methods,
assertthat,
utils,
lazyeval (>= 0.1.10),
Rcpp
Suggests:
testthat,
knitr,
rmarkdown,
Lahman (>= 3.0.1),
microbenchmark
LinkingTo: Rcpp
LazyData: yes
License: MIT + file LICENSE
Expand Down
18 changes: 8 additions & 10 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,19 @@ S3method(format_v,default)
S3method(glimpse,data.frame)
S3method(glimpse,default)
S3method(glimpse,tbl)
S3method(obj_type,"NULL")
S3method(obj_type,data.frame)
S3method(obj_type,data_frame)
S3method(obj_type,default)
S3method(is_vector_s3,Date)
S3method(is_vector_s3,POSIXct)
S3method(is_vector_s3,data.frame)
S3method(is_vector_s3,default)
S3method(is_vector_s3,factor)
S3method(obj_sum,default)
S3method(print,tbl_df)
S3method(print,trunc_mat)
S3method(type_sum,Date)
S3method(type_sum,POSIXt)
S3method(type_sum,array)
S3method(type_sum,character)
S3method(type_sum,data.frame)
S3method(type_sum,default)
S3method(type_sum,factor)
S3method(type_sum,integer)
S3method(type_sum,logical)
S3method(type_sum,matrix)
S3method(type_sum,numeric)
export(add_row)
export(as_data_frame)
export(column_to_rownames)
Expand All @@ -42,9 +38,11 @@ export(dim_desc)
export(frame_data)
export(glimpse)
export(has_rownames)
export(is_vector_s3)
export(knit_print.trunc_mat)
export(lst)
export(lst_)
export(obj_sum)
export(remove_rownames)
export(repair_names)
export(rownames_to_column)
Expand Down
13 changes: 13 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
Version 0.3-4 (2016-03-18)
===

- Renamed `obj_type()` to `obj_sum()`, improvements, better integration with `type_sum()`.
- Add tests.
- Improve documentation and vignette.
- Internal cleanup.
- Improve `[.tbl_df()` error message.
- `frame_data()` returns 0-row but n-col data frame if no data.
- Further cleanup of `repair_names()`.
- Don't trim ws in `repair_names()` (#47).


Version 0.3-3 (2016-03-18)
===

Expand Down
2 changes: 0 additions & 2 deletions R/dataframe.R
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
methods::setOldClass(c("tbl_df", "tbl", "data.frame"))

#' Build a data frame or list.
#'
#' \code{data_frame} is trimmed down version of \code{\link{data.frame}} that:
Expand Down
42 changes: 30 additions & 12 deletions R/frame-data.R
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#' Row-wise data_frame creation
#' Row-wise tibble creation
#'
#' Create a row-wise \code{\link{data_frame}}.
#' Create \code{\link{data_frame}}s laying out the data in rows, rather than
#' in columns. This is useful for small tables of data where readability is
#' important.
#'
#' @param ... Arguments specifying the structure of a \code{data_frame}.
#'
#' Variable names should be formulas, and may only appear before the data.
#' @return A \code{\link{tbl_df}}.
#' @export
#' @examples
#' frame_data(
Expand All @@ -12,6 +15,14 @@
#' "b", 2,
#' "c", 3
#' )
#'
#' # frame_data will create a list column if the value in each cell is
#' # not a scalar
#' frame_data(
#' ~x, ~y,
#' "a", 1:3,
#' "b", 4:6
#' )
frame_data <- function(...) {

dots <- list(...)
Expand All @@ -21,7 +32,9 @@ frame_data <- function(...) {
i <- 1
while (TRUE) {
if (i > length(dots)) {
return(data_frame())
out <- rep(list(logical()), length(frame_names))
names(out) <- frame_names
return(as_data_frame(out))
}

el <- dots[[i]]
Expand All @@ -32,12 +45,14 @@ frame_data <- function(...) {
break

if (length(el) != 2) {
stop("expected a column name with a single argument; e.g. '~ name'")
stop("expected a column name with a single argument; e.g. '~ name'",
call. = FALSE)
}

candidate <- el[[2]]
if (!(is.symbol(candidate) || is.character(candidate))) {
stop("expected a symbol or string denoting a column name")
stop("expected a symbol or string denoting a column name",
call. = FALSE)
}

frame_names <- c(frame_names, as.character(el[[2]]))
Expand All @@ -46,7 +61,7 @@ frame_data <- function(...) {
}

if (!length(frame_names)) {
stop("no column names detected in 'frame_data()' call")
stop("no column names detected in 'frame_data()' call", call. = FALSE)
}

frame_rest <- dots[i:length(dots)]
Expand All @@ -57,11 +72,14 @@ frame_data <- function(...) {
# structure.
frame_ncol <- length(frame_names)
if (n_elements %% frame_ncol != 0) {
stop(sprintf(
"invalid 'frame_data()' specification: had %s elements and %s columns",
n_elements,
frame_ncol
))
stop(
sprintf(
"invalid 'frame_data()' specification: had %s elements and %s columns",
n_elements,
frame_ncol
),
call. = FALSE
)
}

frame_mat <- matrix(frame_rest, ncol = frame_ncol, byrow = TRUE)
Expand Down
45 changes: 18 additions & 27 deletions R/repair-names.R
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
#' Repair object names.
#'
#' \code{repair_names} ensures its input has non-missing and
#' unique names. It also strips any leading or trailing spaces.
#' Valid names are left as is.
#' unique names (duplicated names get a numeric suffix). Valid names are
#' left as is.
#'
#' @param x A named vector.
#' @param prefix A string, the prefix to use for new column names.
#' @param sep A string, inserted between the column name and de-duplicating
#' @param sep A string inserted between the column name and de-duplicating
#' number.
#' @return \code{x} with valid names.
#' @export
Expand All @@ -16,33 +16,24 @@
#' tbl <- as_data_frame(structure(list(3, 4, 5), class = "data.frame"))
#' repair_names(tbl)
repair_names <- function(x, prefix = "V", sep = "") {
if (length(x) == 0)
if (length(x) == 0) {
names(x) <- character()
return(x)
}

xnames <- init_names(x)
blanks <- xnames == ""
new_names <- make_unique(names2(x), prefix = prefix, sep = sep)
setNames(x, new_names)
}

# The order vector defines the order in which make.unique() should process the
# entries. Blanks are initialized with the prefix. The index of the first
# blank entry appears twice in this vector if there's no column named like the
# prefix, to make sure that blank columns always start with V1 (or a higher
# index if appropriate). See also the "pathological cases" test.
order <- c(
which(!blanks),
if (all(xnames[!blanks] != prefix) && any(blanks))
which.max(blanks),
which(blanks))
xnames[blanks] <- prefix
xnames[order] <- make.unique(xnames[order], sep = sep)
make_unique <- function(x, prefix = "V", sep = "") {
blank <- x == ""

names(x) <- xnames
x
}
# Ensure existing names are unique
x[!blank] <- make.unique(x[!blank], sep = sep)

init_names <- function(x) {
xnames <- names(x)
if (is.null(xnames))
rep("", length(x))
else
ifelse(is.na(xnames), "", trim_ws(xnames))
# Replace blank names
new_vars <- setdiff(paste(prefix, seq_along(x), sep = sep), x)
x[blank] <- new_vars[seq_len(sum(blank))]

x
}
46 changes: 23 additions & 23 deletions R/rownames.R
Original file line number Diff line number Diff line change
@@ -1,39 +1,45 @@
#' Row names
#' Tools for working with row names
#'
#' \code{has_rownames} checks if a data frame has row names.
#' @param df Input data frame
#' @export
#' @rdname rownames
#' Generally, it is best to avoid row names, because they are basically a
#' character column with different semantics to every other column. These
#' functions allow to you detect if a data frame has row names
#' (\code{has_rownames}), remove them (\code{remove_rownames}), or convert
#' them back-and-forth between an explicit column (\code{rownames_to_column},
#' and \code{column_to_rownames}).
#'
#' @param df A data frame
#' @param var Name of column to use for rownames.
#' @examples
#' has_rownames(mtcars)
#' has_rownames(iris)
#' has_rownames(remove_rownames(mtcars))
#'
#' head(rownames_to_column(mtcars))
#'
#' mtcars_tbl <- rownames_to_column(tbl_df(mtcars))
#' mtcars_tbl
#' column_to_rownames(mtcars_tbl)
#' @name rownames
NULL


#' @export
#' @rdname rownames
has_rownames <- function(df) {
stopifnot(is.data.frame(df))
.row_names_info(df) > 0L
}

#' \code{remove_rownames} removes all row names.
#' @export
#' @rdname rownames
#' @examples
#' rownames(remove_rownames(mtcars))
remove_rownames <- function(df) {
stopifnot(is.data.frame(df))
rownames(df) <- NULL
df
}

#' \code{rownames_to_column} convert row names to an explicit variable.
#'
#' @param var Name of variable to use
#' @export
#' @rdname rownames
#' @importFrom stats setNames
#' @examples
#' rownames_to_column(mtcars)
#'
#' mtcars_tbl <- rownames_to_column(tbl_df(mtcars))
#' mtcars_tbl
rownames_to_column <- function(df, var = "rowname") {
stopifnot(is.data.frame(df))

Expand All @@ -53,14 +59,8 @@ rownames_to_column <- function(df, var = "rowname") {
new_df
}

#' \code{column_to_rownames} convert a column variable to row names. This is an
#' inverted operation of \code{rownames_to_column}.
#'
#' @rdname rownames
#' @export
#' @examples
#'
#' column_to_rownames(mtcars_tbl)
column_to_rownames <- function(df, var = "rowname") {
stopifnot(is.data.frame(df))

Expand Down
Empty file removed R/src-local.r
Empty file.
Empty file removed R/src.r
Empty file.
Loading

0 comments on commit bfc394f

Please sign in to comment.