You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In some cases, it is useful to temporarily ignore inline code errors (e.g., "We had `r N` participants" called before N was defined). One way to do that is to redefine knitr's evaluate.inline knitting hook. Here is one possible implementation
#' Evaluate Inline Code Without Stopping on Errors#' #' A custom knitr hook for inline code evaluation that captures errors and formats them gracefully.#'#' @note Based on https://stackoverflow.com/a/62041765/3042770#'#' @param code The inline code to evaluate.#' @param envir The environment in which to evaluate the code.#' @return The evaluated result or an error message for rendering.#' #' @export#' #' @examples#' knitr::opts_knit$set(evaluate.inline = blabr::no_stop_inline)no_stop_inline<-function(code, envir=knitr::knit_global()) {
result<- tryCatch(
withVisible(eval(xfun::parse_only(code), envir=envir)),
error=function(e) {
list(value= sprintf("`<<<` ERROR in: `%s`: %s `>>>`", code, e$message), is_error=TRUE)
}
)
if (!is.null(result$is_error) && isTRUE(result$is_error)) {
# Print the error message in the console
cat(result$value)
warning(result$value)
# Return the error message for rendering in the documentknitr::asis_output(result$value)
} else {
if (result$visible)
knitr::knit_print(result$value, inline=TRUE, options=knitr::opts_chunk$get())
}
}
Which can be used with knitr::opts_knit$set(evaluate.inline = blabr::no_stop_inline) in Rmd.
Alternatively (or in addition), we can provide a function that will set this hook so that the user can skip the whole knitr::opts_knit$set(evaluate.inline = xxx) part.
#' Set the Custom Inline Code Evaluation Hook#' #' This function sets the `no_stop_inline` hook as the evaluate.inline hook for knitr.#' #' @export#' #' @return Invisibly returns NULL after setting the hook.#' @examples#' set_no_stop_inline()set_no_stop_inline<-function() {
knitr::opts_knit$set(evaluate.inline=no_stop_inline)
invisible(NULL)
}
The text was updated successfully, but these errors were encountered:
In some cases, it is useful to temporarily ignore inline code errors (e.g., "We had `r N` participants" called before
N
was defined). One way to do that is to redefine knitr'sevaluate.inline
knitting hook. Here is one possible implementationWhich can be used with
knitr::opts_knit$set(evaluate.inline = blabr::no_stop_inline)
in Rmd.Alternatively (or in addition), we can provide a function that will set this hook so that the user can skip the whole
knitr::opts_knit$set(evaluate.inline = xxx)
part.The text was updated successfully, but these errors were encountered: