#load in any needed libraries here
library(tidyverse)
library(patchwork)
library(dplyr)
library(shiny)

# read in your dataset
jeopardy <- read_csv("jeopardy_2.csv")
# counting subjects present in dataset
subject_category_count <- function(user_categories) {
  
  jeopardy |>
    mutate(Category = tolower(Category)) |>
    reframe(
      Topic = user_categories,
      Category_Counts = sapply(user_categories, function(x) {
        sum(str_detect(Category, regex(x)))
      })
    ) |> 
    mutate(Proportion_All_Questions = Category_Counts / length(jeopardy$Category))
}
ui <- fluidPage(
    textInput("favorable", "What are your best categories ... type general categories after looking at the topics column in the jeopardy dataset (ex. science). Topics must be comma separated."
  ),
    h4("Question Counts and Proportion of All Questions by Topic"),
    tableOutput("topic_table"),
  
    plotOutput("wordcloud_plot")
)


server <- function(input, output, session){
  
  topic_counts <- reactive({
    
    categories <- str_split(input$favorable, ",")[[1]]
    categories <- str_trim(categories)
    
    subject_category_count(categories)
    
  })
  
  output$topic_table <- renderTable({
    topic_counts()
  })
}

shinyApp(ui, server)