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

Add a names<- method for vctrs_rcrd #1422

Merged
merged 1 commit into from
Aug 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ S3method("length<-",vctrs_rcrd)
S3method("length<-",vctrs_vctr)
S3method("levels<-",vctrs_sclr)
S3method("levels<-",vctrs_vctr)
S3method("names<-",vctrs_rcrd)
S3method("names<-",vctrs_sclr)
S3method("names<-",vctrs_vctr)
S3method("|",vctrs_vctr)
Expand Down
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# vctrs (development version)

* `vec_set_names()` no longer corrupts `vctrs_rcrd` types (#1419).

* `vec_detect_complete()` now computes completeness for `vctrs_rcrd` types in
the same way as data frames, which means that if any field is missing, the
entire record is considered incomplete (#1386).
Expand Down
9 changes: 9 additions & 0 deletions R/type-rcrd.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ names.vctrs_rcrd <- function(x) {
NULL
}

#' @export
`names<-.vctrs_rcrd` <- function(x, value) {
if (is_null(value)) {
x
} else {
abort("Can't assign names to a <vctrs_rcrd>.")
}
}

#' @export
format.vctrs_rcrd <- function(x, ...) {
if (inherits(x, "vctrs_foobar")) {
Expand Down
22 changes: 22 additions & 0 deletions tests/testthat/test-type-rcrd.R
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,28 @@ test_that("vec_proxy() transforms records to data frames", {
)
})

# base methods ------------------------------------------------------------

test_that("has no names", {
x <- new_rcrd(list(a = 1, b = 2L))

expect_null(names(x))
expect_null(vec_names(x))
})

test_that("removing names with `NULL` is a no-op (#1419)", {
x <- new_rcrd(list(a = 1, b = 2L))

expect_identical(`names<-`(x, NULL), x)
expect_identical(vec_set_names(x, NULL), x)
})

test_that("setting character names is an error (#1419)", {
x <- new_rcrd(list(a = 1, b = 2L))

expect_error(`names<-`(x, "x"), "Can't assign names")
expect_error(vec_set_names(x, "x"), "Can't assign names")
})

# coercion ----------------------------------------------------------------

Expand Down