Skip to content

Commit

Permalink
Refactor knit hooks into prep/install step
Browse files Browse the repository at this point in the history
`tutorial_knitr_options()` now prepares a list of knitr options with the knitr options and hooks that are used during the tutorial pre-render. This list is in the same format as the knitr options expected by `rmarkdown::output_format()` so we can set the knitr options directly in the `tutorial` output format.

This ensures that the hooks are set for each `rmarkdown::render()` call. Previously, by only relying on `.onAttach()`, these hooks might be reset at the end of the first `render()` and wouldn't be re-installed for subsequent renders, resulting in the behavior seen in #598. I haven't removed the `.onAttach()` mechanism because it is still useful for catching learnr component usage outside of the `learnr::tutorial()` format.
  • Loading branch information
gadenbuie committed Oct 15, 2021
1 parent b000739 commit fecefa4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
31 changes: 21 additions & 10 deletions R/knitr-hooks.R
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@ detect_installed_knitr_hooks <- function() {
is.function(tutorial_knit_hook)
}

install_knitr_hooks <- function() {
# set global tutorial option which we can use as a basis for hooks
# (this is so we don't collide with hooks set by the user or
# by other packages or Rmd output formats)
knitr::opts_chunk$set(tutorial = TRUE)

tutorial_knitr_options <- function() {
# helper to check for runtime: shiny_prerendered being active
is_shiny_prerendered_active <- function() {
identical(knitr::opts_knit$get("rmarkdown.runtime"),"shiny_prerendered")
Expand Down Expand Up @@ -186,7 +181,7 @@ install_knitr_hooks <- function() {
}

# hook to turn off evaluation/highlighting for exercise related chunks
knitr::opts_hooks$set(tutorial = function(options) {
tutorial_opts_hook <- function(options) {

# check for chunk type
exercise_chunk <- is_exercise_chunk(options)
Expand Down Expand Up @@ -285,10 +280,10 @@ install_knitr_hooks <- function() {

# return modified options
options
})
}

# hook to amend output for exercise related chunks
knitr::knit_hooks$set(tutorial = function(before, options, envir) {
tutorial_knit_hook <- function(before, options, envir) {

# helper to produce an exercise wrapper div w/ the specified class
exercise_wrapper_div <- function(suffix = NULL, extra_html = NULL) {
Expand Down Expand Up @@ -465,7 +460,23 @@ install_knitr_hooks <- function() {
}

}
})
}

list(
# set global tutorial option which we can use as a basis for hooks
# (this is so we don't collide with hooks set by the user or
# by other packages or Rmd output formats)
opts_chunk = list(tutorial = TRUE),
opts_hooks = list(tutorial = tutorial_opts_hook),
knit_hooks = list(tutorial = tutorial_knit_hook)
)
}

install_knitr_hooks <- function() {
knit_opts <- tutorial_knitr_options()
knitr::opts_chunk$set(tutorial = knit_opts$opts_chunk$tutorial)
knitr::opts_hooks$set(tutorial = knit_opts$opts_hooks$tutorial)
knitr::knit_hooks$set(tutorial = knit_opts$knit_hooks$tutorial)
}

remove_knitr_hooks <- function() {
Expand Down
3 changes: 3 additions & 0 deletions R/tutorial-format.R
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,9 @@ tutorial <- function(fig_width = 6.5,
args = args,
ext = ".html")

tutorial_opts <- tutorial_knitr_options()
knitr_options <- utils::modifyList(knitr_options, tutorial_opts)

# set 1000 as the default maximum number of rows in paged tables
knitr_options$opts_chunk$max.print <- 1000

Expand Down

0 comments on commit fecefa4

Please sign in to comment.