簡體   English   中英

如何使用 R 閃亮過濾數據

[英]How to Filter data with R shiny

我有一個數據庫,包括

  • 中學代碼(代碼)
  • 學生的性別; 1:男,2:女(Cod_Sexe)
  • 關於學生的決定; A:通過,R:失敗(決定)
  • 學生在這一年的行為; 0:壞,1:好(導管)。

這些數據分為 3 年:2008、2011 和 2014。我使用 R 閃亮,我創建了性別、決策和行為的圖表,只顯示中學和學生的數量。 現在我想讓用戶根據某一年過濾這些數據。 這是代碼:

用戶界面

 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:中學編號/stunbr:學生編號/plot_decision:決策直方圖(A/R)/genderdonut:性別分布的甜甜圈圖/Conduite:行為的甜甜圈圖/YearSelect:從數據庫創建的過濾器

服務器

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

我將過濾器應用於中學的數量,當我每年單獨檢查時它起作用,但是當我檢查所有框時它不會返回總數。 另一方面,我不知道如何將它應用於圖表。 此外,當我運行應用程序時,我收到以下錯誤:

警告:錯誤: filter()輸入問題..1 x 輸入..1的大小必須是 111613 或 1,而不是大小 0。 i 輸入..1Test2008$Year == input$YearSelect 170:(在未選中任何框時發生)

Test2008$Year == input$YearSelect 中的警告:較長的對象長度不是較短對象長度的倍數

虛擬數據

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

如果你想讓你的繪圖反應性,你需要使用你的反應性數據集TestFilter而不是你的靜態 data.frame Test2008來創建繪圖。

我不確定這是否是你想要的邏輯,但它應該讓你開始。

請檢查以下內容:

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