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

Expose vec_names2(), vec_names() and vec_set_names() #1173

Merged
merged 4 commits into from
Jul 6, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,8 @@ export(vec_list_cast)
export(vec_match)
export(vec_math)
export(vec_math_base)
export(vec_names)
export(vec_names2)
export(vec_order)
export(vec_proxy)
export(vec_proxy_compare)
Expand Down Expand Up @@ -555,6 +557,7 @@ export(vec_rep_each)
export(vec_repeat)
export(vec_restore)
export(vec_seq_along)
export(vec_set_names)
export(vec_size)
export(vec_size_common)
export(vec_slice)
Expand Down
44 changes: 40 additions & 4 deletions R/names.R
Original file line number Diff line number Diff line change
Expand Up @@ -193,15 +193,47 @@ detect_dot_dot <- function(names) {
grep("^[.][.](?:[.]|[1-9][0-9]*)$", names)
}

#' Extract repaired names from a vector
#' Get or set the names of a vector
#'
#' Returns the repaired names from a vector, even if the vector is unnamed.
#' @description
#' These functions work like [rlang::names2()], [names()] and [names<-()],
#' except that they return or modify the conceptual names of the vector.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the rowwise names of the vector. These are:

  • The usual names() for atomic vectors and lists
  • The row names for data frames and matrices
  • The names of the first dimension for arrays

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excuse the drive-by comment, but what is meant by "conceptual" names? If that term is defined and used elsewhere and I just don't have enough context, then never mind.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed this is not a defined term. I'm suggesting to replace "conceptual" by "rowwise", which seems clearer?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think since it is so related to the vctrs concept of size, they could be called "size consistent names"

#' This is different for data frames and matrices (here, row names are used),
#' and arrays (here, the dimension names of the first dimension are used).
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
#'
#' `vec_names2()` returns the repaired names from a vector, even if it is unnamed.
#' See [vec_as_names()] for details on name repair.
#'
#' `vec_names()` is a bare-bones version that returns `NULL` if the vector is
#' unnamed.
#'
#' `vec_set_names()` sets the names.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps a note that you can set names to NULL to remove them?

#'
#' @param x A vector with names
#' @inheritParams vec_as_names
#'
#' @return The names of x, repaired
#' @noRd
#' @return
#' `vec_names2()` returns the names of `x`, repaired.
#' `vec_names()` returns the names of `x` or `NULL` if unnamed.
#' `vec_set_names()` returns `x` with names updated.
#'
#' @name vec_names
#' @export
#' @examples
#' vec_names2(1:3)
#' vec_names2(1:3, repair = "unique")
#' vec_names2(c(a = 1, b = 2))
#'
#' # `vec_names()` is different from `names()`:
krlmlr marked this conversation as resolved.
Show resolved Hide resolved
#' vec_names(data.frame(a = 1, b = 2))
#' names(data.frame(a = 1, b = 2))
#' vec_names(mtcars)
#' names(mtcars)
#' vec_names(Titanic)
#' names(Titanic)
#'
#' vec_set_names(1:3, letters[1:3])
#' vec_set_names(data.frame(a = 1:3), letters[1:3])
vec_names2 <- function(x,
...,
repair = c("minimal", "unique", "universal", "check_unique"),
Expand Down Expand Up @@ -247,6 +279,8 @@ unique_names <- function(x, quiet = FALSE) {
.Call(vctrs_unique_names, x, quiet)
}

#' @rdname vec_names
#' @export
vec_names <- function(x) {
.Call(vctrs_names, x)
}
Expand Down Expand Up @@ -433,6 +467,8 @@ set_names_fallback <- function(x, names) {
x
}

#' @rdname vec_names
#' @export
vec_set_names <- function(x, names) {
.Call(vctrs_set_names, x, names)
}
Expand Down
84 changes: 84 additions & 0 deletions man/vec_names.Rd

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