Fields are ingredients.
A block is a recipe step:
data |> select(<COLNAMES>)
.1new_select_block <- function (columns = character(), ...) {
all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
select_expr <- quote(dplyr::select(.(columns)))
new_block(
fields = fields,
expr = select_expr,
class = c("select_block", "transform_block"),
...
)
}
new_select_block <- function (columns = character(), ...)
{
2 all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
select_expr <- quote(dplyr::select(.(columns)))
new_block(
fields = fields,
expr = select_expr,
class = c("select_block", "transform_block"),
...
)
}
new_select_block <- function (columns = character(), ...)
{
all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
3 select_expr <- quote(dplyr::select(.(columns)))
new_block(
fields = fields,
expr = select_expr,
class = c("select_block", "transform_block"),
...
)
}
new_select_block <- function (columns = character(), ...)
{
all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
select_expr <- quote(dplyr::select(.(columns)))
new_block(
fields = fields,
expr = select_expr,
4 class = c("select_block", "transform_block"),
...
)
}
transform_block
.
new_select_block <- function (columns = character(), ...)
{
all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
select_expr <- quote(dplyr::select(.(columns)))
new_block(
fields = fields,
expr = select_expr,
class = c("select_block", "transform_block"),
5 ...
)
}
...
for extra parameters like submit
.
new_select_block <- function (columns = character(), ...)
{
all_cols <- function(data) colnames(data)
fields <- list(
columns = new_select_field(columns, all_cols, multiple = TRUE, title = "Columns")
)
select_expr <- quote(dplyr::select(.(columns)))
6 new_block(
fields = fields,
expr = select_expr,
class = c("select_block", "transform_block"),
...
)
}
Create a new cardinal1 block:
cardinal::make_table_09_gtsum
R/cardinal.R
.new_switch_field(<VALUE>)
and new_select_field(values, choices)
for other parameters. You can use the existing all_cols
for dynamic choices.new_cardinal09_block <- function(
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
pref_var = "AEDECOD",
show_colcounts = TRUE,
...) {
1 all_cols <- function(data) colnames(data)
2 fields <- list(
id_var = new_select_field(id_var, all_cols, title = "ID"),
arm_var = new_select_field(arm_var, all_cols, title = "ARM"),
saffl_var = new_select_field(saffl_var, all_cols, title = "SAFFL"),
pref_var = new_select_field(pref_var, all_cols, title = "Pref"),
show_colcounts = new_switch_field(show_colcounts, title = "Show column counts")
)
new_block(
fields = fields,
)
}
new_cardinal09_block <- function(
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
pref_var = "AEDECOD",
show_colcounts = TRUE,
...) {
all_cols <- function(data) colnames(data)
fields <- list(
id_var = new_select_field(id_var, all_cols, title = "ID"),
arm_var = new_select_field(arm_var, all_cols, title = "ARM"),
saffl_var = new_select_field(saffl_var, all_cols, title = "SAFFL"),
pref_var = new_select_field(pref_var, all_cols, title = "Pref"),
show_colcounts = new_switch_field(show_colcounts, title = "Show column counts")
)
new_block(
fields = fields,
expr = quote({
cardinal::make_table_09_gtsum(
adae = data,
# TO DO
)
})
)
}
cardinal::make_table_09_gtsum
call to pass in fields name like so: <param_name> = .(field_name)
. This strange notation is needed by bquote
for partial substitution.new_cardinal09_block <- function(
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
pref_var = "AEDECOD",
show_colcounts = TRUE,
...) {
all_cols <- function(data) colnames(data)
fields <- list(
id_var = new_select_field(id_var, all_cols, title = "ID"),
arm_var = new_select_field(arm_var, all_cols, title = "ARM"),
saffl_var = new_select_field(saffl_var, all_cols, title = "SAFFL"),
pref_var = new_select_field(pref_var, all_cols, title = "Pref"),
show_colcounts = new_switch_field(show_colcounts, title = "Show column counts")
)
new_block(
fields = fields,
3 expr = quote({
cardinal::make_table_09_gtsum(
adae = data,
arm_var = .(arm_var),
saffl_var = .(saffl_var),
id_var = .(id_var),
pref_var = .(pref_var),
show_colcounts = .(show_colcounts)
)
})
)
}
S3 OO system1: customize behavior depending on object class.
new_cardinal09_block <- function(
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
pref_var = "AEDECOD",
show_colcounts = TRUE,
...) {
all_cols <- function(data) colnames(data)
fields <- list(
id_var = new_select_field(id_var, all_cols, title = "ID"),
arm_var = new_select_field(arm_var, all_cols, title = "ARM"),
saffl_var = new_select_field(saffl_var, all_cols, title = "SAFFL"),
pref_var = new_select_field(pref_var, all_cols, title = "Pref"),
show_colcounts = new_switch_field(show_colcounts, title = "Show column counts")
)
new_block(
fields = fields,
expr = quote({
cardinal::make_table_09_gtsum(
adae = data,
arm_var = .(arm_var),
saffl_var = .(saffl_var),
id_var = .(id_var),
pref_var = .(pref_var),
show_colcounts = .(show_colcounts)
)
}),
...,
class = "<TO_REPLACE>"
)
}
Give it the correct classes. Hints:
data_block
class.cardinal::make_table_09_gtsum
does not return data, we can’t call it transform_block
.new_cardinal09_block <- function(
id_var = "USUBJID",
arm_var = "ARM",
saffl_var = "SAFFL",
pref_var = "AEDECOD",
show_colcounts = TRUE,
...) {
all_cols <- function(data) colnames(data)
fields <- list(
id_var = new_select_field(id_var, all_cols, title = "ID"),
arm_var = new_select_field(arm_var, all_cols, title = "ARM"),
saffl_var = new_select_field(saffl_var, all_cols, title = "SAFFL"),
pref_var = new_select_field(pref_var, all_cols, title = "Pref"),
show_colcounts = new_switch_field(show_colcounts, title = "Show column counts")
)
new_block(
fields = fields,
expr = quote({
cardinal::make_table_09_gtsum(
adae = data,
arm_var = .(arm_var),
saffl_var = .(saffl_var),
id_var = .(id_var),
pref_var = .(pref_var),
show_colcounts = .(show_colcounts)
)
}),
...,
4 class = c("cardinal09_block")
)
}
cardinal09_block
. Note that blockr does not support any method for this class. This is important for the following.
pkgload::load_all()
library(blockr)
library(cardinal)
my_stack <- new_stack(
new_dataset_block("cadae", "random.cdisc.data"),
new_cardinal09_block()
)
serve_stack(my_stack)
inst/examples/app.R
(code above) and run it.gt
outputs (server and ui side).cardinal09_block
. We can inherit the transform block one.R/cardinal.R
, have a look at register_cardinal_blocks
.new_cardinal09_block
.R/zzz.R
, call register_cardinal_blocks
to register the block(s) on package load.pkgload::load_all()
library(blockr)
library(cardinal)
my_stack <- new_stack(
new_dataset_block("cadae", "random.cdisc.data")
)
serve_stack(my_stack)
+
button, look for the cardinal09_block
and select it.register_lm_block <- function(pkg) {
register_block(
constructor = new_cardinal09_block,
name = "cardinal09 block",
description = "Create a cardinal09 block",
classes = c("cardinal09_block", "transform_block"),
input = "data.frame",
output = "gt",
package = pkg
)
}
# Put in zzz.R
.onLoad <- function(libname, pkgname) {
register_lm_block(pkgname)
invisible(NULL)
}