Added vectorizing capability and unit test
This commit is contained in:
parent
317d281e70
commit
2f6ad882a9
2 changed files with 14 additions and 1 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
#' @param x list
|
#' @param x list
|
||||||
#' @param field name of field
|
#' @param field name of field
|
||||||
#' @references https://se.mathworks.com/help/matlab/ref/isfield.html
|
#' @references https://se.mathworks.com/help/matlab/ref/isfield.html
|
||||||
|
#' @export
|
||||||
isfield <- function(x, field) {
|
isfield <- function(x, field) {
|
||||||
field %in% names(x)
|
sapply(field, function(f) f %in% names(x))
|
||||||
}
|
}
|
||||||
|
|
@ -100,3 +100,15 @@ test_that("reshape reshapes properly", {
|
||||||
expect_error(reshape(ra, c(1, 2, 3)))
|
expect_error(reshape(ra, c(1, 2, 3)))
|
||||||
expect_equal(reshape(ra, c(3, 2, 2)), array(ra, c(3, 2, 2)))
|
expect_equal(reshape(ra, c(3, 2, 2)), array(ra, c(3, 2, 2)))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
test_that("isfield works as on Matlab", {
|
||||||
|
S <- list()
|
||||||
|
S$x <- rnorm(100)
|
||||||
|
S$y <- sin(S$x)
|
||||||
|
S$title <- "y = sin(x)"
|
||||||
|
expect_true(isfield(S, "title"))
|
||||||
|
expect_equivalent(
|
||||||
|
object = isfield(S, c("x", "y", "z", "title", "error")),
|
||||||
|
expected = c(TRUE, TRUE, FALSE, TRUE, FALSE)
|
||||||
|
)
|
||||||
|
})
|
||||||
Loading…
Add table
Reference in a new issue