Skip to content

Instantly share code, notes, and snippets.

@mrecos
Last active October 4, 2021 14:50
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save mrecos/77815fc4d464ead1c03c1e70b1ca4dda to your computer and use it in GitHub Desktop.
Save mrecos/77815fc4d464ead1c03c1e70b1ca4dda to your computer and use it in GitHub Desktop.
MUSA 508 HW email script
library(gmailr)
library(tidyverse)
library(glue)
library(kable)
library(knitr)
library(googledrive)
# need to get these values for your google drive account
gm_auth_configure(key = "LOTS_OF_NUMBERS_AND_LETTERS.apps.googleusercontent.com",secret = "SECRET_KEY")
gm_oauth_app()
# the ID of the google sheets file
grades <- drive_get(id = "LOTS_OF_NUMBERS_AND_LETTERS")
# downloads the file
hw_meta <- drive_download(grades, overwrite = TRUE)
# loads the sheet with `sheet` argument
hw_file <- readxl::read_xlsx(hw_meta$local_path, sheet = "HW5") %>%
filter(!is.na(HW_comments))
# loop over all rows, one row per student.
# I usually make the first row a test to myself to make sure it works.
for(i in seq_len(nrow(hw_file))){
cat(paste0("sending email to ", hw_file[i,"name"], "\n"))
# HW name
homework <- "#5 - Recidivism letter"
name <- hw_file[i,"name"]
grade <- hw_file[i,"HW_grade"]
# make a table of your comments
comments <- data.frame(comments = hw_file[i,"HW_comments", drop = T]) %>%
separate_rows(comments, sep = ";") %>%
mutate(comments = str_squish(comments)) %>%
kable(format = "html")
# Email Body
mail_body <- glue('Hello <b>{name}</b>! <br><br> Great job on homework {homework}, here is your grade and some comments on the assignment. <br> Your grade for this assignment is a <b> {grade} </b>. <br> Remeber, a 1 is a check-minus, 2 is a check, and 3 is a check-plus. <br> {comments} <br>If you have any questions on this grade, please let me know. <br> Thank you very much! <br> Matt <br> note: this email is generated from some R code and there are probably typos, but if there are any issues, let me know!')
# Send via Gmail
text_msg <- gm_mime() %>%
gm_to(hw_file[i,"email"]) %>%
gm_from("matthew.harris@gmail.com") %>%
gm_html_body(mail_body)
gm_send_message(text_msg)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment