library(knitr)
library(jsonlite)

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  eval = FALSE
)

clean <- function(x) {
  lines <- strsplit(x, "\\n")[[1]]
  clean1 <- lines[!grepl("^#", lines)]
  clean2 <- lines[!grepl("^success_msg", lines)]
  return(paste0(clean2, collapse = "\n"))
}

knit_hooks$set(autogen = function(before, options, envir) {
  if (before) {
    url <- sprintf("https://www.datacamp.com/api/courses/%i/chapters/%i/exercises.json", options$crs_id, options$chap_id)
    ex <- fromJSON(url, simplifyDataFrame = FALSE)[[options$ex_num]]
    content <- sprintf("# solution\n%s\n\n# sct\n%s\n", clean(ex$solution), clean(ex$sct))
    knitr::knit_hooks$get("source")(content, options)
  }
})

For more context on why this particular SCT is a good idea, you can consult the Guides. None of the examples below specify any custom messages; you can consult the function documentation to figure out how you can override the automatically generated messages.

Check object



Check function call


Check function result


Check object created through function call


Check output



Check if else


Check for loop


Check function definition


Check library or require call


Check through string matching


Check ggplot


NOTE: These exercises are pulled from a DataCamp course. You can find the source here.



Data-Camp/testwhat documentation built on June 24, 2022, 9:59 p.m.