-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from debruine/main
quarto stuff
- Loading branch information
Showing
60 changed files
with
3,850 additions
and
3,143 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,4 @@ | |
^cran-comments\.md$ | ||
^README\.html$ | ||
^Makefile$ | ||
^webexercises\.Rproj$ |
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 |
---|---|---|
@@ -1,13 +1,13 @@ | ||
Package: webexercises | ||
Version: 1.0.0 | ||
Date: 2021-09-06 | ||
Version: 1.1.0 | ||
Date: 2023-04-18 | ||
Title: Create Interactive Web Exercises in 'R Markdown' (Formerly 'webex') | ||
Description: Functions for easily creating interactive web pages using | ||
'R Markdown' that students can use in self-guided learning. | ||
Authors@R: c( | ||
person("Dale","Barr", | ||
email = "[email protected]", | ||
role = c("aut", "cre")), | ||
role = c("aut", "cre")), | ||
person("Lisa", "DeBruine", | ||
email = "[email protected]", | ||
role = "aut")) | ||
|
@@ -18,11 +18,14 @@ Imports: | |
knitr, | ||
yaml, | ||
utils, | ||
grDevices, | ||
rstudioapi, | ||
rmarkdown (>= 2.2) | ||
License: CC BY-SA 4.0 | ||
RoxygenNote: 7.1.1 | ||
RoxygenNote: 7.2.1 | ||
Suggests: | ||
testthat, | ||
bookdown, | ||
quarto, | ||
xfun | ||
Encoding: UTF-8 |
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
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,95 @@ | ||
#' Add webexercises helper files to quarto | ||
#' | ||
#' Adds the necessary helper files to an existing quarto project and | ||
#' edits the _quarto.yml file accordingly. A demo file for webexercises | ||
#' will be added and optionally rendered. | ||
#' | ||
#' @param quarto_dir The base directory for your quarto project | ||
#' @param include_dir The directory where you want to put the css and | ||
#' js files (defaults to "include") | ||
#' @param output_format The format you want to add | ||
#' webexercises to (only html for now) | ||
#' | ||
#' @return No return value, called for side effects. | ||
#' @export | ||
#' | ||
add_to_quarto <- function(quarto_dir = ".", | ||
include_dir = "include", | ||
output_format = c("html")) { | ||
# check inputs | ||
if (quarto_dir == "") quarto_dir <- "." | ||
if (include_dir == "") include_dir <- "." | ||
output_format <- match.arg(output_format) | ||
|
||
# get helper files | ||
css <- system.file("reports/default/webex.css", package = "webexercises") | ||
js <- system.file("reports/default/webex.js", package = "webexercises") | ||
demo <- system.file("reports/default/webexercises.qmd", package = "webexercises") | ||
|
||
# make sure include and script directories exist | ||
incdir <- file.path(quarto_dir, include_dir) | ||
dir.create(path = incdir, showWarnings = FALSE, recursive = TRUE) | ||
|
||
# add or update helper files | ||
file.copy(css, incdir, overwrite = TRUE) | ||
file.copy(js, incdir, overwrite = TRUE) | ||
file.copy(demo, quarto_dir, overwrite = TRUE) | ||
message("webex.css, webex.js, and webexercises.qmd updated") | ||
|
||
# update or create _quarto.yml | ||
css_path <- file.path(include_dir, "webex.css") | ||
js_path <- file.path(include_dir, "webex.js") | ||
quarto_defaults <- list( | ||
"html" = list( | ||
"css" = css_path, | ||
"include-after-body" = js_path | ||
) | ||
) | ||
|
||
quarto_file <- file.path(quarto_dir, "_quarto.yml") | ||
if (!file.exists(quarto_file)) { | ||
# add new format with reasonable defaults | ||
yml <- list() | ||
yml[[output_format]] <- quarto_defaults[[output_format]] | ||
} else { | ||
# keep default yml | ||
yml <- yaml::read_yaml(quarto_file) | ||
if ( !is.list(yml$format) || !"format" %in% names(yml)) { | ||
yml$format <- list() | ||
} | ||
|
||
if (!output_format %in% names(yml$format)) { | ||
# append output_format | ||
yml$format[[output_format]] <- quarto_defaults[[output_format]] | ||
} | ||
} | ||
|
||
# get previous values | ||
old_css <- yml$format[[output_format]]$css | ||
old_js <- yml$format[[output_format]]$`include-after-body` | ||
|
||
# merge with new values | ||
yml$format[[output_format]]$css <- union(old_css, css_path) | ||
yml$format[[output_format]]$`include-after-body` <- union(old_js, js_path) | ||
|
||
# write to _quarto.yml | ||
# custom handler to stop converting boolean values to yes and no | ||
yaml::write_yaml(yml, quarto_file, handlers = list( | ||
logical = function(x) { | ||
result <- ifelse(x, "true", "false") | ||
class(result) <- "verbatim" | ||
return(result) | ||
} | ||
)) | ||
message(quarto_file, " updated") | ||
|
||
# update .Rprofile | ||
rprofile <- file.path(quarto_dir, ".Rprofile") | ||
|
||
load_txt <- "# load webexercises before each chapter | ||
# needs to check namespace to not bork github actions | ||
if (requireNamespace('webexercises', quietly = TRUE)) library(webexercises)" | ||
write(load_txt, rprofile, append = TRUE) | ||
|
||
invisible(NULL) | ||
} |
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,28 @@ | ||
#' Create a quarto document with webexercise | ||
#' | ||
#' Creates a new directory with the file name and copies in a demo qmd file and the necessary helper files. | ||
#' | ||
#' @param name Name of the new document | ||
#' @param open Whether to open the document in RStudio | ||
#' | ||
#' @return The file path to the document | ||
#' @export | ||
#' | ||
create_quarto_doc <- function(name = "Untitled", open = interactive()) { | ||
if (!file.exists(name)) dir.create(name, FALSE, TRUE) | ||
path <- normalizePath(name) | ||
filepath <- file.path(path, paste0(basename(name), ".qmd")) | ||
|
||
# get helper files | ||
css <- system.file("reports/default/webex.css", package = "webexercises") | ||
js <- system.file("reports/default/webex.js", package = "webexercises") | ||
index <- system.file("reports/default/index.qmd", package = "webexercises") | ||
|
||
file.copy(css, path) | ||
file.copy(js, path) | ||
file.copy(index, filepath) | ||
|
||
if (open) rstudioapi::documentOpen(filepath) | ||
|
||
invisible(filepath) | ||
} |
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
Oops, something went wrong.