---
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
library(tidyverse)
library(shiny)
library(shinycssloaders)
library(qgraph)
```

```{r message=FALSE, warning=FALSE}
load("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/code/success_rate.RData")
df_success_rate <- read_csv("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/data/df_success_rate.csv")
load("/Users/smeetpoladia/Desktop/Carnegie Mellon University/Department of Statistics and Data Science/Spring_2021/36-726/HCI_Prerequisite_Relations/data_code/code/success_rate_ggm_plot.RData")
```

```{r}
df_success_rate$Workspace_2.1 <- paste(df_success_rate$Workspace_2, " (", df_success_rate$Node_2,
                                       " | ", round(df_success_rate$Weight, 4), ")", sep = "")
df_success_rate$Workspace_1.1 <- paste(df_success_rate$Workspace_1, " (", df_success_rate$Node_1,
                                       ")", sep = "")
df_success_rate <- df_success_rate %>%
  remove_missing()
```

```{r}
ui <- fluidPage(
  sidebarLayout(
    sidebarPanel(
           selectInput(inputId = "ws.1", label = "Workspace (node)",
                       choices = unique(df_success_rate$Workspace_1.1), 
                       selected = "comparing_characteristics_of_data_displays"),
    selectInput(inputId = "ws.2", label = "Related Workspace (node | partial correlation)",
                       choices = NULL)
  ),
  mainPanel(
    h4("Gaussian Graphical Model (Performance Metric - Success Rate of all First Attempts across Workspace)"),
    plotOutput("graph_ggm"),
    tableOutput("table") %>% withSpinner(color="#add8e6"),
    h4("Prerequisite Relation 1"),
    textOutput("wks1.1") %>% withSpinner(color="#add8e6"),
    textOutput("wks2.1"),
    textOutput("ss1.1"),
    textOutput("ss2.1"),
    textOutput("st1.1"),
    textOutput("st2.1"),
    textOutput("st3.1"),
    textOutput("st4.1"),
    plotOutput("graph1.1"),
    plotOutput("graph2.1"),
    h4("Prerequisite Relation 2"),
    textOutput("wks1.2") %>% withSpinner(color="#add8e6"),
    textOutput("wks2.2"),
    textOutput("ss1.2"),
    textOutput("ss2.2"),
    textOutput("st1.2"),
    textOutput("st2.2"),
    textOutput("st3.2"),
    textOutput("st4.2"),
    plotOutput("graph1.2"),
    plotOutput("graph2.2"),
  )))
server <- function(session, input, output) {
    observe({
      temp <- df_success_rate %>%
        filter(Workspace_1.1 == input$ws.1) %>%
        select(Workspace_2.1)
      updateSelectInput(session, inputId = "ws.2", label = "Related Workspace (node | partial correlation)",
                        choices = unique(temp$Workspace_2.1))
      })
  output$graph_ggm <- renderPlot({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)
    temp_col1 <- df_success_rate[temp_index, 1]
    temp_col2 <- df_success_rate[temp_index, 3]
    qgraph(ggm_success_rate, color = c(rep("#F5EAAB",2)), groups = list(temp_col1$Node_1, temp_col2$Node_2), layout = "spring",
       graph = "glasso", minimum = 0.10, legend = FALSE)
  })
  output$table <- renderTable({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     x <- rbind(ten_list[[temp_index]][[2]][[3]], ten_list[[temp_index]][[1]][[3]])
    x <- x %>%
    distinct() %>%
   remove_missing()
    dis <- c(paste("Attempted '", ten_list[[temp_index]][[1]][[1]], "' first, and then '", ten_list[[temp_index]][[1]][[2]], "'", sep = ""), 
         paste("Attempted '", ten_list[[temp_index]][[1]][[2]], "' first, and then '", ten_list[[temp_index]][[1]][[1]], "'", sep = ""), 
         paste("Attempted only '", ten_list[[temp_index]][[1]][[1]], " '", sep = ""),
         paste("Attempted only '", ten_list[[temp_index]][[1]][[2]], " '", sep = ""))
y <- dis[!dis %in% unique(x$Label)]
for (i in 1:length(y)){
  x <- x %>%
    add_row(Label = y[i], Count = 0)
}
x <- x %>%
  remove_missing()
x$Count <- as.integer(x$Count)
x <- x[match(dis, x$Label),]
x
  })
  output$ss1.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     x <- rbind(ten_list[[temp_index]][[2]][[3]], ten_list[[temp_index]][[1]][[3]])
    x <- x %>%
    distinct() %>%
   remove_missing()
    dis <- c(paste("Attempted '", ten_list[[temp_index]][[1]][[1]], "' first, and then '", ten_list[[temp_index]][[1]][[2]], "'", sep = ""), 
         paste("Attempted '", ten_list[[temp_index]][[1]][[2]], "' first, and then '", ten_list[[temp_index]][[1]][[1]], "'", sep = ""), 
         paste("Attempted only '", ten_list[[temp_index]][[1]][[1]], " '", sep = ""),
         paste("Attempted only '", ten_list[[temp_index]][[1]][[2]], " '", sep = ""))
