Skip to content

Commit 213dda2

Browse files
authored
feat(R): Add reactlog_read() to read back reactlog_write() output (#159)
Fixes #71
1 parent 6fa4a0f commit 213dda2

6 files changed

Lines changed: 68 additions & 0 deletions

File tree

‎NAMESPACE‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export(reactlog_disable)
44
export(reactlog_enable)
55
export(reactlog_module_server)
66
export(reactlog_module_ui)
7+
export(reactlog_read)
78
export(reactlog_render)
89
export(reactlog_show)
910
export(reactlog_write)

‎NEWS.md‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

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

7+
- New `reactlog_read()` reads a file written by `reactlog_write()` back into a log that `reactlog_show()` can display (#71)
8+
79
- Migrated project license from GPL-3 to the MIT License.
810

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

‎R/showReactLog.R‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,14 @@ reactlog_show <- function(log = shiny::reactlog(), time = TRUE, ...) {
7676
#' json representation is return.
7777
#' @param session_token Session token identifier to be used when subsetting the
7878
#' complete reactlog
79+
#' @seealso [reactlog_read()] to read the file back in.
7980
#' @export
81+
#' @examples
82+
#' \dontrun{
83+
#' reactlog_write(shiny::reactlog(), "reactlog.json")
84+
#' log <- reactlog_read("reactlog.json")
85+
#' reactlog_show(log)
86+
#' }
8087
reactlog_write <- function(log, file = stdout(), session_token = NULL) {
8188
if (!is.null(session_token)) {
8289
log <- Filter(
@@ -103,6 +110,20 @@ reactlog_write <- function(log, file = stdout(), session_token = NULL) {
103110
}
104111
}
105112

113+
#' Read reactlog
114+
#'
115+
#' Read a reactlog written by [reactlog_write()], so it can be passed to
116+
#' [reactlog_show()] or [reactlog_render()].
117+
#'
118+
#' @param file JSON file written by [reactlog_write()].
119+
#' @return The reactlog: a list of log entries with a `version` attribute.
120+
#' @export
121+
reactlog_read <- function(file) {
122+
log <- jsonlite::read_json(file, simplifyVector = FALSE)
123+
# ponytail: the JSON has no version field; "1" is the only version so far
124+
attr(log, "version") <- "1"
125+
log
126+
}
106127

107128
write_utf8 <- function(x, file, ...) {
108129
writeLines(enc2utf8(x), file, ..., useBytes = TRUE)

‎man/reactlog_read.Rd‎

Lines changed: 18 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎man/reactlog_write.Rd‎

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎tests/testthat/test-render.R‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,19 @@ test_that("reactlog_show() defaults to shiny::reactlog()", {
3232
reactlog_show()
3333
expect_true(called)
3434
})
35+
36+
test_that("reactlog_read() reads back what reactlog_write() wrote", {
37+
log <- list(list(
38+
action = "define", reactId = "r1", label = "a\\b\"c\\",
39+
type = "observable", session = NULL, time = 1
40+
))
41+
attr(log, "version") <- "1"
42+
file <- tempfile(fileext = ".json")
43+
on.exit(unlink(file))
44+
reactlog_write(log, file)
45+
46+
log2 <- reactlog_read(file)
47+
expect_identical(attr(log2, "version"), "1")
48+
expect_identical(reactlog_write(log2, NULL), reactlog_write(log, NULL))
49+
expect_no_error(reactlog_render(log2))
50+
})

0 commit comments

Comments
 (0)