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: | Dale Barr [aut], Lisa DeBruine [aut, cre] |
Maintainer: | Lisa DeBruine <[email protected]> |
License: | CC BY-SA 4.0 |
Version: | 1.1.0.9000 |
Built: | 2024-11-12 05:27:06 UTC |
Source: | https://github.com/psyteachr/webexercises |
Adds the necessary helper files to an existing bookdown project and edits the _output.yml and _bookdown.yml files accordingly. If the directory does not have a bookdown project in it, a template project will be set up.
add_to_bookdown( bookdown_dir = ".", include_dir = "include", script_dir = "R", output_format = c("bs4_book", "gitbook", "html_book", "tufte_html_book"), render = FALSE )
add_to_bookdown( bookdown_dir = ".", include_dir = "include", script_dir = "R", output_format = c("bs4_book", "gitbook", "html_book", "tufte_html_book"), render = FALSE )
bookdown_dir |
The base directory for your bookdown project |
include_dir |
The directory where you want to put the css and js files (defaults to "include") |
script_dir |
The directory where you want to put the .R script (defaults to "R") |
output_format |
The bookdown format you want to add webexercises to (defaults to "bs4_book") This is typically your default HTML format in the _output.yml file. |
render |
Whether to render the book after updating (defaults to FALSE). |
No return value, called for side effects.
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.
add_to_quarto( quarto_dir = ".", include_dir = "include", output_format = c("html") )
add_to_quarto( quarto_dir = ".", include_dir = "include", output_format = c("html") )
quarto_dir |
The base directory for your quarto project |
include_dir |
The directory where you want to put the css and js files (defaults to "include") |
output_format |
The format you want to add webexercises to (only html for now) |
No return value, called for side effects.
Creates a new directory with the file name and copies in a demo qmd file and the necessary helper files.
create_quarto_doc(name = "Untitled", open = interactive())
create_quarto_doc(name = "Untitled", open = interactive())
name |
Name of the new document |
open |
Whether to open the document in RStudio |
The file path to the document
Escape a string for regex
escape_regex(string)
escape_regex(string)
string |
A string to escape. |
A string with escaped characters.
escape_regex("library(tidyverse)")
escape_regex("library(tidyverse)")
Create a fill-in-the-blank question
fitb( answer, width = calculated_width, num = NULL, ignore_case = FALSE, tol = NULL, ignore_ws = TRUE, regex = FALSE )
fitb( answer, width = calculated_width, num = NULL, ignore_case = FALSE, tol = NULL, ignore_ws = TRUE, regex = FALSE )
answer |
The correct answer (can be a vector if there is more than one correct answer). |
width |
Width of the input box in characters. Defaults to the length of the longest answer. |
num |
Whether the input is numeric, in which case allow for leading zeroes to be omitted. Determined from the answer data type if not specified. |
ignore_case |
Whether to ignore case (capitalization). |
tol |
The tolerance within which numeric answers will be
accepted; i.e. if |
ignore_ws |
Whether to ignore whitespace. |
regex |
Whether to use regex to match answers (concatenates all answers with '|' before matching). |
Writes html code that creates an input box widget. Call this function inline in an RMarkdown document. See the Web Exercises RMarkdown template for examples of its use in RMarkdown.
A character string with HTML code to generate an input box.
# What is 2 + 2? fitb(4, num = TRUE) # What was the name of the Beatles drummer? fitb(c("Ringo", "Ringo Starr"), ignore_case = TRUE) # What is pi to three decimal places? fitb(pi, num = TRUE, tol = .001)
# What is 2 + 2? fitb(4, num = TRUE) # What was the name of the Beatles drummer? fitb(c("Ringo", "Ringo Starr"), ignore_case = TRUE) # What is pi to three decimal places? fitb(pi, num = TRUE, tol = .001)
Create button revealing hidden content
hide(button_text = "Solution")
hide(button_text = "Solution")
button_text |
Text to appear on the button that reveals the hidden content. |
Writes HTML to create a content that is revealed by a
button press. Call this function inline in an RMarkdown
document. Any content appearing after this call up to an inline
call to unhide()
will only be revealed when the user
clicks the button. See the Web Exercises RMarkdown Template for
examples.
A character string containing HTML code to create a button that reveals hidden content.
unhide
# default behavior is to generate a button that says "Solution" hide() # or the button can display custom text hide("Click here for a hint")
# default behavior is to generate a button that says "Solution" hide() # or the button can display custom text hide("Click here for a hint")
Longer MCQs with Radio Buttons
longmcq(opts)
longmcq(opts)
opts |
Vector of alternatives. The correct answer is the element(s) of this vector named 'answer'. |
Writes html code that creates a radio button widget, with a single correct answer. This is more suitable for longer answers. Call this function inline in an RMarkdown document. See the Web Exercises RMarkdown template for further examples.
A character string containing HTML code to create a set of radio buttons.
# What is a p-value? opts <- c( "the probability that the null hypothesis is true", answer = paste("the probability of the observed, or more extreme, data", "under the assumption that the null-hypothesis is true"), "the probability of making an error in your conclusion" ) longmcq(opts)
# What is a p-value? opts <- c( "the probability that the null hypothesis is true", answer = paste("the probability of the observed, or more extreme, data", "under the assumption that the null-hypothesis is true"), "the probability of making an error in your conclusion" ) longmcq(opts)
Create a multiple-choice question
mcq(opts)
mcq(opts)
opts |
Vector of alternatives. The correct answer is the element(s) of this vector named 'answer'. |
Writes html code that creates an option box widget, with one or more correct answers. Call this function inline in an RMarkdown document. See the Web Exercises RMarkdown template for further examples.
A character string with HTML code to generate a pull-down menu.
# How many planets orbit closer to the sun than the Earth? mcq(c(1, answer = 2, 3)) # Which actor played Luke Skywalker in the movie Star Wars? mcq(c("Alec Guinness", answer = "Mark Hamill", "Harrison Ford"))
# How many planets orbit closer to the sun than the Earth? mcq(c(1, answer = 2, 3)) # Which actor played Luke Skywalker in the movie Star Wars? mcq(c("Alec Guinness", answer = "Mark Hamill", "Harrison Ford"))
Round up from .5
round2(x, digits = 0)
round2(x, digits = 0)
x |
A vector of numeric values. |
digits |
Integer indicating the number of decimal places ('round') or significant digits ('signif') to be used. |
Implements rounding using the "round up from .5" rule,
which is more conventional than the "round to even" rule
implemented by R's built-in round
function. This
implementation was taken from
https://stackoverflow.com/a/12688836.
A vector of rounded numeric values.
round2(c(2, 2.5)) # compare to: round(c(2, 2.5))
round2(c(2, 2.5)) # compare to: round(c(2, 2.5))
Strip leading zero from numeric string
strip_lzero(x)
strip_lzero(x)
x |
A numeric string (or number that can be converted to a string). |
A string with leading zero removed.
strip_lzero("0.05")
strip_lzero("0.05")
Change webexercises widget style
style_widgets( incorrect = "#983E82", correct = "#59935B", highlight = "#467AAC" )
style_widgets( incorrect = "#983E82", correct = "#59935B", highlight = "#467AAC" )
incorrect |
The colour of the widgets when the answer is incorrect (defaults to pink #983E82). |
correct |
The colour of the widgets when the correct answer not filled in (defaults to green #59935B). |
highlight |
The colour of the borders around hidden blocks and checked sections (defaults to blue #467AAC). |
Call this function in an RMarkdown document to change the feedback colours using R colour names (see 'colours()') or any valid CSS colour specification (e.g., red, rgb(255,0,0), hsl(0, 100
If you want more control over the widget styles, please edit the webex.css file directly.
A character string containing HTML code to change the CSS style values for widgets.
style_widgets("goldenrod", "purple")
style_widgets("goldenrod", "purple")
Create a true-or-false question
torf(answer)
torf(answer)
answer |
Logical value TRUE or FALSE, corresponding to the correct answer. |
Writes html code that creates an option box widget with TRUE or FALSE as alternatives. Call this function inline in an RMarkdown document. See the Web Exercises RMarkdown template for further examples.
A character string with HTML code to generate a pull-down menu with elements TRUE and FALSE.
# True or False? 2 + 2 = 4 torf(TRUE) # True or False? The month of April has 31 days. torf(FALSE)
# True or False? 2 + 2 = 4 torf(TRUE) # True or False? The month of April has 31 days. torf(FALSE)
End hidden HTML content
unhide()
unhide()
Call this function inline in an RMarkdown document to mark the end of hidden content (see the Web Exercises RMarkdown Template for examples).
A character string containing HTML code marking the end of hiddent content.
hide
# just produce the closing </div> unhide()
# just produce the closing </div> unhide()
This function wraps rmarkdown::html_document
to configure
compilation to embed the default webexercises CSS and JavaScript files in
the resulting HTML.
webexercises_default(...)
webexercises_default(...)
... |
Additional function arguments to pass to
|
Call this function as the output_format
argument
for the render
function when compiling
HTML documents from RMarkdown source.
R Markdown output format to pass to 'render'.
# copy the webexercises 'R Markdown' template to a temporary file ## Not run: my_rmd <- tempfile(fileext = ".Rmd") rmarkdown::draft(my_rmd, "webexercises", "webexercises") # compile it rmarkdown::render(my_rmd, webexercises::webexercises_default()) # view the result browseURL(sub("\\.Rmd$", ".html", my_rmd)) ## End(Not run)
# copy the webexercises 'R Markdown' template to a temporary file ## Not run: my_rmd <- tempfile(fileext = ".Rmd") rmarkdown::draft(my_rmd, "webexercises", "webexercises") # compile it rmarkdown::render(my_rmd, webexercises::webexercises_default()) # view the result browseURL(sub("\\.Rmd$", ".html", my_rmd)) ## End(Not run)