Reputation: 2368
I have an shiny
app that looks like this
ui.R
shinyUI(fluidPage(
titlePanel("Test Application"),
sidebarLayout(
sidebarPanel(
selectizeInput("N",
label = ("N"),
multiple = TRUE,
choices = NULL,
options = list(
placeholder = 'Select All Desired, Type to Search',
onInitialize = I('function() { this.setValue(""); }')
)),
selectizeInput("M",
label = "M",
multiple = TRUE,
choices = NULL,
options = list(
placeholder = 'Select All Desired, Type to Search',
onInitialize = I('function() { this.setValue(""); }')
))
),
mainPanel(
tabsetPanel(
tabPanel("Test Plot 1",
plotOutput("testPlot1"))
)
))))
server.R
library(data.table)
library(ggplot2)
options("stringsAsFactors" = FALSE, datatable.verbose=TRUE, datatable.auto.index=TRUE)
testDT <- data.table(
L = (1:32),
M = rep(letters[23:26], each = 64),
N = rep(LETTERS[1:4], times = 20, each = 512),
O = rnorm(2048, 1))
testDT$L <- factor(testDT$L, levels = seq(from = 1, to = 32, by = 1))
shinyServer(function(input, output, session) {
updateSelectizeInput(session, "N",
server = TRUE,
choices = sort(unique(testDT$N)),
)
updateSelectizeInput(session, "M",
server = TRUE,
choices = unique(testDT$M),
)
testDT1 <- reactive({
if (input$N == 0 & input$M == 0){
testDT
} else if(input$N != 0 & input$M == 0) {
testDT[eval(call("%in%", as.name("N"), input$N))]
} else if(input$M != 0 & input$N == 0) {
testDT[eval(call("%in%", as.name("M"), input$M))]
} else {
testDT[eval(call("%in%", as.name("N"), input$N)) &
eval(call("%in%", as.name("M"), input$M))]
}
})
output$testTable <- renderDataTable(testDT1())
output$testPlot1 <- renderPlot({
p <- ggplot(testDT1(), aes(L,O)) +
geom_boxplot(aes(fill = N)) +
theme_bw() +
theme(legend.position = "top", legend.title=element_blank()) +
facet_grid(M ~ ., scales = "free") +
labs(x = "L", y = "O")
print(p)
})
})
When I open the app, both N
and M
have empty sets. I am hoping to find a way that each selectizeInput
box will have all options selected on open (AKA no subsetted dataset) but also still show the empty placeholder.
I created this function to conditionally display everything when there is nothing inputted into one or both of the selectizeInput
boxes:
testDT1 <- reactive({
if (input$N == 0 & input$M == 0){
testDT
} else if(input$N != 0 & input$M == 0) {
testDT[eval(call("%in%", as.name("N"), input$N))]
} else if(input$M != 0 & input$N == 0) {
testDT[eval(call("%in%", as.name("M"), input$M))]
} else {
testDT[eval(call("%in%", as.name("N"), input$N)) &
eval(call("%in%", as.name("M"), input$M))]
}
})
Unfortunately, it seems as if a input$N == 0
is not what an empty set of input$N
produces. I've tried 0
, NA
, and ""
but none of those work.
So the question is: What is the output for input$N
when nothing is selected in the selectizeInput
box?
Thanks in advance!
EDIT: Thanks to @Romain, I modified the function to produce this:
testDT1 <- reactive({
if (is.null(input$N) & is.null(input$M)){
testDT
} else if(!is.null(input$N) & is.null(input$M)) {
testDT[eval(call("%in%", as.name("N"), input$N))]
} else if(is.null(input$N) & !is.null(input$M)) {
testDT[eval(call("%in%", as.name("M"), input$M))]
} else {
testDT[eval(call("%in%", as.name("N"), input$N)) &
eval(call("%in%", as.name("M"), input$M))]
}
})
Which works perfectly
Upvotes: 7
Views: 5506
Reputation: 771
When nothing is selected the input is NULL.
Use is.null() if you want to check if nothing is selected.
Upvotes: 10