This R6
class is designed to store and manage report cards,
facilitating the creation, manipulation, and serialization of report-related data.
Note
The function has to be used in the shiny reactive context.
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 get_cards()
Retrieves all ReportCard
objects contained in the Reporter
.
Returns
A (list
) of ReportCard
objects.
Method get_blocks()
Compiles and returns all content blocks from the ReportCard
in the Reporter
.
Usage
Reporter$get_blocks(sep = NewpageBlock$new())
Arguments
sep
An optional separator to insert between each content block. Default is a
NewpageBlock$new()
object.
Returns
list()
list of TableBlock
, TextBlock
, PictureBlock
and NewpageBlock
.
Method reset()
Resets the Reporter
, removing all ReportCard
objects and metadata.
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 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.
Examples
library(ggplot2)
library(rtables)
card1 <- ReportCard$new()
card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
ggplot(iris, aes(x = Petal.Length)) + geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
card2 <- ReportCard$new()
card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
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$append_table(table_res2)
card2$append_table(iris)
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
library(ggplot2)
library(rtables)
card1 <- ReportCard$new()
card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
ggplot(iris, aes(x = Petal.Length)) + geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
card2 <- ReportCard$new()
card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
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$append_table(table_res2)
card2$append_table(iris)
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_cards()
#> [[1]]
#> <ReportCard>
#> Public:
#> append_content: function (content)
#> append_html: function (content)
#> append_metadata: function (key, value)
#> append_plot: function (plot, dim = NULL)
#> append_rcode: function (text, ...)
#> append_table: function (table)
#> append_text: function (text, style = TextBlock$new()$get_available_styles()[1])
#> clone: function (deep = FALSE)
#> from_list: function (card, output_dir)
#> get_content: function ()
#> get_metadata: function ()
#> get_name: function ()
#> initialize: function ()
#> reset: function ()
#> set_name: function (name)
#> to_list: function (output_dir)
#> Private:
#> content: list
#> deep_clone: function (name, value)
#> dispatch_block: function (block_class)
#> metadata: list
#> name:
#>
#> [[2]]
#> <ReportCard>
#> Public:
#> append_content: function (content)
#> append_html: function (content)
#> append_metadata: function (key, value)
#> append_plot: function (plot, dim = NULL)
#> append_rcode: function (text, ...)
#> append_table: function (table)
#> append_text: function (text, style = TextBlock$new()$get_available_styles()[1])
#> clone: function (deep = FALSE)
#> from_list: function (card, output_dir)
#> get_content: function ()
#> get_metadata: function ()
#> get_name: function ()
#> initialize: function ()
#> reset: function ()
#> set_name: function (name)
#> to_list: function (output_dir)
#> Private:
#> content: list
#> deep_clone: function (name, value)
#> dispatch_block: function (block_class)
#> metadata: list
#> name:
#>
library(ggplot2)
library(rtables)
card1 <- ReportCard$new()
card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
ggplot(iris, aes(x = Petal.Length)) + geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
card2 <- ReportCard$new()
card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
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$append_table(table_res2)
card2$append_table(iris)
reporter <- Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_blocks()
#> [[1]]
#> <TextBlock>
#> Inherits from: <ContentBlock>
#> Public:
#> clone: function (deep = FALSE)
#> from_list: function (x)
#> get_available_styles: function ()
#> get_content: function ()
#> get_style: function ()
#> initialize: function (content = character(0), style = private$styles[1])
#> set_content: function (content)
#> set_style: function (style)
#> to_list: function ()
#> Private:
#> content: Header 2 text
#> deep_clone: function (name, value)
#> style: header2
#> styles: default header2 header3 verbatim
#>
#> [[2]]
#> <TextBlock>
#> Inherits from: <ContentBlock>
#> Public:
#> clone: function (deep = FALSE)
#> from_list: function (x)
#> get_available_styles: function ()
#> get_content: function ()
#> get_style: function ()
#> initialize: function (content = character(0), style = private$styles[1])
#> set_content: function (content)
#> set_style: function (style)
#> to_list: function ()
#> Private:
#> content: A paragraph of default text
#> deep_clone: function (name, value)
#> style: header2
#> styles: default header2 header3 verbatim
#>
#> [[3]]
#> <PictureBlock>
#> Inherits from: <FileBlock>
#> Public:
#> clone: function (deep = FALSE)
#> finalize: function ()
#> from_list: function (x, output_dir)
#> get_content: function ()
#> get_dim: function ()
#> get_title: function ()
#> initialize: function (plot)
#> set_content: function (content)
#> set_dim: function (dim)
#> set_title: function (title)
#> to_list: function (output_dir)
#> Private:
#> content: /tmp/RtmpuTjhom/filebe45a92e34c.png
#> deep_clone: function (name, value)
#> dim: 800 600
#> supported_plots: ggplot grob trellis Heatmap
#> title:
#> type:
#>
#> [[4]]
#> <NewpageBlock>
#> Inherits from: <ContentBlock>
#> Public:
#> clone: function (deep = FALSE)
#> from_list: function (x)
#> get_content: function ()
#> initialize: function ()
#> set_content: function (content)
#> to_list: function ()
#> Private:
#> content:
#> \newpage
#>
#> deep_clone: function (name, value)
#>
#> [[5]]
#> <TextBlock>
#> Inherits from: <ContentBlock>
#> Public:
#> clone: function (deep = FALSE)
#> from_list: function (x)
#> get_available_styles: function ()
#> get_content: function ()
#> get_style: function ()
#> initialize: function (content = character(0), style = private$styles[1])
#> set_content: function (content)
#> set_style: function (style)
#> to_list: function ()
#> Private:
#> content: Header 2 text
#> deep_clone: function (name, value)
#> style: header2
#> styles: default header2 header3 verbatim
#>
#> [[6]]
#> <TextBlock>
#> Inherits from: <ContentBlock>
#> Public:
#> clone: function (deep = FALSE)
#> from_list: function (x)
#> get_available_styles: function ()
#> get_content: function ()
#> get_style: function ()
#> initialize: function (content = character(0), style = private$styles[1])
#> set_content: function (content)
#> set_style: function (style)
#> to_list: function ()
#> Private:
#> content: A paragraph of default text
#> deep_clone: function (name, value)
#> style: header2
#> styles: default header2 header3 verbatim
#>
#> [[7]]
#> <TableBlock>
#> Inherits from: <FileBlock>
#> Public:
#> clone: function (deep = FALSE)
#> finalize: function ()
#> from_list: function (x, output_dir)
#> get_content: function ()
#> initialize: function (table)
#> set_content: function (content)
#> to_list: function (output_dir)
#> Private:
#> content: /tmp/RtmpuTjhom/filebe47fb36972.rds
#> deep_clone: function (name, value)
#> supported_tables: data.frame rtables TableTree ElementaryTable listing_df
#>
#> [[8]]
#> <TableBlock>
#> Inherits from: <FileBlock>
#> Public:
#> clone: function (deep = FALSE)
#> finalize: function ()
#> from_list: function (x, output_dir)
#> get_content: function ()
#> initialize: function (table)
#> set_content: function (content)
#> to_list: function (output_dir)
#> Private:
#> content: /tmp/RtmpuTjhom/filebe479896293.rds
#> deep_clone: function (name, value)
#> supported_tables: data.frame rtables TableTree ElementaryTable listing_df
#>
## ------------------------------------------------
## Method `Reporter$new`
## ------------------------------------------------
reporter <- Reporter$new()
## ------------------------------------------------
## Method `Reporter$get_reactive_add_card`
## ------------------------------------------------
library(shiny)
isolate(Reporter$new()$get_reactive_add_card())
#> [1] 0
## ------------------------------------------------
## 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)
## ------------------------------------------------
## 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/RtmpuTjhom/jsondir"
## ------------------------------------------------
## Method `Reporter$from_jsondir`
## ------------------------------------------------
reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
#> Warning: '/tmp/RtmpuTjhom/jsondir' already exists
unlink(list.files(tmp_dir, recursive = TRUE))
reporter$to_jsondir(tmp_dir)
#> [1] "/tmp/RtmpuTjhom/jsondir"
reporter$from_jsondir(tmp_dir)