I think much better would be to leave NA
as a special case, i.e. treating it as missing value, not character
. This is because in R (and you compute in R) NA
is a well-established object, so you may get many advantages (like speed) when leaving it in a data frame as NA
(missing value).
But that's true, that JavaScript doesn't have such an construct as NA
, so we end up with character type:
jsonlite::toJSON(c(1, NA, 2))
#> [1,"NA",2]
Assuming that:
- you are working with data.frame (you want to filter data.frame) and you use
dplyr::filter
(very important as by the default, if you filter by known value, missing values are dropped, different than in e.g. c(1, NA)[c(1, NA) == 1]
, where NA values will be preserved).
You can try something like this:
library(shiny)
library(dplyr)
is_val <- function(value, set) {
if (all(value == "NA")) {
is.na(set)
} else {
if (any(value == "NA")) {
set %in% value | is.na(set)
} else {
set %in% value
}
}
}
choices <- data.frame(col = c(1, NA, 2, 1, 3, 1, 2))
ui <- fluidPage(
selectInput("column_name", "Choose", choices = unique(choices$col), multiple = TRUE),
tableOutput("display_val")
)
server <- function(input, output, session) {
output$display_val <- renderTable({
req(input$column_name)
choices |>
filter(is_val(input$column_name, col))
})
}
shinyApp(ui, server)
The crucial is function is_val
of course, which has different modes depending of if the "NA
" was picked up by user or not. This code could be a little simpler if pickerInput
won't have multiple = TRUE
.