Skip to content

Commit 21fb795

Browse files
committed
Fix edge case with nested choices. Fixes rstudio#560
When the set of choices is a list containing a named vector of length 1, choicesWithNames would return the wrong result.
1 parent 3aa2212 commit 21fb795

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

R/bootstrap.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,7 +681,7 @@ choicesWithNames <- function(choices) {
681681
res <- lapply(obj, function(val) {
682682
if (is.list(val))
683683
listify(val)
684-
else if (length(val) == 1)
684+
else if (length(val) == 1 && is.null(names(val)))
685685
val
686686
else
687687
makeNamed(as.list(val))

inst/tests/test-bootstrap.r

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,11 @@ test_that("Choices are correctly assigned names", {
7676
choicesWithNames(list(A="a", B="b", C=list("d", "e"))),
7777
list(A="a", B="b", C=list(d="d", e="e"))
7878
)
79+
# List, named, with a named sub-vector of length 1
80+
expect_identical(
81+
choicesWithNames(list(A="a", B="b", C=c(D="d"))),
82+
list(A="a", B="b", C=list(D="d"))
83+
)
7984
# List, some named, with sublist
8085
expect_identical(
8186
choicesWithNames(list(A="a", "b", C=list("d", E="e"))),

0 commit comments

Comments
 (0)