Skip to content

Instantly share code, notes, and snippets.

@benmarwick
Last active October 4, 2022 22:27
Show Gist options
  • Save benmarwick/da2d99ea4dfef523e08d57f4cfeac16b to your computer and use it in GitHub Desktop.
Save benmarwick/da2d99ea4dfef523e08d57f4cfeac16b to your computer and use it in GitHub Desktop.
# This script will take a folder fill of qmd files and try to render each qmd file.
# If one qmd fails to render, the script will continue on to the next one. The
# results of all the attempts to render are collected in a data frame so we can
# easily inspect and find the files that failed to render We don't have to
# manually render individual files.
# How to use
# 1. Download all submissions from Canvas (go to assignment page, look for the
# download button near the speedgrader button), when it arrives on your
# computer it should appear as 'submissions.zip'
# 2. Unzip submissions.zip so we have a folder on our computer called 'submissions'
# 3. Put one copy of the data file into the 'submissions' folder
# 4. Run the code below and observe if any failed to render, update the rubric for those students that didn't render
# 5. delete the 'submissions' folder and 'submissions.zip'
library(fs)
library(tidyverse)
# in the RStudio menu, go to Session -> Set Working Directory ->
# Choose directory and set to the unzipped submissions folder
# make sure it has one copy of the data files
# get names of qmd files from students
student_qmd_files <- dir_ls( glob = "*.qmd")
student_html_files <- dir_ls( glob = "*html")
# delete all html files in the submissions folder so we can
# track the output of our render text
fs::file_delete(student_html_files)
# check to see if some Rmd have install.packages("XXX") in
# their Rmd, find it. Open the Rmd and comment it out by hand, save it, proceed
# You may get an error about 'incomplete final line', to solve it, open the file
# and add a few empty lines at the bottom of the qmd, save and close and try again
qmd_with_install_pkgs <-
tibble(student_qmd_files = names(student_qmd_files)) %>%
mutate(full_text_of_qmd = map(student_qmd_files, ~readLines(.x))) %>%
mutate(has_install_pkgs = map_int(full_text_of_qmd, ~sum(str_detect(.x, 'install.packages'))))
# apply the render function to all the student's qmd files
render_test <-
map_int(student_qmd_files,
~system2("Rscript", c("-e", shQuote(paste0("rmarkdown::render('", .x ,"')")))),
)
# make a data frame of the results so we can easily see
student_html_files <- tibble(html_file = names(dir_ls( glob = "*html")))
render_test_df <-
tibble(student_qmd_files = student_qmd_files,
did_it_render = !as.logical(render_test),
student_name = str_remove(student_qmd_files, "_.*")) %>%
left_join(student_html_files %>%
mutate(student_name = str_remove(html_file, "_.*")))
# inspect the results
render_test_df
# tidy up by deleting the qmd and HTML files from the 'grading' folder
fs::file_delete(student_html_files$html_file)
fs::file_delete(student_qmd_files)
# and delete the data files by hand
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment