Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export(reactlog_disable)
export(reactlog_enable)
export(reactlog_module_server)
export(reactlog_module_ui)
export(reactlog_read)
export(reactlog_render)
export(reactlog_show)
export(reactlog_write)
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

- 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)

- New `reactlog_read()` reads a file written by `reactlog_write()` back into a log that `reactlog_show()` can display (#71)

- Migrated project license from GPL-3 to the MIT License.

- Added a `?` button to the reactlog header. Hover over it or click it to see every keyboard shortcut, with key names for your operating system. The navigation button tooltips now also use your operating system's key names. The vignette has the same list (#73)
Expand Down
21 changes: 21 additions & 0 deletions R/showReactLog.R
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,14 @@ reactlog_show <- function(log = shiny::reactlog(), time = TRUE, ...) {
#' json representation is return.
#' @param session_token Session token identifier to be used when subsetting the
#' complete reactlog
#' @seealso [reactlog_read()] to read the file back in.
#' @export
#' @examples
#' \dontrun{
#' reactlog_write(shiny::reactlog(), "reactlog.json")
#' log <- reactlog_read("reactlog.json")
#' reactlog_show(log)
#' }
reactlog_write <- function(log, file = stdout(), session_token = NULL) {
if (!is.null(session_token)) {
log <- Filter(
Expand All @@ -103,6 +110,20 @@ reactlog_write <- function(log, file = stdout(), session_token = NULL) {
}
}

#' Read reactlog
#'
#' Read a reactlog written by [reactlog_write()], so it can be passed to
#' [reactlog_show()] or [reactlog_render()].
#'
#' @param file JSON file written by [reactlog_write()].
#' @return The reactlog: a list of log entries with a `version` attribute.
#' @export
reactlog_read <- function(file) {
log <- jsonlite::read_json(file, simplifyVector = FALSE)
# ponytail: the JSON has no version field; "1" is the only version so far
attr(log, "version") <- "1"
log
}

write_utf8 <- function(x, file, ...) {
writeLines(enc2utf8(x), file, ..., useBytes = TRUE)
Expand Down
18 changes: 18 additions & 0 deletions man/reactlog_read.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions man/reactlog_write.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions tests/testthat/test-render.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,19 @@ test_that("reactlog_show() defaults to shiny::reactlog()", {
reactlog_show()
expect_true(called)
})

test_that("reactlog_read() reads back what reactlog_write() wrote", {
log <- list(list(
action = "define", reactId = "r1", label = "a\\b\"c\\",
type = "observable", session = NULL, time = 1
))
attr(log, "version") <- "1"
file <- tempfile(fileext = ".json")
on.exit(unlink(file))
reactlog_write(log, file)

log2 <- reactlog_read(file)
expect_identical(attr(log2, "version"), "1")
expect_identical(reactlog_write(log2, NULL), reactlog_write(log, NULL))
expect_no_error(reactlog_render(log2))
})
Loading