Skip to content

Commit b62526c

Browse files
committed
add subset by tag, closes #305
1 parent 550e59f commit b62526c

File tree

6 files changed

+30
-16
lines changed

6 files changed

+30
-16
lines changed

DESCRIPTION

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: simmer
22
Type: Package
33
Title: Discrete-Event Simulation for R
4-
Version: 4.4.6.8
4+
Version: 4.4.6.9
55
Authors@R: c(
66
person("Iñaki", "Ucar", email="iucar@fedoraproject.org",
77
role=c("aut", "cph", "cre"), comment=c(ORCID="0000-0001-6403-5550")),
@@ -24,6 +24,6 @@ Depends: R (>= 3.4.0)
2424
Imports: Rcpp, magrittr, codetools, utils
2525
Suggests: simmer.plot, parallel, testthat, knitr, rmarkdown, rticles
2626
LinkingTo: Rcpp (>= 0.12.9)
27-
RoxygenNote: 7.2.3
27+
RoxygenNote: 7.3.2
2828
Roxygen: list(old_usage = TRUE)
2929
VignetteBuilder: knitr

NEWS.md

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# simmer devel
22

3+
## New features
4+
5+
- Add support for subsetting by activity tag (#305).
6+
7+
## Minor changes and fixes
8+
39
- Fix `set_source()` to avoid leaking arrivals from the old source (#322).
410
- Fix sources to properly reset distributions and trajectories (#324).
511
- Fix resources to properly reset initial parameters (#325).

R/simmer.R

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (C) 2014 Bart Smeets
2-
# Copyright (C) 2017-2019 Iñaki Ucar
2+
# Copyright (C) 2017-2024 Iñaki Ucar
33
#
44
# This file is part of simmer.
55
#
@@ -53,12 +53,11 @@
5353
#' vignette(package = "simmer")
5454
#' }
5555
#'
56-
#' @docType package
5756
#' @name simmer-package
5857
#'
5958
#' @useDynLib simmer, .registration=TRUE
6059
#' @importFrom Rcpp evalCpp
61-
NULL
60+
"_PACKAGE"
6261

6362
#' @importFrom magrittr %>%
6463
#' @export

R/trajectory-methods.R

+10-7
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Copyright (C) 2014-2015 Bart Smeets
22
# Copyright (C) 2015-2016 Bart Smeets and Iñaki Ucar
3-
# Copyright (C) 2016-2022 Iñaki Ucar
3+
# Copyright (C) 2016-2024 Iñaki Ucar
44
#
55
# This file is part of simmer.
66
#
@@ -93,8 +93,8 @@
9393
trajectory <- function(name="anonymous", verbose=FALSE) {
9494
check_args(name="character", verbose="flag")
9595

96-
env <- list2env(list(
97-
name=name, verbose=verbose, n_activities=0, names=NULL, ptrs=NULL))
96+
env <- list2env(list(name=name, verbose=verbose, n_activities=0,
97+
names=NULL, tags=NULL, ptrs=NULL))
9898
env$head <- function() env$ptrs[[1]]
9999
env$tail <- function() env$ptrs[[length(env)]]
100100
env$clone <- function() subset.trajectory(env)
@@ -114,7 +114,7 @@ print.trajectory <- function(x, indent=0, verbose=x$verbose, ...) {
114114

115115
add_activity <- function(x, activity, env.=parent.frame()) {
116116
tag <- env.$tag
117-
if (!missing(tag)) {
117+
if (missing(tag)) tag <- NA else {
118118
if (!is.character(tag))
119119
stop(get_caller(), ": 'tag' is not a valid character", call.=FALSE)
120120
activity_set_tag_(activity, tag)
@@ -123,6 +123,7 @@ add_activity <- function(x, activity, env.=parent.frame()) {
123123
activity_chain_(x$tail(), activity)
124124
x$ptrs <- c(x$ptrs, activity)
125125
x$names <- c(x$names, get_caller())
126+
x$tags <- c(x$tags, tag)
126127
x$n_activities <- x$n_activities + activity_get_count_(activity)
127128
x
128129
}
@@ -136,7 +137,7 @@ get_parts <- function(x, i, double=FALSE) {
136137
if (is.logical(i)) {
137138
parts <- which(rep_len(i, length(x)))
138139
} else if (is.character(i)) {
139-
parts <- which(x$names %in% i)
140+
parts <- sort(unique(c(which(x$names %in% i), which(x$tags %in% i))))
140141
if (double) parts <- parts[[1]]
141142
} else if (is.numeric(i)) {
142143
i <- i[!is.na(i)]
@@ -163,6 +164,7 @@ subset.trajectory <- function(x, i, double=FALSE) {
163164
})
164165
mapply(activity_chain_, new$ptrs[-length(new$ptrs)], new$ptrs[-1])
165166
new$names <- x$names[parts]
167+
new$tags <- x$tags[parts]
166168
}
167169
new
168170
}
@@ -195,8 +197,8 @@ replace.trajectory <- function(x, i, value, double=FALSE) {
195197
#' hence truncated towards zero). Negative integers indicate elements/slices to
196198
#' leave out the selection.
197199
#'
198-
#' Character vectors will be matched to the names of the activities in the
199-
#' trajectory as by \code{\link{\%in\%}}.
200+
#' Character vectors will be matched to the names and tags of the activities
201+
#' in the trajectory as by \code{\link{\%in\%}}.
200202
#'
201203
#' Logical vectors indicate elements/slices to select. Such vectors are recycled
202204
#' if necessary to match the corresponding extent.
@@ -328,6 +330,7 @@ join.trajectory <- function(...) {
328330

329331
new$ptrs <- c(new$ptrs, i$ptrs)
330332
new$names <- c(new$names, i$names)
333+
new$tags <- c(new$tags, i$tags)
331334
new$n_activities <- new$n_activities + i$n_activities
332335
}
333336
new

man/Extract.trajectory.Rd

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-trajectory.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Copyright (C) 2015-2016 Iñaki Ucar and Bart Smeets
2-
# Copyright (C) 2016-2022 Iñaki Ucar
2+
# Copyright (C) 2016-2024 Iñaki Ucar
33
#
44
# This file is part of simmer.
55
#
@@ -233,7 +233,7 @@ t0 <- trajectory(verbose = TRUE) %>%
233233
t1 <- trajectory(verbose = TRUE) %>%
234234
branch(function() 1, c(TRUE), t0) %>%
235235
join(t0) %>%
236-
branch(function() 1, c(TRUE, TRUE, TRUE), t0, t0, t0) %>%
236+
branch(function() 1, c(TRUE, TRUE, TRUE), t0, t0, t0, tag="foo") %>%
237237
join(t0) %>%
238238
branch(function() 1, c(TRUE, TRUE, TRUE, TRUE, TRUE), t0, t0, t0, t0, t0)
239239

@@ -353,6 +353,9 @@ test_that("character subsetting with [ works as expected", {
353353
test <- t1["asdf"]
354354
expect_equal(length(test), 0)
355355
expect_equal(get_n_activities(test), 0)
356+
test <- t1["foo"]
357+
expect_equal(length(test), 1)
358+
expect_equal(get_n_activities(test), 4)
356359
})
357360

358361
test_that("character replacing with [ works as expected", {
@@ -364,6 +367,9 @@ test_that("character replacing with [ works as expected", {
364367
test["asdf"] <- t0
365368
expect_equal(length(test), 5)
366369
expect_equal(get_n_activities(test), 14)
370+
test["foo"] <- t0
371+
expect_equal(length(test), 5)
372+
expect_equal(get_n_activities(test), 11)
367373
})
368374

369375
test_that("integer subsetting with [[ works as expected", {

0 commit comments

Comments
 (0)