我有一个shiny看起来像这样的应用程序
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"))
)
))))
Run Code Online (Sandbox Code Playgroud)
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)
})
})
Run Code Online (Sandbox Code Playgroud)
当我打开的应用程序,都N和M有空集。我希望找到一种方法,使每个selectizeInput框都可以在打开时选择所有选项(也称为无子集数据集),但仍显示空占位符。
我创建了这个函数,当一个或两个selectizeInput框中没有输入任何内容时,有条件地显示所有内容:
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))]
}
})
Run Code Online (Sandbox Code Playgroud)
不幸的是,似乎 ainput$N == 0不是空集input$N产生的。我试过0, NA,""但这些都不起作用。
所以问题是:input$N当selectizeInput框中没有选择任何内容时,输出是什么?
提前致谢!
编辑:感谢@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))]
}
})
Run Code Online (Sandbox Code Playgroud)
哪个完美
| 归档时间: |
|
| 查看次数: |
3890 次 |
| 最近记录: |