Advertisement
Guest User

Untitled

a guest
Apr 30th, 2017
760
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.57 KB | None | 0 0
  1. # Install packages
  2. # if (!require("pacman")) install.packages("pacman")
  3. # pacman::p_load(knitr, rmarkdown, readxl, shiny, plyr)
  4. #install.packages(c('knitr', 'rmarkdown', 'readxl', 'shiny', 'plyr'))
  5. library(knitr)
  6. library(rmarkdown)
  7. library(readxl)
  8. library(shiny)
  9. library(plyr)
  10. library(mailR)
  11.  
  12. reviewers_file <- ""
  13. institutions_file <- ""
  14.  
  15. server <- function(input, output) {
  16. output$downloadData <- downloadHandler(
  17.  
  18.  
  19.  
  20. filename = function() {
  21. paste('reviewers', '.csv', sep='')
  22. },
  23. content = function(file) {
  24.  
  25. # input$file1 will be NULL initially. After the user selects
  26. # and uploads a file, it will be a data frame with 'name',
  27. # 'size', 'type', and 'datapath' columns. The 'datapath'
  28. # column will contain the local filenames where the data can
  29. # be found.
  30.  
  31. inFile <- input$file1
  32.  
  33. if (is.null(inFile) || is.null(input$markdown))
  34. return(NULL)
  35.  
  36. # Import settings from file
  37. # config <- read.table("CONFIG.txt", sep=",", row.names=1)
  38. num_reviewers_per <- input$number_rev
  39. output_file <- "goober"
  40. word_template <- "bleble"
  41.  
  42. # Import proposals and reviewers
  43. file.rename(inFile$datapath,
  44. paste(inFile$datapath, ".xlsx", sep=""))
  45. institutions <- read_excel(paste(inFile$datapath, ".xlsx", sep=""), sheet = 1)
  46. reviewers <- read_excel(paste(inFile$datapath, ".xlsx", sep=""), sheet = 2)
  47.  
  48. # Randomize List of reviewers
  49. num_reviewers <- nrow(reviewers)
  50. num_institutions <- nrow(institutions)
  51. array_reviewers <- 1:num_reviewers
  52.  
  53. # Create Blank space for reviewer names
  54. orig_ncol_i <- ncol(institutions)
  55. orig_ncol_r <- ncol(reviewers)
  56. add_cols_to_r <- ceiling((num_institutions * num_reviewers_per) / num_reviewers)
  57.  
  58. institutions[,(ncol(institutions)+1):(ncol(institutions)+num_reviewers_per)] <- " "
  59. reviewers[,(ncol(reviewers)+1):(ncol(reviewers)+add_cols_to_r)] <- " "
  60.  
  61. # Iterate and Assign Reviewers
  62.  
  63. # For each institutions
  64. for (i in 1:num_institutions) {
  65. # For each reviewer slot per institution
  66. for (j in 1:num_reviewers_per) {
  67. randomized_reviewers <- sample(1:num_reviewers) # A random list
  68. most_empty_slots <- max(apply(reviewers, 1, function(z) sum(z == " "))) # count person with least amount of unsigned slots
  69. for (k in randomized_reviewers) {
  70. reviewer_name <- paste(as.character(reviewers[k, 1:2]), collapse=" ") # Create reviewer name
  71. if (reviewer_name %in% institutions[i,]) { # check that reviewer hasn't been assigned to proposal
  72. next
  73. }
  74. 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
  75. next
  76. }
  77. else if (sum(reviewers[k, ] == " ") != most_empty_slots ) { # if this person already has many assignments
  78. next()
  79. }
  80. else {
  81. institutions[i,orig_ncol_i + j] <- reviewer_name # insert reviewer name
  82. reviewers[k ,min(which(reviewers[k,] == " "))] <- institutions[i,1] # insert institution name in first blank space
  83. break
  84. }
  85. }
  86. }
  87. }
  88. reviewers
  89. reviewers_file <- reviewers
  90. institutions_file <- institutions
  91. combined <- rbind.fill( reviewers, institutions)
  92.  
  93. filenames <- c("1.csv", "2.csv")
  94. write.csv(combined, file )
  95. #write.csv(institutions, filenames[2] )
  96.  
  97. #tar(tarfile=file, files=filenames)
  98.  
  99. # if(file.exists(paste0(file, ".tar"))) {file.rename(paste0(file, ".tar"), file)}
  100. i = 1
  101. reviewer_name <- paste(as.character(reviewers[i, 1:2]), collapse=" ")
  102. proposal_links <- c()
  103. proposal_names <- c()
  104. for (j in orig_ncol_r:(orig_ncol_r + add_cols_to_r - sum(reviewers[i,] == " "))) {
  105. proposal_links <- c(proposal_links, institutions[as.character(institutions$`Name of Higher Education Institution`) %in% reviewers[i,j],2])
  106. proposal_names <- c(proposal_names, institutions[as.character(institutions$`Name of Higher Education Institution`) %in% reviewers[i,j],1])
  107. }
  108.  
  109. # Just have them upload markdown lol
  110. templateFile <- file.path(tempdir(), "template.html")
  111. #file.create(templateFile, overwrite=TRUE)
  112. templateName <- templateFile
  113. #write(input$markdown, file=templateName)
  114. render(input$markdown$datapath, output_file=templateName)
  115.  
  116. templateChar <- readChar(templateName, file.info(templateName)$size)
  117.  
  118. send.mail(from = paste(input$username, "@gmail.com", sep=""),
  119. to = c("XXX@gmail.com"),
  120. subject = "HTML file generated using Markdown",
  121. body = templateChar,
  122. html = TRUE,
  123. #inline = TRUE,
  124. smtp = list(host.name = "smtp.gmail.com", port = 465, user.name = input$username, passwd = input$password, ssl = TRUE),
  125. authenticate = TRUE,
  126. send = TRUE)
  127.  
  128.  
  129. }
  130.  
  131. )
  132.  
  133.  
  134. }
  135.  
  136. ui <- fluidPage(
  137. sidebarPanel(
  138. numericInput("number_rev", "Number of reviews per proposal:", 5, min = 1, max = 20),
  139. fileInput('file1', 'Choose Excel File',
  140. accept=c('.xls',
  141. '.xlsx')),
  142. #textAreaInput("markdown", "Email Template", "*Copy and Paste Here*", width ="800px"),
  143. fileInput('markdown', 'Choose Template (Rmd) File',
  144. accept = c('.Rmd')),
  145. textInput("username", "Gmail Username", "XXX@gmail.com"),
  146. passwordInput("password", "Gmail Password"),
  147. tags$hr()
  148. ),
  149. mainPanel(
  150. downloadButton('downloadData', 'Submit')
  151. )
  152. )
  153.  
  154. shinyApp(ui = ui, server = server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement