2016-12-07 112 views
0

这里是一个工作的模板:这里矢量闪亮选择:: selectInput()

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()选择多个值。但是,如果我想将所有变量作为初始状态,则这是不可行的。

回答

3

会这样的工作?

#rm(list=ls()) 
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",letters[1:5])), 
    selectInput("in2", "Choice v2", selected = "a", choices = letters[1:2]) 
), 
    mainPanel(
    plotOutput("out1") 
) 
) 

server <- function(input, output){ 
    output$out1 <- renderPlot({ 
    value <- input$in1 
    if(value == "All"){ 
     value <- letters[1:5] 
    } 
    ggplot(x[v1 %in% value & v2 %in% input$in2], aes(x = v3)) + 
     geom_density(fill = "dodgerblue4", alpha = .7) + 
     theme_light() 
    }) 
} 

runApp(shinyApp(ui, server)) 

enter image description here

+0

是的,会的。这是可能的解决方法,我应该在后提及。如果你有10个变量全部设置为“全部”作为初始状态,它最终会膨胀很多临时变量。并且在每个变量选择中有几个级别的聚合......否则它是完全有效的选项。我只是希望在闪亮的框架内提供直接解决方案。 –

0

shiny支持多个值中选择selectInput。您需要设置multiple = TRUEselectize = FALSE。我认为这会为你提供你想要的功能。

然后,您可以使choicesselected变量相同以预选所有变量。如果您需要使用“全部”功能,则需要添加一个操作按钮以运行updateSelectInput。通过编写一个模块可以将这两个功能结合起来。

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 = letters[1:5], 
       choices = letters[1:5], 
       multiple = TRUE, 
       selectize = FALSE), 
    selectInput("in2", "Choice v2", 
       selected = letters[1:2], 
       choices = letters[1:2], 
       multiple = TRUE, 
       selectize = FALSE) 
), 
    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)) 
+0

这不是我想去的方向。从自己的意义上说,它与选择选项非常相似,但它不能很好地扩展。我的意思是,这部分问题不是一个好的解决方案:''a_b_c“= c(”a“,”b“,”c“)'。想象一下,如果最初你有基本的城市聚合。然后,我想选择一个国家和另一个级别的聚合 - 按大陆。最重要的是 - 全部或全球。 –

+0

我正在考虑为映射创建一个单独的数据表,并参考通过它的选择......目前看来是唯一可行的解​​决方案。 –

+1

在这种情况下,我认为你用猪排的答案可能会更好。我质疑在选择框中添加“a_b_c”选项的价值。如果您只允许用户一次选择一个值,则可能需要提供每个选项的排列方式,这会使用户界面膨胀 - 这种成本比膨胀服务器端代码的成本更高(在至少在我看来)。 – Benjamin

0

在阅读其他答案并想知道可能的干净和紧凑的解决方法时,这里是我想出的。有一个干净的方法来添加新的变量是至关重要的。

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)) 

map.dt <- function(x, variables){ 
    map.out <- data.table(name = character(), variable = character(), value = character()) 
    for(i in variables){ 
    map.out <- rbind(map.out, 
        data.table(name = x[,sort(as.character(na.omit(unique(get(i)))))], 
           variable = i, 
           value = x[,sort(as.character(na.omit(unique(get(i)))))]), 
        data.table(name = "All", 
           variable = i, 
           value = x[,sort(as.character(na.omit(unique(get(i)))))])) 
    } 
    return(map.out) 
} 

y <- map.dt(x, c("v1", "v2")) 

ui <- fluidPage(
    sidebarPanel(
    selectInput("in1", "Choice v1", selected = "All", choices = c("All", letters[1:5])), 
    selectInput("in2", "Choice v2", selected = "All", choices = c("All", letters[1:2])) 
), 
    mainPanel(
    plotOutput("out1") 
) 
) 

server <- function(input, output){ 
    output$out1 <- renderPlot({ 
    ggplot(x[v1 %in% y[variable == "v1" & name == input$in1, value] & 
       v2 %in% y[variable == "v2" & name == input$in2, value]], 
      aes(x = v3)) + 
     geom_density(fill = "dodgerblue4", alpha = .7) + 
     theme_light() 
    }) 
} 

runApp(shinyApp(ui, server)) 

基本上,它增加了一个通过函数生成的中间映射表。