#subor s nazvom server.r --> nižšie je uvedeny subor ui.r library(tidyverse) library(ggplot2) library(dplyr) library(shiny) data <- read_delim("kompletdata.csv", delim=";") names(data) <- c("Project Reference", "Academic Year", "Mobility Start Month", "Mobility End Month", "Mobility Duration", "Activity (mob)", "Field of Education", "Participant Nationality", "Education Level", "Participant Gender", "Participant Profile", "Special Needs", "Fewer Opportunities", "GroupLeader", "Participant Age", "Sending Country Code", "Sending City", "Sending Organization", "Sending Organisation Erasmus Code", "Receiving Country Code", "Receiving City", "Receiving Organization", "Receiving Organisation Erasmus Code", "Participants") df <- data.frame(Age = c(data$`Participant Age`), Gender = c(data$`Participant Gender`), Year = c(data$`Academic Year`)) #vytvorim si data frame len so stlpcami vek, pohlavie a poÄŤet participantov df$Age <- as.integer(df$Age) #zmenim data typ chr na int aby sa mi s vekom dalo pracovaĹĄ ako s ÄŤĂ­slom df <- df[(df$Age != "-"),] #v data frame odstranim všetky nevyplnenĂ© hodnoty v stÄľpci vek df <- df[(df$Age >= 1 & df$Age <= 100),] #taktieĹľ odstránim chybnĂ© hodnoty, ktorĂ© sĂş menšie ako 1 a väčšie ako 100 df <- filter(df, Gender != "Undefined") #odstránim tieĹľ hodnoty, ktorĂ© sĂş v stÄşpci pohlavie uvedenĂ© ako nedefinovanĂ© df <- arrange(df, Age) #usporiadam data frame podÄľa stlpca vek labs <- c(paste(seq(0, 75, by = 5), seq(0+5, 80, by = 5), #vytvorim si vekove kategorie sep = "-")) df$AgeGroup <- cut(df$Age, breaks = c(seq(0, 75, by = 5), Inf), labels = labs, right = FALSE) #priradĂ­m vek vekovej kategĂłrii df <- df %>% group_by(AgeGroup, Gender, Year) %>% tally() #jednotlivĂ˝ch účastnĂ­kov podÄľa veku zaradĂ­m do prĂ­slušnej vekovej skupiny shinyServer( function(input, output){ output$pyramidplot <- renderPlot({ Filtered <- reactive(df[which(df$Year == input$year),]) g <- ggplot(data = Filtered(), aes(x = AgeGroup, fill = Gender, y = ifelse(test = Gender == "Male", yes = -n, no = n))) + geom_bar(stat = "identity") + scale_y_continuous(labels = function(x) { x = abs(x) format(x,scientific = FALSE) }, limits = max(Filtered()$n) * c(-1,1)) + ylab("Number of Participants") + xlab("Age Group") + coord_flip() g }) } ) #subor ui.r library(shiny) shinyUI(fluidPage( titlePanel(title = 'Population Pyramid'), sidebarLayout( sidebarPanel(h3("Erasmus age group vs. gender"), selectInput("year", "Select the year", choices = c("2014-2015", "2015-2016", "2016-2017", "2017-2018", "2018-2019", "2019-2020"), selected = '2019-2020' ) ), mainPanel( plotOutput("pyramidplot") ) ) ) )