3

I teach a lab and I have my students write their answers in .Rmd files. For grading, I download and render them as pdfs in a batch. I use the following script to render everything and save in a file.

library(rmarkdown)

# Handy functions for concatenating strings because I want to do it like a Python
# programmer damnit!
`%s%` <- function(x,y) {paste(x,y)}
`%s0%` <- function(x,y) {paste0(x,y)}

# You should set the working directory to the one where the assignments are
# located. Also, make sure ONLY .rmd files are there; anything else may cause
# a problem.
subs <- list.files(getwd())     # Get list of files in working directory
errorfiles <- c()               # A vector for names of files that produced errors
for (f in subs) {
  print(f)
  tryCatch({
    # Try to turn the document into a PDF file and save in a pdfs subdirectory
    # (you don't need to make the subdirectory; it will be created automatically
    # if it does not exist).
    render(f, pdf_document(), output_dir = getwd() %s0% "/pdfs")
    },
           # If an error happens, complain, then save the name in errorfiles
           error = function(c) {
             warning("File" %s% "did not render!")
             warning(c)
             errorfiles <- c(errorfiles, f)
           })
}

This last assignment I forgot to set error=TRUE in the chunks, so documents will fail to compile if errors are found and I will have to go hunt those errors down and fix them. I tried to modify this code so that I set the parameter error=TRUE as default outside the document. Unfortunately, I've been working at this for hours and have found no way to do so.

How can I change this code so I can change this parameter outside the documents? (Bear in mind that I don't own the computer so I cannot install anything, but the packages knitr and rmarkdown are installed.)

cgmil
  • 410
  • 2
  • 18
  • 1
    This doesn't directly answer your question, and you may already have thought of this, but to solve your problem you could pre-process the Rmd file to set `error = TRUE` (`readLines(f)`, do one or more `gsub`s, write it back out to either the original file or a temporary file) and then render the pre-processed Rmd file. I did a quick proof-of-concept and it takes about 4 lines of code. – Weihuang Wong Sep 20 '16 at 00:40
  • [This](https://gist.github.com/ClaudiusL/1057f075754b84784dbdb0fea33fbb24#file-q39583461-txt) is the answer I was about to post, unless I realized that this seems to work for any chunk option *except* `error`. If nobody can explain why this happens, it could be worth a bug report. – CL. Sep 20 '16 at 07:03
  • @CL. This did not work. I don't think setting chunk options works for `render`, I think. – cgmil Sep 20 '16 at 13:57
  • @cgmil Yes and no. In general, this works - check the "almost answer" in my previous comment. But `error` seems to be a special case where it does not work. – CL. Sep 20 '16 at 13:59

0 Answers0