How to Filter graphs with R shiny?

I have a database consisting of

  • Code of middle schools (code)
  • Gender of the students (Cod_Sexe)
  • Decision Concerning the student; A: Pass , R:Fail (Decision)
  • Behaviour of the student during the year ; 0: Bad , 1:Good (Conduite).

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

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, however I don't know how to apply it to the graph.

Because Per_A, Per_R, DecisionName, and DecisionFraction are defined outside of the output$plot_decision, they are calculated on Test2008, not on TestFilter(). Try moving those variables inside the renderPlot and replacing every Test2008 with a TestFilter().

Hope that helps, but it would be easier to come up with an answer if the code was more generic and less specific to your data. Cheers,

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.