gpt4 book ai didi

r - 在 selectizeInput 中未选择任何值时的 Shiny input$____ 值

转载 作者:行者123 更新时间:2023-12-04 09:12:05 26 4
gpt4 key购买 nike

我有一个 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"))
)
))))
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)
})
})

当我打开应用程序时, NM有空集。我希望找到一种方法,每个 selectizeInput框将在打开时选择所有选项(AKA 没有子集数据集),但仍显示空占位符。

我创建了这个函数来有条件地显示所有内容,当没有任何内容输入到 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))]
}
})

不幸的是,似乎 input$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))]
}
})

哪个完美

最佳答案

当未选择任何内容时,输入为 NULL。

如果要检查是否未选择任何内容,请使用 is.null()。

关于r - 在 selectizeInput 中未选择任何值时的 Shiny input$____ 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34257779/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com