This R6
class is designed to store and manage reports,
facilitating the creation, manipulation, and serialization of report-related data.
It supports both ReportCard
and teal_card
objects, allowing flexibility
in the types of reports that can be stored and managed.
Note
if Report has an id when converting to JSON then It will be compared to the currently available one.
if Report has an id when converting to JSON then It will be compared to the currently available one.
Methods
Method reorder_cards()
Reorders teal_card
objects in Reporter
.
Reorders teal_card
objects in Reporter
.
Method replace_card()
Sets ReportCard
or teal_card
content.
Method get_cards()
Retrieves all teal_card
objects contained in Reporter
.
Returns
A (list
) of teal_card
objects.
Method get_blocks()
Compiles and returns all content blocks from the teal_card
objects in the Reporter
.
Arguments
sep
An optional separator to insert between each content block. Default is a
\n\\newpage\n
markdown.
Returns
list()
of teal_card
Method from_reporter()
Reinitializes a Reporter
instance by copying the report cards and metadata from another Reporter
.
Examples
reporter <- Reporter$new()
reporter$from_reporter(reporter)
Method to_list()
Convert a Reporter
to a list and transfer any associated files to specified directory.
Returns
named list
representing the Reporter
instance, including version information,
metadata, and report cards.
Examples
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "testdir")
dir.create(tmp_dir)
reporter$to_list(tmp_dir)
Method write_figures()
Extracts and saves all figure elements from the teal_card
objects in the
Reporter
to a specified directory.
Method from_list()
Reinitializes a Reporter
from a list representation and associated files in a specified directory.
Arguments
rlist
(
named list
) representing aReporter
instance.output_dir
(
character(1)
) a path to the directory from which files will be copied.
Examples
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "testdir")
unlink(tmp_dir, recursive = TRUE)
dir.create(tmp_dir)
reporter$from_list(reporter$to_list(tmp_dir), tmp_dir)
Method to_jsondir()
Serializes the Reporter
to a JSON
file and copies any associated files to a specified directory.
Arguments
output_dir
(
character(1)
) a path to the directory where files will be copied,JSON
and statics.
Examples
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
reporter$to_jsondir(tmp_dir)
Method from_jsondir()
Reinitializes a Reporter
from a JSON
file and files in a specified directory.
Examples
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
unlink(list.files(tmp_dir, recursive = TRUE))
reporter$to_jsondir(tmp_dir)
reporter$from_jsondir(tmp_dir)
Method set_id()
Set the Reporter
id
Optionally add id to a Reporter
which will be compared when it is rebuilt from a list.
The id is added to the downloaded file name.
Method set_template()
Set template function for teal_card
Set a function that is called on every report content (of class teal_card
) added through $append_cards
Examples
reporter <- teal.reporter::Reporter$new()
template_fun <- function(document) {
disclaimer <- teal.reporter::teal_card("Here comes disclaimer text")
c(disclaimer, document)
}
reporter$set_template(template_fun)
doc1 <- teal.reporter::teal_card("## Header 2 text", "Regular text")
metadata(doc1, "title") <- "Welcome card"
reporter$append_cards(doc1)
reporter$get_cards()
Examples
library(ggplot2)
card1 <- teal_card("## Header 2 text", "A paragraph of default text")
card1 <- c(card1, ggplot(iris, aes(x = Petal.Length)) + geom_histogram())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
metadata(card1, "title") <- "Card1"
card2 <- teal_card("Document introduction")
metadata(card2, "title") <- "Card2"
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
library(ggplot2)
library(rtables)
#> Loading required package: formatters
#>
#> Attaching package: ‘formatters’
#> The following object is masked from ‘package:base’:
#>
#> %||%
#> Loading required package: magrittr
#>
#> Attaching package: ‘magrittr’
#> The following objects are masked from ‘package:testthat’:
#>
#> equals, is_less_than, not
#>
#> Attaching package: ‘rtables’
#> The following object is masked from ‘package:utils’:
#>
#> str
card1 <- teal_card("## Header 2 text", "A paragraph of default text")
card1 <- c(card1, ggplot(iris, aes(x = Petal.Length)) + geom_histogram())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
metadata(card1, "title") <- "Card1"
lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- build_table(lyt, airquality)
#> Split var [Day] was not character or factor. Converting to factor
card2 <- teal_card(
"## Header 2 text",
"A paragraph of default text",
table_res2
)
metadata(card2, "title") <- "Card2"
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
names(reporter$get_cards())
#> [1] "card_d33e76de" "card_83faca31"
reporter$reorder_cards(c("Card2", "Card1"))
names(reporter$get_cards())
#> [1] "card_d33e76de" "card_83faca31"
library(ggplot2)
library(rtables)
card1 <- teal_card("## Header 2 text", "A paragraph of default text")
card1 <- c(card1, ggplot(iris, aes(x = Petal.Length)) + geom_histogram())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
metadata(card1, "title") <- "Card1"
reporter <- Reporter$new()
reporter$append_cards(list(card1))
lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- build_table(lyt, airquality)
#> Split var [Day] was not character or factor. Converting to factor
card2 <- teal_card(
"## Header 2 text",
"A paragraph of default text",
table_res2
)
metadata(card2, "title") <- "Card2"
metadata(reporter$get_cards()[[1]], "title")
#> [1] "Card1"
reporter$replace_card(card2, names(reporter$get_cards())[[1]])
metadata(reporter$get_cards()[[1]], "title")
#> [1] "Card2"
library(ggplot2)
library(rtables)
card1 <- teal_card("## Header 2 text", "A paragraph of default text")
card1 <- c(card1, ggplot(iris, aes(x = Petal.Length)) + geom_histogram())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- build_table(lyt, airquality)
#> Split var [Day] was not character or factor. Converting to factor
card2 <- teal_card(
"## Header 2 text",
"A paragraph of default text",
table_res2
)
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_cards()
#> $card_7f0c9738
#> $`76b0e858`
#> [1] "## Header 2 text"
#>
#> $`57283d11`
#> [1] "A paragraph of default text"
#>
#> $`8a60b036`
#>
#> attr(,"class")
#> [1] "teal_card"
#> attr(,"metadata")
#> list()
#>
#> $card_79b2252b
#> $`2e0a13ea`
#> [1] "## Header 2 text"
#>
#> $`838635aa`
#> [1] "A paragraph of default text"
#>
#> $`53179958`
#> all obs
#> —————————————————————————
#> 1
#> mean 77.75
#> 2
#> mean 43
#> 3
#> mean 33.25
#> 4
#> mean 62.3333333333333
#> 5
#> mean 48.6666666666667
#> 6
#> mean 41.5
#> 7
#> mean 54.2
#> 8
#> mean 57
#> 9
#> mean 61.4
#> 10
#> mean 49.3333333333333
#> 11
#> mean 25.5
#> 12
#> mean 22.75
#> 13
#> mean 23.4
#> 14
#> mean 29.3333333333333
#> 15
#> mean 12.6666666666667
#> 16
#> mean 30.2
#> 17
#> mean 36.6
#> 18
#> mean 24.6
#> 19
#> mean 35.2
#> 20
#> mean 29.4
#> 21
#> mean 12.75
#> 22
#> mean 14.3333333333333
#> 23
#> mean 20
#> 24
#> mean 41
#> 25
#> mean 96.6666666666667
#> 26
#> mean 41
#> 27
#> mean 52
#> 28
#> mean 48.75
#> 29
#> mean 57.75
#> 30
#> mean 70.75
#> 31
#> mean 60.3333333333333
#>
#> attr(,"class")
#> [1] "teal_card"
#>
library(ggplot2)
library(rtables)
card1 <- teal_card("## Header 2 text", "A paragraph of default text")
card1 <- c(card1, ggplot(iris, aes(x = Petal.Length)) + geom_histogram())
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
lyt <- analyze(split_rows_by(basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- build_table(lyt, airquality)
#> Split var [Day] was not character or factor. Converting to factor
card2 <- teal_card(
"## Header 2 text",
"A paragraph of default text",
table_res2
)
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_blocks()
#> $`7857f2a8`
#> [1] "# _Unnamed Card (1)_"
#>
#> $`1fe5e2c9`
#> [1] "## Header 2 text"
#>
#> $`5ca3e079`
#> [1] "A paragraph of default text"
#>
#> $e2cc4e61
#>
#> $`65138471`
#> [1] "\\newpage"
#>
#> $`6adea753`
#> [1] "# _Unnamed Card (2)_"
#>
#> $e9090158
#> [1] "## Header 2 text"
#>
#> $`99daf383`
#> [1] "A paragraph of default text"
#>
#> $e45084d1
#> all obs
#> —————————————————————————
#> 1
#> mean 77.75
#> 2
#> mean 43
#> 3
#> mean 33.25
#> 4
#> mean 62.3333333333333
#> 5
#> mean 48.6666666666667
#> 6
#> mean 41.5
#> 7
#> mean 54.2
#> 8
#> mean 57
#> 9
#> mean 61.4
#> 10
#> mean 49.3333333333333
#> 11
#> mean 25.5
#> 12
#> mean 22.75
#> 13
#> mean 23.4
#> 14
#> mean 29.3333333333333
#> 15
#> mean 12.6666666666667
#> 16
#> mean 30.2
#> 17
#> mean 36.6
#> 18
#> mean 24.6
#> 19
#> mean 35.2
#> 20
#> mean 29.4
#> 21
#> mean 12.75
#> 22
#> mean 14.3333333333333
#> 23
#> mean 20
#> 24
#> mean 41
#> 25
#> mean 96.6666666666667
#> 26
#> mean 41
#> 27
#> mean 52
#> 28
#> mean 48.75
#> 29
#> mean 57.75
#> 30
#> mean 70.75
#> 31
#> mean 60.3333333333333
#>
#> attr(,"class")
#> [1] "teal_card"
#> attr(,"metadata")
#> list()
## ------------------------------------------------
## Method `Reporter$new`
## ------------------------------------------------
reporter <- Reporter$new()
## ------------------------------------------------
## Method `Reporter$get_metadata`
## ------------------------------------------------
reporter <- Reporter$new()$append_metadata(list(sth = "sth"))
reporter$get_metadata()
#> $sth
#> [1] "sth"
#>
## ------------------------------------------------
## Method `Reporter$append_metadata`
## ------------------------------------------------
reporter <- Reporter$new()$append_metadata(list(sth = "sth"))
reporter$get_metadata()
#> $sth
#> [1] "sth"
#>
## ------------------------------------------------
## Method `Reporter$from_reporter`
## ------------------------------------------------
reporter <- Reporter$new()
reporter$from_reporter(reporter)
#> Warning: `Reporter$from_reporter()` was deprecated in teal.reporter 0.5.0.9000.
## ------------------------------------------------
## Method `Reporter$to_list`
## ------------------------------------------------
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "testdir")
dir.create(tmp_dir)
reporter$to_list(tmp_dir)
#> $name
#> [1] "teal Reporter"
#>
#> $version
#> [1] "1"
#>
#> $id
#> [1] ""
#>
#> $cards
#> list()
#>
#> $metadata
#> list()
#>
## ------------------------------------------------
## Method `Reporter$from_list`
## ------------------------------------------------
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "testdir")
unlink(tmp_dir, recursive = TRUE)
dir.create(tmp_dir)
reporter$from_list(reporter$to_list(tmp_dir), tmp_dir)
## ------------------------------------------------
## Method `Reporter$to_jsondir`
## ------------------------------------------------
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
reporter$to_jsondir(tmp_dir)
#> [1] "/tmp/RtmpJnD3n8/jsondir"
## ------------------------------------------------
## Method `Reporter$from_jsondir`
## ------------------------------------------------
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
#> Warning: '/tmp/RtmpJnD3n8/jsondir' already exists
unlink(list.files(tmp_dir, recursive = TRUE))
reporter$to_jsondir(tmp_dir)
#> [1] "/tmp/RtmpJnD3n8/jsondir"
reporter$from_jsondir(tmp_dir)
## ------------------------------------------------
## Method `Reporter$set_template`
## ------------------------------------------------
reporter <- teal.reporter::Reporter$new()
template_fun <- function(document) {
disclaimer <- teal.reporter::teal_card("Here comes disclaimer text")
c(disclaimer, document)
}
reporter$set_template(template_fun)
doc1 <- teal.reporter::teal_card("## Header 2 text", "Regular text")
metadata(doc1, "title") <- "Welcome card"
reporter$append_cards(doc1)
reporter$get_cards()
#> $card_5a97de3f
#> $`47d5dfaa`
#> [1] "Here comes disclaimer text"
#>
#> $`6362e88a`
#> [1] "## Header 2 text"
#>
#> $ae6372d6
#> [1] "Regular text"
#>
#> attr(,"class")
#> [1] "teal_card"
#> attr(,"metadata")
#> attr(,"metadata")$title
#> [1] "Welcome card"
#>
#>