Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Install packages
- # if (!require("pacman")) install.packages("pacman")
- # pacman::p_load(knitr, rmarkdown, readxl, shiny, plyr)
- #install.packages(c('knitr', 'rmarkdown', 'readxl', 'shiny', 'plyr'))
- library(knitr)
- library(rmarkdown)
- library(readxl)
- library(shiny)
- library(plyr)
- library(mailR)
- reviewers_file <- ""
- institutions_file <- ""
- server <- function(input, output) {
- output$downloadData <- downloadHandler(
- filename = function() {
- paste('reviewers', '.csv', sep='')
- },
- content = function(file) {
- # input$file1 will be NULL initially. After the user selects
- # and uploads a file, it will be a data frame with 'name',
- # 'size', 'type', and 'datapath' columns. The 'datapath'
- # column will contain the local filenames where the data can
- # be found.
- inFile <- input$file1
- if (is.null(inFile) || is.null(input$markdown))
- return(NULL)
- # Import settings from file
- # config <- read.table("CONFIG.txt", sep=",", row.names=1)
- num_reviewers_per <- input$number_rev
- output_file <- "goober"
- word_template <- "bleble"
- # Import proposals and reviewers
- file.rename(inFile$datapath,
- paste(inFile$datapath, ".xlsx", sep=""))
- institutions <- read_excel(paste(inFile$datapath, ".xlsx", sep=""), sheet = 1)
- reviewers <- read_excel(paste(inFile$datapath, ".xlsx", sep=""), sheet = 2)
- # Randomize List of reviewers
- num_reviewers <- nrow(reviewers)
- num_institutions <- nrow(institutions)
- array_reviewers <- 1:num_reviewers
- # Create Blank space for reviewer names
- orig_ncol_i <- ncol(institutions)
- orig_ncol_r <- ncol(reviewers)
- add_cols_to_r <- ceiling((num_institutions * num_reviewers_per) / num_reviewers)
- institutions[,(ncol(institutions)+1):(ncol(institutions)+num_reviewers_per)] <- " "
- reviewers[,(ncol(reviewers)+1):(ncol(reviewers)+add_cols_to_r)] <- " "
- # Iterate and Assign Reviewers
- # For each institutions
- for (i in 1:num_institutions) {
- # For each reviewer slot per institution
- for (j in 1:num_reviewers_per) {
- randomized_reviewers <- sample(1:num_reviewers) # A random list
- most_empty_slots <- max(apply(reviewers, 1, function(z) sum(z == " "))) # count person with least amount of unsigned slots
- for (k in randomized_reviewers) {
- reviewer_name <- paste(as.character(reviewers[k, 1:2]), collapse=" ") # Create reviewer name
- if (reviewer_name %in% institutions[i,]) { # check that reviewer hasn't been assigned to proposal
- next
- }
- else if (sum(reviewers[k,orig_ncol_r:(orig_ncol_r + add_cols_to_r )] == " ") == 0) { # check that reviewer hasn't been assigned too many proposals
- next
- }
- else if (sum(reviewers[k, ] == " ") != most_empty_slots ) { # if this person already has many assignments
- next()
- }
- else {
- institutions[i,orig_ncol_i + j] <- reviewer_name # insert reviewer name
- reviewers[k ,min(which(reviewers[k,] == " "))] <- institutions[i,1] # insert institution name in first blank space
- break
- }
- }
- }
- }
- reviewers
- reviewers_file <- reviewers
- institutions_file <- institutions
- combined <- rbind.fill( reviewers, institutions)
- filenames <- c("1.csv", "2.csv")
- write.csv(combined, file )
- #write.csv(institutions, filenames[2] )
- #tar(tarfile=file, files=filenames)
- # if(file.exists(paste0(file, ".tar"))) {file.rename(paste0(file, ".tar"), file)}
- i = 1
- reviewer_name <- paste(as.character(reviewers[i, 1:2]), collapse=" ")
- proposal_links <- c()
- proposal_names <- c()
- for (j in orig_ncol_r:(orig_ncol_r + add_cols_to_r - sum(reviewers[i,] == " "))) {
- proposal_links <- c(proposal_links, institutions[as.character(institutions$`Name of Higher Education Institution`) %in% reviewers[i,j],2])
- proposal_names <- c(proposal_names, institutions[as.character(institutions$`Name of Higher Education Institution`) %in% reviewers[i,j],1])
- }
- # Just have them upload markdown lol
- templateFile <- file.path(tempdir(), "template.html")
- #file.create(templateFile, overwrite=TRUE)
- templateName <- templateFile
- #write(input$markdown, file=templateName)
- render(input$markdown$datapath, output_file=templateName)
- templateChar <- readChar(templateName, file.info(templateName)$size)
- send.mail(from = paste(input$username, "@gmail.com", sep=""),
- to = c("XXX@gmail.com"),
- subject = "HTML file generated using Markdown",
- body = templateChar,
- html = TRUE,
- #inline = TRUE,
- smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = input$username, passwd = input$password, ssl = TRUE),
- authenticate = TRUE,
- send = TRUE)
- }
- )
- }
- ui <- fluidPage(
- sidebarPanel(
- numericInput("number_rev", "Number of reviews per proposal:", 5, min = 1, max = 20),
- fileInput('file1', 'Choose Excel File',
- accept=c('.xls',
- '.xlsx')),
- #textAreaInput("markdown", "Email Template", "*Copy and Paste Here*", width ="800px"),
- fileInput('markdown', 'Choose Template (Rmd) File',
- accept = c('.Rmd')),
- textInput("username", "Gmail Username", "XXX@gmail.com"),
- passwordInput("password", "Gmail Password"),
- tags$hr()
- ),
- mainPanel(
- downloadButton('downloadData', 'Submit')
- )
- )
- shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement