From 5b7bad420fb803320e7504618e1a2f1ee694942f Mon Sep 17 00:00:00 2001 From: Barret Schloerke Date: Thu, 22 Oct 2026 16:05:53 -0400 Subject: [PATCH 1/2] feat(reactlog_widget): Add an htmlwidget for the reactlog (#160) reactlog_widget() inlines the reactlog page and shows it in an iframe, so it can be embedded in R Markdown or Quarto. Like reactlog_show(), log defaults to shiny::reactlog(). htmlwidgets is a suggested package, checked at run time with rlang::check_installed(). --- DESCRIPTION | 2 + NAMESPACE | 1 + NEWS.md | 2 + R/renderReactLog.R | 55 +++++++++++---------- R/widget.R | 93 ++++++++++++++++++++++++++++++++++++ inst/htmlwidgets/reactlog.js | 18 +++++++ man/reactlog_widget.Rd | 53 ++++++++++++++++++++ tests/testthat/test-render.R | 18 +++++++ 8 files changed, 217 insertions(+), 25 deletions(-) create mode 100644 R/widget.R create mode 100644 inst/htmlwidgets/reactlog.js create mode 100644 man/reactlog_widget.Rd diff --git a/DESCRIPTION b/DESCRIPTION index e0df144..13a11ed 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -23,10 +23,12 @@ Depends: R (>= 3.0.2) Imports: jsonlite (>= 0.9.16), + rlang, rmarkdown Suggests: fontawesome (>= 0.3.0), htmltools, + htmlwidgets, knitr, shiny (>= 1.5.0), testthat diff --git a/NAMESPACE b/NAMESPACE index c4f8636..321f44f 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -6,4 +6,5 @@ export(reactlog_module_server) export(reactlog_module_ui) export(reactlog_render) export(reactlog_show) +export(reactlog_widget) export(reactlog_write) diff --git a/NEWS.md b/NEWS.md index 8996ec4..f134359 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,7 @@ # reactlog (development) +- Added `reactlog_widget()`, which shows the reactlog as an htmlwidget so it can be embedded in R Markdown or Quarto documents. Like `reactlog_show()`, `log` defaults to `shiny::reactlog()`. Requires the htmlwidgets package (#160) + - `reactlog_show()` now uses `shiny::reactlog()` as its default `log`, so `reactlog_show()` works with no arguments after running an app (#91) - The graph legend now shows that a solid line is a dependency, a dashed line is an `isolate()`d dependency, and a dotted line is an inactive dependency (one that existed earlier but is not active at the current step) (#76) diff --git a/R/renderReactLog.R b/R/renderReactLog.R index 2502bf2..f19c043 100644 --- a/R/renderReactLog.R +++ b/R/renderReactLog.R @@ -11,31 +11,7 @@ inst_reactlog_assets <- function() { #' @param session_token token to be used to subset which session is displayed. #' Defaults to all sessions. reactlog_render <- function(log, session_token = NULL, time = TRUE) { - log <- reactlog_upgrade(log) - - template_file <- inst_reactlog_file("reactlog.html") - # does not need utf8 support - html <- paste(readLines(template_file, warn = FALSE), collapse = "\n") - - # get the json form of the lgo without writing to disk - json_blob <- as.character( - reactlog_write(log, file = NULL, session_token = session_token) - ) - - fixed_sub <- function(...) { - sub(..., fixed = TRUE) - } - - html <- fixed_sub( - "__DATA__", paste(json_blob, collapse = "\n"), - fixed_sub( - "__TIME__", paste0("\"", time, "\""), - fixed_sub( - "", "", - html - ) - ) - ) + html <- reactlog_page_html(log, session_token = session_token, time = time) # check if shiny is even available if (requireNamespace("shiny", quietly = TRUE)) { @@ -60,3 +36,32 @@ reactlog_render <- function(log, session_token = NULL, time = TRUE) { file } + +fixed_sub <- function(...) { + sub(..., fixed = TRUE) +} + +# The reactlog page as one string. Assets are still linked from `reactlogAsset/`. +reactlog_page_html <- function(log, session_token = NULL, time = TRUE) { + log <- reactlog_upgrade(log) + + template_file <- inst_reactlog_file("reactlog.html") + # does not need utf8 support + html <- paste(readLines(template_file, warn = FALSE), collapse = "\n") + + # get the json form of the lgo without writing to disk + json_blob <- as.character( + reactlog_write(log, file = NULL, session_token = session_token) + ) + + fixed_sub( + "__DATA__", paste(json_blob, collapse = "\n"), + fixed_sub( + "__TIME__", paste0("\"", time, "\""), + fixed_sub( + "", "", + html + ) + ) + ) +} diff --git a/R/widget.R b/R/widget.R new file mode 100644 index 0000000..f4bb5cd --- /dev/null +++ b/R/widget.R @@ -0,0 +1,93 @@ +#' Reactlog htmlwidget +#' +#' Displays the reactlog as an \pkg{htmlwidgets} widget, so it can be embedded +#' in R Markdown or Quarto documents, or shown in the RStudio viewer. +#' +#' The widget is a self-contained page shown in an `