I have an shiny
app that looks like this
我有一个闪亮的应用,看起来像这样
ui.R
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
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.
当我打开应用程序时,N和M都是空集。我希望找到一种方法,让每个selectizeInput框在open(也就是没有子嗣数据集)上都有选择的选项,但仍然显示空的占位符。
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.
不幸的是,似乎输入$N = 0并不是输入$N产生的空集。我试过了0,NA,和""但都没用。
So the question is: What is the output for input$N
when nothing is selected in the selectizeInput
box?
所以问题是:当选择输入框中没有任何东西被选中时,输入$N的输出是什么?
Thanks in advance!
提前谢谢!
EDIT: Thanks to @Romain, I modified the function to produce this:
编辑:感谢@Romain,我修改了函数,生成如下内容:
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
完美的工作
1 个解决方案
#1
3
When nothing is selected the input is NULL.
当没有选择任何内容时,输入为NULL。
Use is.null() if you want to check if nothing is selected.
如果要检查是否没有选择任何内容,请使用is.null()。
#1
3
When nothing is selected the input is NULL.
当没有选择任何内容时,输入为NULL。
Use is.null() if you want to check if nothing is selected.
如果要检查是否没有选择任何内容,请使用is.null()。