y <- dis[!dis %in% unique(x$Label)]
for (i in 1:length(y)){
  x <- x %>%
    add_row(Label = y[i], Count = 0)
}
x <- x %>%
  remove_missing()
x$Count <- as.integer(x$Count)
x <- x[match(dis, x$Label),]
paste(x$Count[1], " student(s) attempted Workspace 1 first.", sep = "")
  })
  output$ss2.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     x <- rbind(ten_list[[temp_index]][[2]][[3]], ten_list[[temp_index]][[1]][[3]])
    x <- x %>%
    distinct() %>%
   remove_missing()
    dis <- c(paste("Attempted '", ten_list[[temp_index]][[1]][[1]], "' first, and then '", ten_list[[temp_index]][[1]][[2]], "'", sep = ""), 
         paste("Attempted '", ten_list[[temp_index]][[1]][[2]], "' first, and then '", ten_list[[temp_index]][[1]][[1]], "'", sep = ""), 
         paste("Attempted only '", ten_list[[temp_index]][[1]][[1]], " '", sep = ""),
         paste("Attempted only '", ten_list[[temp_index]][[1]][[2]], " '", sep = ""))
y <- dis[!dis %in% unique(x$Label)]
for (i in 1:length(y)){
  x <- x %>%
    add_row(Label = y[i], Count = 0)
}
x <- x %>%
  remove_missing()
