Similarity to solution

download as .csv
show with app
  • app.R
#
# This is a Shiny web application. You can run the application by clicking
# the 'Run App' button above.
#
# Find out more about building applications with Shiny here:
#
#    http://shiny.rstudio.com/
#

library(shiny)
library(SimilaR)
library(reticulate)
library(dplyr)
library(stringr)
library(shinyjs)

source_python('helpers.py')

# Define UI for application that shows table of program similarities
ui <- fluidPage(
  useShinyjs(),
  # includeScript("../../../Matomo-qhelp.js"),
  
    # Application title
    titlePanel("Similarity to solution"),

    # Sidebar with document uploads and options
    sidebarLayout(
        sidebarPanel(
            fileInput("students", "Please upload student attempts and solution", multiple = TRUE),
            #Add similarity options
            radioButtons("simmeasure", "Which type of similarity would you like?", choices = c(Symmetric = "sym", "Amount of solution in submission" = "solinsub", "Amount of submission in solution" = "subinsol")),
            radioButtons("specific", "Would you like all comparisons or to a single file?", choices = c("File" = "file", "All" = "all")),
            textInput("solution", "What is the file name of the file you would like to compare?"),
            downloadButton("dwld_simtable", "download as .csv")
        ),
        
        # Show the table of program similarities
        mainPanel(
            DT::dataTableOutput("simtable")
        )
    )
)

# Define server logic required
server <- function(input, output) {
  shinyjs::runjs('toggleCodePosition();')
  tab = reactive({
        if(!is.null(input$students)){
            # Finds temp directory
            fold = dirname(toString(input$students[[1,"datapath"]]))
            # Uses Python helper function to make every program a function of itself, also renames functions as the
            # original name of file
            namefunchelper(fold, input$students$name)
            
            # Measures similarity by selected similarity metric
            if(input$simmeasure == "sym"){
                base = SimilaR_fromDirectory(fold, returnType = 'data.frame', aggregation = 'sym')}
            else{base = SimilaR_fromDirectory(fold, returnType = 'data.frame', aggregation = 'both')}
    
            
            if(input$specific =="file"){
                # Only includes comparisons to the sample solution
                base = base %>% filter((grepl(as.character(input$solution), base$name1))
                                       |(grepl(as.character(input$solution), base$name2)))
                
                # Locates point of comparison and name of file being analysed
                base$oneortwo = ifelse(grepl(as.character(input$solution), base$name1), 1, 2)
                base$SubmissionName = ifelse(grepl(as.character(input$solution), base$name1), base$name2, base$name1) 
                
                # Puts all comparisons into one column
                if(input$simmeasure == "sym"){
                    base$similarity = base$SimilaR
                } else if(input$simmeasure == "solinsub"){
                    base$similarity = ifelse(base$oneortwo == 1, base$SimilaR12, base$SimilaR21)
                } else if(input$simmeasure == "subinsol"){
                    base$similarity = ifelse(base$oneortwo == 1, base$SimilaR21, base$SimilaR12)
                }
                
                # Only shows basic information
                final = base %>% select(SubmissionName, similarity, decision)
            }
            else{
                final = base
            }
            
            final
        }
    })
    output$simtable <- DT::renderDataTable(tab())
    
    output$dwld_simtable = downloadHandler(filename = function(){paste("SimilarityMeasures",Sys.Date(), ".csv", sep="")},
                                           content = function(file){write.csv(tab(), file)})
}

# Run the application 
shinyApp(ui = ui, server = server)