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.
library
or require
callNOTE: These exercises are pulled from a DataCamp course. You can find the source here.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.