-
Notifications
You must be signed in to change notification settings - Fork 239
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
ColinFay
wants to merge
6
commits into
rstudio:main
Choose a base branch
from
ThinkR-open:colin/sanity-check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+560
−1
Draft
Colin/sanity check #445
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
aa3dad4
This function will perform a sanity check on `setup` and `solution` c…
ColinFay 3780415
added doc
ColinFay d7ad3ea
Exercise checker based on evaluate_code
ColinFay 93a44c7
added example + kept the TODO in code_check
ColinFay 7b09892
Correct return for check_exercise
ColinFay d86bc7f
redoc
ColinFay File filter
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
commit aa3dad4d175eefc85ec5ae9e94913d3d049aeee4
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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