【发布时间】:2017-04-22 05:57:38
【问题描述】:
这是一个工作模板:
require(data.table)
require(shiny)
require(ggplot2)
x <- data.table(v1 = sample(letters[1:5], 100, replace = T),
v2 = sample(letters[1:2], 100, replace = T),
v3 = runif(100, 0, 1))
ui <- fluidPage(
sidebarPanel(
selectInput("in1", "Choice v1", selected = "All", choices = c("All" = list(letters[1:5]))),
selectInput("in2", "Choice v2", selected = "a", choices = letters[1:2])
),
mainPanel(
plotOutput("out1")
)
)
server <- function(input, output){
output$out1 <- renderPlot({
ggplot(x[v1 %in% input$in1 & v2 %in% input$in2], aes(x = v3)) +
geom_density(fill = "dodgerblue4", alpha = .7) +
theme_light()
})
}
runApp(shinyApp(ui, server))
这里的问题是我想允许在变量中选择值的子集。行selectInput("in1", "Choice v1", selected = "All", choices = c("All" = list(letters[1:5]))) 旨在将letters[1:5] 传递给input$in1,有效地选择所有值并且不对v1 执行数据子集。
同样适用于任何其他值子集,例如选择"a_b_c" = c("a", "b", "c"),或者"All" = x[,unique(v1)]等等。闪亮的作用是将列表中包含的所有值分解,基本上实现了与预期结果相反的结果。
我知道有selectizeInput() 可以选择多个值。但是,如果我希望将所有变量的 selected = "All" 作为初始状态,这是不可行的。
【问题讨论】: