Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
r:使用复选框发布筛选数据_R_Shiny - Fatal编程技术网

r:使用复选框发布筛选数据

r:使用复选框发布筛选数据,r,shiny,R,Shiny,在我闪亮的应用程序中,我使用复选框过滤我的数据。考虑一个例子,我有一个数据集的集合和颜色:集合1包含红色和黄色的颜色,集合2只包含红色,而集合3只包含颜色黄色。 当我按红色和黄色过滤数据时,它返回set2(仅包含红色)、SET3(仅包含黄色)和set1(包含红色和黄色)。然而,我期望的输出是,选择红色和黄色只会返回包含这两种颜色的组(set1) 我希望返回的组与我的复选框选择完全匹配。 i、 e.只选中红色将返回仅包含红色的组,只选中黄色将返回仅包含黄色的组,选中红色和黄色将返回包含红色和黄色的

在我闪亮的应用程序中,我使用复选框过滤我的数据。考虑一个例子,我有一个数据集的集合和颜色:集合1包含红色和黄色的颜色,集合2只包含红色,而集合3只包含颜色黄色。 当我按红色和黄色过滤数据时,它返回set2(仅包含红色)、SET3(仅包含黄色)和set1(包含红色和黄色)。然而,我期望的输出是,选择红色和黄色只会返回包含这两种颜色的组(set1)

我希望返回的组与我的复选框选择完全匹配。 i、 e.只选中红色将返回仅包含红色的组,只选中黄色将返回仅包含黄色的组,选中红色和黄色将返回包含红色和黄色的组

此示例的代码(这是我的应用程序的简化版本)如下所示:

我的服务器:

library(dplyr)
library(shiny)
shinyServer(function(input, output) {

  data <- structure(list(Set = c("Set1", "Set1", "Set1", "Set2", "Set2", 
                             "Set2", "Set3", "Set3", "Set3", "Set4"), Colour = c("red", "red", 
                                                                                 "yellow", "red", "red", "red", "yellow", "yellow", "yellow", 
                                                                                 "blue")), class = "data.frame", row.names = c(NA, -10L))

  output$choose_colour <- renderUI({

    colour.names <- as.vector( unique(data$Colour ))

    checkboxGroupInput("colours", "Choose Colours to Include", 
                       choices  = colour.names)
  })

  model.data <- reactive({

    filtered_data <- data %>% group_by(Set) %>% filter(all(Colour%in% input$colours))
    filtered_data

  })

  output$filteredData <- renderDataTable({
    filtered_data <- model.data()
    filtered_data
  })

})
库(dplyr)
图书馆(闪亮)
shinyServer(功能(输入、输出){

数据我改变了顺序,改变了所有的顺序

library(dplyr)
library(shiny)
library(purrr)
server <- shinyServer(function(input, output) {
  
  data <- structure(list(Set = c("Set1", "Set1", "Set1", "Set2", "Set2", 
                                 "Set2", "Set3", "Set3", "Set3", "Set4"), Colour = c("red", "red", 
                                                                                     "yellow", "red", "red", "red", "yellow", "yellow", "yellow", 
                                                                                     "blue")), class = "data.frame", row.names = c(NA, -10L))
  
  output$choose_colour <- renderUI({
    
    colour.names <- as.vector( unique(data$Colour ))
    
    checkboxGroupInput("colours", "Choose Colours to Include", 
                       choices  = colour.names)
  })
  
  model.data <- reactive({
    
    filtered_data <- data %>% group_by(Set) %>% filter(all(Colour %in% input$colours) & all(input$colours %in%Colour))
    filtered_data
    
  })
  
  output$filteredData <- renderDataTable({
    filtered_data <- model.data()
    filtered_data
  })
  
})

ui <- fluidPage(
  
  
  mainPanel(
    uiOutput('choose_colour'),
    
    
    dataTableOutput('filteredData')
    
  )
)

shinyApp(ui,server)
库(dplyr)
图书馆(闪亮)
图书馆(purrr)

服务器我希望返回的数据与复选框中的筛选器完全匹配。例如,如果我选择红色,则返回仅包含红色的组。如果我选择黄色,则返回仅包含黄色的组。如果我选择红色和黄色,则返回包含红色和黄色的组。我尝试将其翻转,但最终失败p正如你之前通过双向检查解决问题一样,如果不是在闪亮的应用程序上,你的问题会简单得多
library(dplyr)
library(shiny)
library(purrr)
server <- shinyServer(function(input, output) {
  
  data <- structure(list(Set = c("Set1", "Set1", "Set1", "Set2", "Set2", 
                                 "Set2", "Set3", "Set3", "Set3", "Set4"), Colour = c("red", "red", 
                                                                                     "yellow", "red", "red", "red", "yellow", "yellow", "yellow", 
                                                                                     "blue")), class = "data.frame", row.names = c(NA, -10L))
  
  output$choose_colour <- renderUI({
    
    colour.names <- as.vector( unique(data$Colour ))
    
    checkboxGroupInput("colours", "Choose Colours to Include", 
                       choices  = colour.names)
  })
  
  model.data <- reactive({
    
    filtered_data <- data %>% group_by(Set) %>% filter(all(Colour %in% input$colours) & all(input$colours %in%Colour))
    filtered_data
    
  })
  
  output$filteredData <- renderDataTable({
    filtered_data <- model.data()
    filtered_data
  })
  
})

ui <- fluidPage(
  
  
  mainPanel(
    uiOutput('choose_colour'),
    
    
    dataTableOutput('filteredData')
    
  )
)

shinyApp(ui,server)