Skip to content

Commit 7170279

Browse files
committed
Add is.na and is.na<- methods
1 parent 74a4dec commit 7170279

File tree

7 files changed

+54
-6
lines changed

7 files changed

+54
-6
lines changed

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,13 @@
33
S3method("[",blob)
44
S3method("[<-",blob)
55
S3method("[[<-",blob)
6+
S3method("is.na<-",blob)
67
S3method(as.blob,character)
78
S3method(as.blob,integer)
89
S3method(as.blob,list)
910
S3method(as.blob,raw)
1011
S3method(format,blob)
12+
S3method(is.na,blob)
1113
S3method(is_vector_s3,blob)
1214
S3method(obj_sum,blob)
1315
S3method(print,blob)

R/accessors.R

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414

1515
#' @export
1616
`[[<-.blob` <- function(x, i, ..., value) {
17-
if (!is.raw(value)) {
18-
stop("RHS must be raw vector", call. = FALSE)
17+
if (!is.raw(value) && !is.null(value)) {
18+
stop("RHS must be raw vector or NULL", call. = FALSE)
1919
}
2020

21-
NextMethod()
21+
if (is.null(value)) {
22+
x[i] <- list(NULL)
23+
x
24+
} else {
25+
NextMethod()
26+
}
2227
}

R/format.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ format.blob <- function(x, ...) {
33
if (length(x) == 0)
44
return(character())
55

6-
paste0("blob[", blob_size(x, ...) , "]")
6+
ifelse(is.na(x), "<NA>", paste0("blob[", blob_size(x, ...) , "]"))
77
}
88

99
#' @export

R/missing.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#' @export
2+
is.na.blob <- function(x) {
3+
vapply(x, is.null, logical(1))
4+
}
5+
6+
#' @export
7+
`is.na<-.blob` <- function(x, value) {
8+
if (!is.logical(value) || length(x) != length(value)) {
9+
stop("RHS must be a logical the same length as `x`", call. = FALSE)
10+
}
11+
12+
x[value] <- rep(list(NULL), sum(value))
13+
x
14+
}

R/util.R

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ is_raw_list <- function(x) {
22
if (!is.list(x))
33
return(FALSE)
44

5-
all_raw <- all(vapply(x, is.raw, logical(1)))
6-
if (!all_raw)
5+
raw <- vapply(x, is.raw, logical(1))
6+
null <- vapply(x, is.null, logical(1))
7+
8+
if (!all(raw | null))
79
return(FALSE)
810

911
TRUE
1012
}
13+

tests/testthat/test-accessors.R

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@ test_that("can't insert objects of incorrect type", {
1111
expect_error(x[[1]] <- 1, "must be raw vector")
1212
expect_error(x[1] <- 1, "must be list of raw vectors")
1313
})
14+
15+
test_that("can insert raw or NULL", {
16+
x <- as.blob(1:4)
17+
18+
x[[1]] <- as.raw(0)
19+
x[2] <- list(as.raw(0))
20+
x[[3]] <- NULL
21+
x[4] <- list(NULL)
22+
23+
expect_equal(x, blob(as.raw(0), as.raw(0), NULL, NULL))
24+
})

tests/testthat/test-missing.R

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
context("missing")
2+
3+
test_that("is.na detects nulls", {
4+
x <- blob(as.raw(1), NULL, as.raw(2), NULL)
5+
expect_equal(is.na(x), c(FALSE, TRUE, FALSE, TRUE))
6+
})
7+
8+
test_that("is.na<- sets missing values", {
9+
x <- as.blob(1:4)
10+
is.na(x) <- (1:4 %% 2 == 0)
11+
12+
expect_equal(x, blob(as.raw(1), NULL, as.raw(3), NULL))
13+
})

0 commit comments

Comments
 (0)