Skip to contents

[Experimental] R6 class that stores and manages report cards.

Note

The function has to be used in the shiny reactive context.

Methods


Method new()

Returns a Reporter object.

Usage

Reporter$new()

Returns

a Reporter object

Examples

reporter <- teal.reporter::Reporter$new()


Method append_cards()

Appends a table to this Reporter.

Usage

Reporter$append_cards(cards)

Arguments

cards

ReportCard or a list of such objects

Returns

invisibly self

Examples

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::build_table(lyt, airquality)
card2$append_table(table_res2)
card2$append_table(iris)

reporter <- teal.reporter::Reporter$new()
reporter$append_cards(list(card1, card2))


Method get_cards()

Returns cards of this Reporter.

Usage

Reporter$get_cards()

Returns

list() list of ReportCard

Examples

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::build_table(lyt, airquality)
card2$append_table(table_res2)
card2$append_table(iris)

reporter <- teal.reporter::Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_cards()


Method get_blocks()

Returns blocks of all ReportCard of this Reporter.

Usage

Reporter$get_blocks(sep = NewpageBlock$new())

Arguments

sep

the element inserted between each content element in this Reporter. Pass NULL to return content without any additional elements. Default: NewpageBlock$new()

Returns

list() list of TableBlock, TextBlock, PictureBlock and NewpageBlock

Examples

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::build_table(lyt, airquality)
card2$append_table(table_res2)
card2$append_table(iris)

reporter <- teal.reporter::Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_blocks()


Method reset()

Removes all ReportCard objects added to this Reporter. Additionally all metadata are removed.

Usage

Reporter$reset()

Returns

invisibly self


Method remove_cards()

remove a specific Card in the Reporter

Usage

Reporter$remove_cards(ids = NULL)

Arguments

ids

integer the indexes of cards

Returns

invisibly self


Method swap_cards()

swap two cards in the Reporter

Usage

Reporter$swap_cards(start, end)

Arguments

start

integer the index of the first card

end

integer the index of the second card

Returns

invisibly self


Method get_reactive_add_card()

get a value for the reactive value for the add card

Usage

Reporter$get_reactive_add_card()

Returns

reactive_add_card field value

Examples

shiny::isolate(Reporter$new()$get_reactive_add_card())


Method get_metadata()

get metadata of this Reporter.

Usage

Reporter$get_metadata()

Returns

metadata

Examples

reporter <- Reporter$new()$append_metadata(list(sth = "sth"))
reporter$get_metadata()


Method append_metadata()

Appends metadata to this Reporter.

Usage

Reporter$append_metadata(meta)

Arguments

meta

(list) of metadata.

Returns

invisibly self

Examples

reporter <- Reporter$new()$append_metadata(list(sth = "sth"))
reporter$get_metadata()


Method from_reporter()

Create/Recreate a Reporter from another Reporter

Usage

Reporter$from_reporter(reporter)

Arguments

reporter

Reporter instance.

Returns

invisibly self

Examples

reporter <- Reporter$new()
reporter$from_reporter(reporter)


Method to_list()

Convert a Reporter to a list and transfer files

Usage

Reporter$to_list(output_dir)

Arguments

output_dir

character(1) a path to the directory where files will be copied.

Returns

named list Reporter representation

Examples

reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "testdir")
dir.create(tmp_dir)
reporter$to_list(tmp_dir)


Method from_list()

Create/Recreate a Reporter from a list and directory with files

Usage

Reporter$from_list(rlist, output_dir)

Arguments

rlist

named list Reporter representation.

output_dir

character(1) a path to the directory from which files will be copied.

Returns

invisibly self

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

Create/Recreate a Reporter to a directory with JSON file and static files

Usage

Reporter$to_jsondir(output_dir)

Arguments

output_dir

character(1) a path to the directory where files will be copied, JSON and statics.

Returns

invisibly self

Examples

reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
reporter$to_jsondir(tmp_dir)


Method from_jsondir()

Create/Recreate a Reporter from a directory with JSON file and static files

Usage

Reporter$from_jsondir(output_dir)

Arguments

output_dir

character(1) a path to the directory with files, JSON and statics.

Returns

invisibly self

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

The objects of this class are cloneable with this method.

Usage

Reporter$clone(deep = FALSE)

Arguments

deep

Whether to make a deep clone.

Examples


## ------------------------------------------------
## Method `Reporter$new`
## ------------------------------------------------

reporter <- teal.reporter::Reporter$new()


## ------------------------------------------------
## Method `Reporter$append_cards`
## ------------------------------------------------

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::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 <- teal.reporter::Reporter$new()
reporter$append_cards(list(card1, card2))


## ------------------------------------------------
## Method `Reporter$get_cards`
## ------------------------------------------------

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::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 <- teal.reporter::Reporter$new()
reporter$append_cards(list(card1, card2))
reporter$get_cards()
#> [[1]]
#> <ReportCard>
#>   Public:
#>     append_content: 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) 
#>     metadata: list
#>     name: 
#> 
#> [[2]]
#> <ReportCard>
#>   Public:
#>     append_content: 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) 
#>     metadata: list
#>     name: 
#> 

## ------------------------------------------------
## Method `Reporter$get_blocks`
## ------------------------------------------------

card1 <- teal.reporter::ReportCard$new()

card1$append_text("Header 2 text", "header2")
card1$append_text("A paragraph of default text", "header2")
card1$append_plot(
 ggplot2::ggplot(iris, ggplot2::aes(x = Petal.Length)) + ggplot2::geom_histogram()
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.

card2 <- teal.reporter::ReportCard$new()

card2$append_text("Header 2 text", "header2")
card2$append_text("A paragraph of default text", "header2")
lyt <- rtables::analyze(rtables::split_rows_by(rtables::basic_table(), "Day"), "Ozone", afun = mean)
table_res2 <- rtables::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 <- teal.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/Rtmp4XVYd2/file1bf2cef9df8.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/Rtmp4XVYd2/file1bf5b509f36.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/Rtmp4XVYd2/file1bfcce4c31.rds
#>     deep_clone: function (name, value) 
#>     supported_tables: data.frame rtables TableTree ElementaryTable listing_df
#> 


## ------------------------------------------------
## Method `Reporter$get_reactive_add_card`
## ------------------------------------------------

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)
#> $version
#> [1] "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/Rtmp4XVYd2/jsondir"

## ------------------------------------------------
## Method `Reporter$from_jsondir`
## ------------------------------------------------

reporter <- Reporter$new()
tmp_dir <- file.path(tempdir(), "jsondir")
dir.create(tmp_dir)
#> Warning: '/tmp/Rtmp4XVYd2/jsondir' already exists
unlink(list.files(tmp_dir, recursive = TRUE))
reporter$to_jsondir(tmp_dir)
#> [1] "/tmp/Rtmp4XVYd2/jsondir"
reporter$from_jsondir(tmp_dir)