Skip to content

Commit

Permalink
Added length<- method for sps objects
Browse files Browse the repository at this point in the history
  • Loading branch information
marberts committed Jul 8, 2023
1 parent 655d037 commit e0a43a9
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 3 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: sps
Title: Sequential Poisson Sampling
Version: 0.5.1.9001
Version: 0.5.1.9002
Authors@R: c(
person("Steve", "Martin", role = c("aut", "cre", "cph"), email = "stevemartin041@gmail.com", comment = c(ORCID = "0000-0003-2544-9480")),
person("Justin", "Francis", role = "ctb")
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export(
S3method(weights, sps)
S3method(levels, sps)
S3method("levels<-", sps)
S3method("length<-", sps)
S3method(print, sps)
S3method(Ops, sps)
S3method(Math, sps)
Expand Down
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

- `sps` objects now inherit from numeric instead of integer.

- Fixed a bug where the levels attribute could be set for `sps` objects.
- Replacement methods for `length` and `levels` no longer mangle `sps` objects.

## Changes in version 0.5.0

Expand Down
7 changes: 6 additions & 1 deletion R/sps.R
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ ps <- stratify(ps_)

#---- Methods for class 'sps' ----
levels.sps <- function(x) {
res <- rep_len("TS", length(x))
res <- rep.int("TS", length(x))
res[weights(x) == 1] <- "TA"
res
}
Expand All @@ -79,6 +79,11 @@ levels.sps <- function(x) {
stop("cannot replace levels attribute")
}

`length<-.sps` <- function(x, value) {
x <- as.vector(x)
NextMethod()
}

weights.sps <- function(object, ...) {
attr(object, "weights")
}
Expand Down
3 changes: 3 additions & 0 deletions tests/testthat/test-sps.R
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,7 @@ test_that("attributes get removed", {
# and replacement methods
expect_true(inherits(replace(samp, 1, 1), "numeric"))
expect_true(inherits(replace(samp, 1, 1L), "integer"))

length(samp) <- 2
expect_true(inherits(samp, "integer"))
})

0 comments on commit e0a43a9

Please sign in to comment.