Skip to content

Commit c645c75

Browse files
authored
Fix scenario list (#249)
* changing asplit to split * Updating news * Updating scenario_list
1 parent 507b1bb commit c645c75

5 files changed

Lines changed: 26 additions & 18 deletions

File tree

NEWS.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# simstudy (development version)
22

3+
* scenario_list() return type change: now returns a list of 1-row
4+
data.frames (instead of vectors) to support mixed numeric/character
5+
scenario values without coercion. If you were transposing scenario
6+
vectors for logging (e.g., data.table(t(argsvec), result)),
7+
switch to data.table(argsvec, result). Vignettes updated.
8+
39
# simstudy (0.9.2)
410

511
* A minor fix in a unit test for correlated matrices that was failing on CRAN due to

R/utility.R

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ grouped <- function(...) {
5353
#' vectors or `grouped_params` objects created with \code{\link{grouped}}.
5454
#' @param each Integer representing the number of replications for each scenario
5555
#' or set of parameters. Defaults to 1.
56-
#' @return A list of scenarios, where each element is a named vector of parameter
57-
#' values with an added element `scenario` giving the scenario index.
56+
#' @return A list of scenarios, where each element is a one-row data.frame
57+
#' of parameter values with an added element `scenario` giving the scenario index.
5858
#' @examples
5959
#' # Regular parameters
6060
#' s1 <- scenario_list(a = 1:2, b = c("x", "y"))
@@ -139,9 +139,10 @@ scenario_list <- function(..., each = 1) {
139139
}
140140

141141
argmat$scenario <- seq_len(nrow(argmat))
142-
scenarios <- asplit(argmat, MARGIN = 1)
143-
142+
scenarios <- split(x = argmat, f = seq_len(nrow(argmat)))
143+
names(scenarios) <- NULL
144144
return(rep(scenarios, each = each))
145+
145146
}
146147

147148
#' Generate Mixture Formula

man/scenario_list.Rd

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-utility.R

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1516,14 +1516,14 @@ test_that("scenario_list() preserves correct values in scenarios", {
15161516
result <- scenario_list(a, grouped(x, y))
15171517

15181518
# Check first scenario
1519-
expect_equal(unname(result[[1]][1]), 1)
1520-
expect_equal(unname(result[[1]][2]), 10)
1521-
expect_equal(unname(result[[1]][[3]]), 100)
1519+
expect_equal(result[[1]][1, 1], 1)
1520+
expect_equal(result[[1]][1, 2], 10)
1521+
expect_equal(result[[1]][[1, 3]], 100)
15221522

15231523
# Check last scenario
1524-
expect_equal(unname(result[[4]][1]), 2)
1525-
expect_equal(unname(result[[4]][2]), 20)
1526-
expect_equal(unname(result[[4]][[3]]), 200)
1524+
expect_equal(result[[4]][1, 1], 2)
1525+
expect_equal(result[[4]][1, 2], 20)
1526+
expect_equal(result[[4]][1, 3], 200)
15271527
})
15281528

15291529
test_that("scenario_list() assigns sequential scenario numbers", {
@@ -1533,16 +1533,17 @@ test_that("scenario_list() assigns sequential scenario numbers", {
15331533
result <- scenario_list(a, b)
15341534

15351535
scenarios <- sapply(result, function(x) x["scenario"])
1536-
expect_equal(unname(scenarios), 1:6)
1536+
expect_equal(unname(unlist(scenarios)), seq(1:6))
1537+
15371538
})
15381539

1539-
test_that("scenario_list() returns list of named vectors", {
1540+
test_that("scenario_list() returns list of data.frames", {
15401541
a <- c(1, 2)
15411542

15421543
result <- scenario_list(a)
15431544

15441545
expect_type(result, "list")
1545-
expect_named(result[[1]], c("a", "scenario"))
1546+
expect_s3_class(result[[1]], "data.frame")
15461547
})
15471548

15481549

@@ -1552,8 +1553,8 @@ test_that("scenario_list() handles edge case of single value", {
15521553
result <- scenario_list(a)
15531554

15541555
expect_equal(length(result), 1)
1555-
expect_equal(unname(result[[1]]["a"]), 1)
1556-
expect_equal(unname(result[[1]]["scenario"]), 1)
1556+
expect_equal(result[[1]][["a"]], 1)
1557+
expect_equal(result[[1]][["scenario"]], 1)
15571558
})
15581559

15591560
test_that("scenario_list() preserves variable names correctly when specified in call", {

vignettes/framework.Rmd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ Once the data have been collected, it is quite easy to summarize and create a ta
185185

186186
```{r, eval = FALSE}
187187
summarize <- function(m_fit) {
188-
args <- data.table(t(m_fit[[1]]))
188+
args <- data.table(m_fit[[1]])
189189
reject <- m_fit[[2]][term == "rx", p.value <= 0.05]
190190
cbind(args, reject)
191191
}

0 commit comments

Comments
 (0)