简体   繁体   English

如何使用 R 闪亮过滤数据

[英]How to Filter data with R shiny

I have a database consisting of我有一个数据库,包括

  • Code of middle schools (code)中学代码(代码)
  • Gender of the students ;学生的性别; 1:Male,2:Female (Cod_Sexe) 1:男,2:女(Cod_Sexe)
  • Decision Concerning the student;关于学生的决定; A: Pass , R:Fail (Decision) A:通过,R:失败(决定)
  • Behaviour of the student during the year ;学生在这一年的行为; 0: Bad , 1:Good (Conduite). 0:坏,1:好(导管)。

This data is divided over 3 years: 2008,2011 and 2014. I am using R shiny, I created graphs for gender, decision and behaviour and just display the number of middle schools and students.这些数据分为 3 年:2008、2011 和 2014。我使用 R 闪亮,我创建了性别、决策和行为的图表,只显示中学和学生的数量。 Now I want to enable the user to filter this data according to a certain year.现在我想让用户根据某一年过滤这些数据。 Here's the code:这是代码:

USER INTERFACE用户界面

 ui <- fluidPage( 
    textOutput("inst_nbr"),
    textOutput("stunbr"),
    plotOutput("plot_decision"),
    plotOutput("genderdonut"),
    plotOutput("Conduite"),
    checkboxGroupInput(inputId = "YearSelect", "Select the corresponding year", 
      choices = levels(factor(Test2008$Year)), selected = levels(factor(Test2008$Year)))
)

inst_nbr : middle school number / stunbr: students number / plot_decision: histogram of the decisions(A/R) / genderdonut:donut chart for the gender distribution / Conduite:donut chart for the behavior / YearSelect: filter created from the database inst_nbr:中学编号/stunbr:学生编号/plot_decision:决策直方图(A/R)/genderdonut:性别分布的甜甜圈图/Conduite:行为的甜甜圈图/YearSelect:从数据库创建的过滤器

Server服务器

# CREATE YEAR FILTER
    TestFilter <- reactive({ # <-- Reactive function here
        Test2008 %>% 
            filter(Test2008$Year == input$YearSelect)
    })

# NBR OF INSTITUTIONS
    output$inst_nbr=  renderText({
        Test=TestFilter()
        length(unique(x = Test$Code))
        })

# PLOT DECISION DIAGRAM
    # % of each decison
    Per_A= 100*length(which(Test2008$Decision=='A'))/length(Test2008$Decision)
    Per_R= 100*length(which(Test2008$Decision=='R'))/length(Test2008$Decision)
    DecisionName=c('Accepté','Refusé')
    DecisionFraction=c(Per_A,Per_R)
    # Plot of decisions
    output$plot_decision=renderPlot({
        Test=TestFilter()
        barplot(height= DecisionFraction, names = DecisionName )
    })   

I applied the filter to the number of middle school, it works when I check each year alone, however when I check all the boxes it doesn't return the total.我将过滤器应用于中学的数量,当我每年单独检查时它起作用,但是当我检查所有框时它不会返回总数。 I don't know how to apply it to the graph on the other hand.另一方面,我不知道如何将它应用于图表。 In addition when I run the application I get these errors:此外,当我运行应用程序时,我收到以下错误:

Warning: Error in : Problem with filter() input ..1 .警告:错误: filter()输入问题..1 x Input ..1 must be of size 111613 or 1, not size 0. i Input ..1 is Test2008$Year == input$YearSelect . x 输入..1的大小必须是 111613 或 1,而不是大小 0。 i 输入..1Test2008$Year == input$YearSelect 170: (Occurs when no box is checked) 170:(在未选中任何框时发生)

And

Warning in Test2008$Year == input$YearSelect : longer object length is not a multiple of shorter object length Test2008$Year == input$YearSelect 中的警告:较长的对象长度不是较短对象长度的倍数

DUMMY DATA虚拟数据

Code     Cod_sexe    conduite   decision    year 
1002        1           1           A       2008
2065        1           0           R       2008
1002        2           1           A       2008
4225        2           1           R       2011        
2005        1           1           R       2011
1003        2           0           R       2014
2005        2           0           A       2014

If you want to make your plot reactive, you need to use your reactive dataset TestFilter rather than your static data.frame Test2008 to create the plot.如果你想让你的绘图反应性,你需要使用你的反应性数据集TestFilter而不是你的静态 data.frame Test2008来创建绘图。

I'm nnot sure if this is the logic you want, but it should get you started.我不确定这是否是你想要的逻辑,但它应该让你开始。

Please check the following:请检查以下内容:

library(shiny)
library(dplyr)

Test2008 <- data.frame(
  stringsAsFactors = FALSE,
  Code = c(1002L, 2065L, 1002L, 4225L, 2005L, 1003L, 2005L),
  Cod_sexe = c(1L, 1L, 2L, 2L, 1L, 2L, 2L),
  conduite = c(1L, 0L, 1L, 1L, 1L, 0L, 0L),
  Decision = c("A", "R", "A", "R", "R", "R", "A"),
  Year = c(2008L, 2008L, 2018L, 2011L, 2011L, 2014L, 2014L)
)

ui <- fluidPage(
  checkboxGroupInput(
    inputId = "YearSelect",
    "Select the corresponding year",
    choices = unique(Test2008$Year),
    selected = unique(Test2008$Year)
  ),
  textOutput("inst_nbr"),
  textOutput("stunbr"),
  plotOutput("plot_decision"),
  plotOutput("genderdonut"),
  plotOutput("Conduite")
)

server <- function(input, output, session) {
  # CREATE YEAR FILTER
  TestFilter <- reactive({
    Test2008 %>% filter(Year %in% input$YearSelect)
  })
  
  # NBR OF INSTITUTIONS
  output$inst_nbr = renderText({
    length(unique(TestFilter()$Code))
  })
  
  # PLOT DECISION DIAGRAM
  output$plot_decision = renderPlot({
    req(TestFilter())
    # % of each decison
    Per_A = 100 * length(which(TestFilter()$Decision == 'A')) / length(TestFilter()$Decision)
    Per_R = 100 * length(which(TestFilter()$Decision == 'R')) / length(TestFilter()$Decision)
    DecisionName = c('Accepté', 'Refusé')
    DecisionFraction = c(Per_A, Per_R)
    barplot(height = DecisionFraction, names = DecisionName)
  })
}

shinyApp(ui, server)

结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM