I have a database consisting of
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, 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
. x Input..1
must be of size 111613 or 1, not size 0. i Input..1
isTest2008$Year == input$YearSelect
. 170: (Occurs when no box is checked)
And
Warning in Test2008$Year == input$YearSelect : longer object length is not a multiple of shorter object length
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.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.