-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Strict checking of integer and logical column indexes. For integers, passing a non-integer index or an out-of-bounds index raises an error. For logicals, only vectors of length 1 or `ncol` are supported. Passing a matrix or an array now raises an error in any case (#83).
- Loading branch information
Showing
4 changed files
with
119 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
check_names_df <- function(j, ...) UseMethod("check_names_df") | ||
|
||
check_names_df.default <- function(j, ...) { | ||
stop("unsupported index type: ", class(j)[[1L]]) | ||
} | ||
|
||
check_names_df.character <- function(j, x) { | ||
check_needs_no_dim(j) | ||
|
||
pos <- safe_match(j, names(x)) | ||
if(any(is.na(pos))){ | ||
names <- j[is.na(pos)] | ||
stop("undefined columns: ", paste(names, collapse = ", "), call. = FALSE) | ||
} | ||
pos | ||
} | ||
|
||
check_names_df.numeric <- function(j, x) { | ||
check_needs_no_dim(j) | ||
|
||
if (any(is.na(j))) { | ||
stop("NA column indexes not supported", call. = FALSE) | ||
} | ||
|
||
non_integer <- (j != trunc(j)) | ||
if (any(non_integer)) { | ||
stop("invalid non-integer column indexes: ", paste(j[non_integer], collapse = ", "), call. = FALSE) | ||
} | ||
neg_too_small <- (j < -length(x)) | ||
if (any(neg_too_small)) { | ||
stop("invalid negative column indexes: ", paste(j[neg_too_small], collapse = ", "), call. = FALSE) | ||
} | ||
pos_too_large <- (j > length(x)) | ||
if (any(pos_too_large)) { | ||
stop("invalid column indexes: ", paste(j[pos_too_large], collapse = ", "), call. = FALSE) | ||
} | ||
|
||
seq_along(x)[j] | ||
} | ||
|
||
check_names_df.logical <- function(j, x) { | ||
check_needs_no_dim(j) | ||
|
||
if (!(length(j) %in% c(1L, length(x)))) { | ||
stop("length of logical index vector must be 1 or ", length(x), ", got: ", length(j), call. = FALSE) | ||
} | ||
if (any(is.na(j))) { | ||
stop("NA column indexes not supported", call. = FALSE) | ||
} | ||
seq_along(x)[j] | ||
} | ||
|
||
check_needs_no_dim <- function(j) { | ||
if (needs_dim(j)) { | ||
stop("unsupported use of matrix or array for column indexing", call. = FALSE) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters