From 2f6ad882a92dea9473af4bf6c6e727e21a002b63 Mon Sep 17 00:00:00 2001 From: Waldir Leoncio Date: Tue, 3 Mar 2020 11:04:40 +0100 Subject: [PATCH] Added vectorizing capability and unit test --- R/isfield.R | 3 ++- tests/testthat/test-convertedBaseFunctions.R | 12 ++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/R/isfield.R b/R/isfield.R index ef818a2..dc6404a 100644 --- a/R/isfield.R +++ b/R/isfield.R @@ -4,6 +4,7 @@ #' @param x list #' @param field name of field #' @references https://se.mathworks.com/help/matlab/ref/isfield.html +#' @export isfield <- function(x, field) { - field %in% names(x) + sapply(field, function(f) f %in% names(x)) } \ No newline at end of file diff --git a/tests/testthat/test-convertedBaseFunctions.R b/tests/testthat/test-convertedBaseFunctions.R index 7f75050..5683637 100644 --- a/tests/testthat/test-convertedBaseFunctions.R +++ b/tests/testthat/test-convertedBaseFunctions.R @@ -99,4 +99,16 @@ test_that("reshape reshapes properly", { expect_error(reshape(mx, 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))) +}) + +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) + ) }) \ No newline at end of file