Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colin/sanity check #445

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
This function will perform a sanity check on setup and solution c…
…hunks
ColinFay committed Sep 30, 2020
commit aa3dad4d175eefc85ec5ae9e94913d3d049aeee4
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -39,7 +39,8 @@ Imports:
renv (>= 0.8.0),
curl,
promises,
digest
digest,
cli
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to Suggests and check for it before running the main function

Remotes:
rstudio/htmltools,
rstudio/shinytest
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -17,6 +17,7 @@ S3method(question_ui_initialize,default)
S3method(question_ui_try_again,default)
export(answer)
export(available_tutorials)
export(check_exercise)
export(correct)
export(disable_all_tags)
export(duplicate_env)
107 changes: 107 additions & 0 deletions R/check_exercise.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#' Check the code from an Exercise
#'
#' This function will take all the chunks with a label that matches `setup` or
#' `-solution`, put them in a separate script and try to run them all.
#' This allows teachers to check that their setup and solution chunks
#' contain valid code.
#'
#' @param path Path to the Markdown file containing the RMarkdown.
#' @param verbose Should the test output information on the console?
#'
#' @return TRUE or FALSE invisibly.
#' @export
#'
#' @examples
#' if (interactive()){
#' check_exercise("sandbox/sandbox.Rmd")
#' }
check_exercise <- function(
path,
verbose = TRUE
){
# Create a file that will receive the chunks
tempr <- tempfile(fileext = ".R")
write_there <- function(x){
write(
x,
tempr,
append = TRUE
)
}

# Getting the old chunk hook, and reset it on exit
hook_old <- knitr::knit_hooks$get("chunk")
on.exit(
knitr::knit_hooks$set(chunk = hook_old)
)

# Setting a hook on every chunk
knitr::knit_hooks$set(chunk = function(x, options) {
# It the chunk is a setup or solution chunk, we add it to
# the temp .R script
if(grepl("(\\-*setup|\\-solution)$", options$label)){
write_there(
sprintf(
"# %s ----",
options$label
)
)
if (verbose){
write_there(
sprintf(
'cli::cat_rule("Checking chunk %s")',
options$label
)
)
}
write_there(
options$code
)
if (verbose){
write_there(
'cli::cat_bullet("Ok", col = "green", bullet = "tick");cli::cat_line(" ")'
)
}
}
hook_old(x, options)
})

# Trick knitr into thinking we are in a shiny_prerender context
hook_runtime<- knitr::knit_hooks$get("rmarkdown.runtime")
on.exit(
knitr::knit_hooks$set("rmarkdown.runtime" = hook_runtime)
)
knitr::opts_knit$set(rmarkdown.runtime = "shiny_prerendered")

# We don't need the knitted output so we unlink it immediatly
unlink(knitr::knit(path, quiet = TRUE))

# Trying to source the temp R script
tc <- try( source(tempr) )
unlink(tempr)

cli::cat_line(" ")
cli::cat_rule("Check finished")
cli::cat_line(" ")

if (
inherits(tc, "try-error")
){
cli::cat_bullet(
"Running setup and/or solution chunks failed",
col = "red",
bullet = "cross"
)
return(invisible(FALSE))
}

cli::cat_bullet(
"Successfully run setup and/or solution chunks",
col = "green",
bullet = "tick"
)

cli::cat_line(" ")

return(invisible(TRUE))
}