x$Count <- as.integer(x$Count)
x <- x[match(dis, x$Label),]
paste(x$Count[2] + x$Count[4], " student(s) didn't attempt Workspace 1 first.", sep = "")
  })
  output$ss1.2 <- renderText({
     temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     x <- rbind(ten_list[[temp_index]][[2]][[3]], ten_list[[temp_index]][[1]][[3]])
    x <- x %>%
    distinct() %>%
   remove_missing()
    dis <- c(paste("Attempted '", ten_list[[temp_index]][[1]][[1]], "' first, and then '", ten_list[[temp_index]][[1]][[2]], "'", sep = ""), 
         paste("Attempted '", ten_list[[temp_index]][[1]][[2]], "' first, and then '", ten_list[[temp_index]][[1]][[1]], "'", sep = ""), 
         paste("Attempted only '", ten_list[[temp_index]][[1]][[1]], " '", sep = ""),
         paste("Attempted only '", ten_list[[temp_index]][[1]][[2]], " '", sep = ""))
y <- dis[!dis %in% unique(x$Label)]
for (i in 1:length(y)){
  x <- x %>%
    add_row(Label = y[i], Count = 0)
}
x <- x %>%
  remove_missing()
x$Count <- as.integer(x$Count)
x <- x[match(dis, x$Label),]
paste(x$Count[2], " student(s) attempted Workspace 1 first.", sep = "")
  })
  output$ss2.2 <- renderText({
     temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     x <- rbind(ten_list[[temp_index]][[2]][[3]], ten_list[[temp_index]][[1]][[3]])
    x <- x %>%
    distinct() %>%
   remove_missing()
    dis <- c(paste("Attempted '", ten_list[[temp_index]][[1]][[1]], "' first, and then '", ten_list[[temp_index]][[1]][[2]], "'", sep = ""), 
         paste("Attempted '", ten_list[[temp_index]][[1]][[2]], "' first, and then '", ten_list[[temp_index]][[1]][[1]], "'", sep = ""), 
         paste("Attempted only '", ten_list[[temp_index]][[1]][[1]], " '", sep = ""),
         paste("Attempted only '", ten_list[[temp_index]][[1]][[2]], " '", sep = ""))
y <- dis[!dis %in% unique(x$Label)]
for (i in 1:length(y)){
  x <- x %>%
    add_row(Label = y[i], Count = 0)
}
x <- x %>%
  remove_missing()
x$Count <- as.integer(x$Count)
x <- x[match(dis, x$Label),]
paste(x$Count[1] + x$Count[3], " student(s) didn't attempt Workspace 1 first.", sep = "")
  })
  output$wks1.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     paste("Workspace 1 is '", temp_list.1[[1]], "' and we prove whether it's a prerequisite.", sep = "")
  })
  output$wks2.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     paste("Workspace 2 is '", temp_list.1[[2]], "'.", sep = "")
  })
  output$graph1.1 <- renderPlot({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     temp_list.1[[length(temp_list.1)-1]]
  })
  output$graph2.1 <- renderPlot({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     temp_list.1[[length(temp_list.1)]]
  })
  output$st1.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     if (length(temp_list.1) <= 5){print("Data insufficient to determine prerequisite relation.")}
     else {paste(ten_list[[temp_index]][[1]][[4]],".", sep = "")}
  })
  output$st2.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     if (length(temp_list.1) <= 5){NULL}
     else {paste(ten_list[[temp_index]][[1]][[5]],".", sep = "")}
  })
  output$st3.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     if (length(temp_list.1) <= 5){NULL}
     else {paste(ten_list[[temp_index]][[1]][[6]],".", sep = "")}
  })
  output$st4.1 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.1 <- ten_list[[temp_index]][[1]]
     if (length(temp_list.1) <= 5){NULL}
     else {
     if (paste(ten_list[[temp_index]][[1]][[7]],".", sep = "")=="There is significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)."){
       paste(ten_list[[temp_index]][[1]][[7]],".", " Workspace 1 is a prerequisite for Workspace 2.", sep = "")}
       else{paste(ten_list[[temp_index]][[1]][[7]],".", " There is no prerequisite relation here.", sep = "")}
     }
  })
  output$wks1.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     paste("Workspace 1 is '", temp_list.2[[1]], "' and we prove whether it's a prerequisite.", sep = "")
  })
  output$wks2.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     paste("Workspace 2 is '", temp_list.2[[2]], "'.", sep = "")
  })
  output$graph1.2 <- renderPlot({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     temp_list.2[[length(temp_list.2)-1]]
  })
  output$graph2.2 <- renderPlot({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     temp_list.2[[length(temp_list.2)]]
  })
  output$st1.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
    temp_list.2 <- ten_list[[temp_index]][[2]] 
    if (length(temp_list.2) <= 5){print("Data insufficient to determine prerequisite relation.")}
     else {paste(ten_list[[temp_index]][[2]][[4]],".", sep = "")}
  })
  output$st2.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     if (length(temp_list.2) <= 5){NULL}
     else {paste(ten_list[[temp_index]][[2]][[5]],".", sep = "")}
  })
  output$st3.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     if (length(temp_list.2) <= 5){NULL}
     else {paste(ten_list[[temp_index]][[2]][[6]],".", sep = "")}
  })
  output$st4.2 <- renderText({
    temp_index <- which(df_success_rate$Workspace_1.1 == input$ws.1 & df_success_rate$Workspace_2.1 == input$ws.2)  
     temp_list.2 <- ten_list[[temp_index]][[2]]
     if (length(temp_list.2) <= 5){NULL}
     else {
       if (paste(ten_list[[temp_index]][[2]][[7]],".", sep = "")=="There is significant difference in mean success rate on Workspace 2 when students first attempt Workspace 1 (at 5% level of significance)."){
       paste(ten_list[[temp_index]][[2]][[7]],".", " Workspace 1 is a prerequisite for Workspace 2.", sep = "")}
       else{paste(ten_list[[temp_index]][[2]][[7]],".", " There is no prerequisite relation here.", sep = "")}}
  })
}
```

```{r}
shinyApp(ui = ui, server = server)
```
