library(readr)
library(ggplot2)
## Warning: package 'ggplot2' was built under R version 3.5.3
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(plotly)
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
library(shiny)

library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.5.3
## -- Attaching packages --------------------------------------------------------------------- tidyverse 1.2.1 --
## v tibble  2.0.1     v purrr   0.3.0
## v tidyr   0.8.2     v stringr 1.4.0
## v tibble  2.0.1     v forcats 0.3.0
## Warning: package 'stringr' was built under R version 3.5.3
## -- Conflicts ------------------------------------------------------------------------ tidyverse_conflicts() --
## x plotly::filter() masks dplyr::filter(), stats::filter()
## x dplyr::lag()     masks stats::lag()
df <- read.csv("responses2.csv")


df <- df %>% gather(key = 'Age.group', value = 'Answer', Science.Q1..1.pt.:Sports.Q9..1.pt.) %>% 
    separate(Age.group, c("Category", "Question Number"),sep="\\.",extra='drop')


choise_list <- unique(df$Category)
# Define UI for application that draws a histogram

ui <- fluidPage(
    
    # Application title
    titlePanel("Trivia Night Results"),
    
    # Sidebar with a slider input for number of bins 
    sidebarLayout(
        sidebarPanel(
            selectInput('category', 'category', unique(df$Category), selected='Science')),
        mainPanel(
            plotOutput('plot1')
        )
    )
)

# Define server logic required to draw a histogram
server <- function(input, output, session) {
    
    selectedData <- reactive({
        dfSlice <- df %>%
            dplyr::filter(Category == input$category) %>% 
            dplyr::group_by(Category,Team.Name) %>% 
            dplyr::summarize(Score=sum(Answer))
  
    })
    print(selectedData)
    output$plot1 <- renderPlot({
        ggplot(selectedData(), aes(x=reorder(Team.Name,-Score),y=Score))+
            geom_bar(stat="identity", fill="steelblue")+
            geom_text(aes(label=Team.Name), vjust=1.6, color="white", size=2)+
            theme(axis.text.x = element_text(angle = 90, hjust = 1))+
            xlab("Team") 
    })
}

# Run the application 
shinyApp(ui = ui, server = server)
## PhantomJS not found. You can install it with webshot::install_phantomjs(). If it is installed, please make sure the phantomjs executable can be found via the PATH variable.
Shiny applications not supported in static R Markdown documents