New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
selectInput: improve factor handling #2524
Conversation
R/input-utils.R
Outdated
|
||
#' @export | ||
listify.character <- function(x) { | ||
if (length(x) == 1 && is.null(names(x))) x else as.list(ensureNamed(x)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
... else ensureNames(as.list(x))
?
Is the goal to return a list with every element's name as ''
or to return a list with no names?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In that case, return a list with every element's name as ''
(unless it's already named)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's my interpretation of https://github.com/rstudio/shiny/pull/2524/files#diff-c851d180beb8024d6b46a73dab4756d9L113 . makeNamed(as.list(val))
vs as.list(ensureNamed(x))
R/input-utils.R
Outdated
# Take a vector or list, and convert to list. Also, if any children are | ||
# vectors with length > 1, convert those to list. If the list is unnamed, | ||
# convert it to a named list with blank names. | ||
listify <- function(x) UseMethod("listify", x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My gut reaction is this should be rewritten to be a if
/ else
.
- It wouldn't need to be exported to work.
- It could handle odd s3 cases better (things that don't have atomic classes).
Setup:
if `is.list`, `is.null`
"code for `is.list`"
else if `is.numeric`, `is.complex`, `is.logical`, `is.factor`
"code for `is.numeric`"
else
"code for `is.character`"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You know, now that it has all the @export
noise, I have to agree. I swear, it was looking pretty tight before all that 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the listify
generic isn't exported from the package, you don't need to @export
every method, and therefore nothing will be added to the NAMESPACE file. That said, I also lean toward having a single function with if-else here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would add {``}
due to the nested if/else statement.
(I like them any time there is anything beyond an if
. Makes it so things can be executed in the console.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fix travis. otherwise LGTM!
One thought -- we should make sure there are tests for lists (and atomic vectors) where the list is named, but some or all of the names are x <- list(A="a", "b", "c")
x
#> $A
#> [1] "a"
#>
#> [[2]]
#> [1] "b"
#>
#> [[3]]
#> [1] "c"
attributes(x)
#> $names
#> [1] "A" "" ""
y <- x[2:3]
y
#> [[1]]
#> [1] "b"
#>
#> [[2]]
#> [1] "c"
attributes(y)
#> $names
#> [1] "" ""
# Contrast y with:
z <- list("b", "c")
z
#> [[1]]
#> [1] "b"
#>
#> [[2]]
#> [1] "c"
attributes(z)
#> NULL |
@wch there's a test for that case: shiny/tests/testthat/test-bootstrap.r Line 89 in 0d7b149
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good! Just a couple of small nits.
15deadc
to
65f4974
Compare
…hiny#2524" This reverts commit b79f51d.
* Use a factor in the selectInput for 002-text, tests rstudio/shiny#2524 * Revert "Use a factor in the selectInput for 002-text, tests rstudio/shiny#2524" This reverts commit b79f51d. * Add test app for selecting from a factor, tests rstudio/shiny#2524
This work was motivated by #2515, which demonstrated that unnamed factors work as expected, but named factors did not.
listify()
was restructured in order to make clearer how various types are handled. Support for named factors was added.selectInput()
documentation now mentions factor handling.complex
andlogical
vectors.Open Questions
ShouldWe decided not to produce a warning.selectInput()
produce a warning if a factor is used as an option list or otherwise included in the list of options?QA Notes
Given this test application:
Error in (function (choice, name) : All sub-lists in "choices" must be named.
devtools::install_github("rstudio/shiny")
(this PR has been merged)