The class Tabula can be used to calculate the crosstabs
specified on the Questions sheet of the Excel mapping file.
Super class
datadaptor::Mapping -> Tabula
Public fields
dat_moddat_modmodified data field of the super-classdatadaptor::Mapping.mapping_filemapping_filefile path field of the super-classdatadaptor::Mapping.datdatinput data field of the super-classdatadaptor::Mapping. If this is specified,dat_modwill be ignored, and instead generated withdatadaptor::Mapping$modify_data()qrowsA
list()ofQrowobjectsditwThis is the "dust in the wind" list object field that stores data that didn't make it into their own field. For developers only! For reproducible code you should NEVER rely on this field as it might be subject to change without any warning. This overwrites the
datadaptor::Mapping$ditwfield; the list field additionally contains thectelement.
Methods
Method new()
Initialize a Tabula object
Usage
Tabula$new(
dat_mod = NULL,
mapping_file = NULL,
row = NULL,
dat = NULL,
tabulate = TRUE,
...
)Arguments
dat_moddat_modmodified data field of the super-classdatadaptor::Mapping.mapping_filemapping_filefile path field of the super-classdatadaptor::Mapping.rowNumeric vector with the row numbers in the Questions sheet, where crosstabs should be calculated, when calling
Tabula$calc_crosstabs(). OrNULL(the default) resulting in the selection of all row numbers whereTypeis specified.datdatinput data field of the super-classdatadaptor::Mapping. If this is specified,dat_modwill be ignored, and instead generated withdatadaptor::Mapping$modify_data()tabulateLogical, whether to call the
Tabula$calc_crosstabs()method when initializing (defaults toTRUE)....Arguments passed to
Tabula$set_options()
Method set_options()
Set Tabula options.
This overwrites datadaptor::Mapping$set_options()
Arguments
...Arguments passed to
get_tabula_options().
Method save_html_app()
Write a table_charter app html file of the crosstab data
Usage
Tabula$save_html_app(
template_file =
"https://gitlab.com/urswilke/table_charter/-/raw/main/example_dashboard.html",
output_file = "dashboard.html",
project_data = NULL
)Arguments
template_filePath to the template file (see description).
output_fileFile path to the table_charter app html file.
project_dataEither a
list()object to modify the default:list(logo_base64 = "", logo_url = "https://gitlab.com/urswilke/table_charter/-/raw/main/img/logo_small.svg", title = "Dashboard", date = Sys.Date()), orNULL(the default). IfNULL, nothing is done. The fields will modify the elements in the header of the dashboard.
Details
This needs a valid html template_file, i.e. one of:
The file example_dashboard.html which is directly scraped from the table_charter repo by default (no installation of table_charter needed).
For deploying it in the web or running it on a dev server, you need to install table_charter first, and then use the file index.html on your machine.
After installing, you can also generate a standalone html file (without the need to download javascript libraries) by running:
npm run standalone-buildand then using the template file created in the
dist/sub-directory.
Method get_crosstabs_data()
Return the crosstabs data of the Tabula object
This method returns a list of dataframes containing all the crosstabs information. Thus it's not chainable.
Returns
A list of dataframes with the data of the crosstabs;
see vignette("data-format").
Method print()
Print the crosstabs of the Tabula object
This method is called under the hood, if you print() a Tabula object.
This will call the print method of all Qrow elements in the Tabula$qrows field.
Examples
df <- tibble::tibble(
q1 = c(1, 2, 1) |> haven::labelled(c(Yes = 1, No = 2), label = "Super important question"),
age = c(2, 1, 1) |> haven::labelled(c("18-39" = 1, "40+" = 2), label = "age")
)
mapping_file = list(
Questions = data.frame(
Type = "cat",
RowVar = "q1",
Title = "The crosstab's title"
),
Macro = list(ColVar = "age")
)
m <- Tabula$new(df, mapping_file)
m
#> $`2`
#> $`2`[[1]]
#> # The crosstab's title
#> TOTAL age -----
#> 18-39 40+
#> TOTAL abs 3 2 1
#> Yes abs 2 1 1
#> in % 66.7 50 100
#> No abs 1 1 0
#> in % 33.3 50 0
#> VALID CASES abs 3 2 1
#> in % 100 100 100
#>
#>
# The previous line prints the "Tabula" object.
# Under the hood, a list of `Qrow` objects were generated.
# Printing `m` prints the list of `Qtab` elements of each `Qrow`:
m$qrows
#> $`2`
#> <Qrow>
#> Public:
#> clone: function (deep = FALSE)
#> ditw: list
#> initialize: function (df_qrow, mapping, ...)
#> log: list
#> m: Tabula, Mapping, R6
#> p: list
#> qtabs: list
#> Private:
#> prep_tab_row_val: function ()
#>
# For instance, this prints the list of `Qtab` elements
# of the first `Qrow` element:
m$qrows[[1]]$qtabs |> print()
#> [[1]]
#> # The crosstab's title
#> TOTAL age -----
#> 18-39 40+
#> TOTAL abs 3 2 1
#> Yes abs 2 1 1
#> in % 66.7 50 100
#> No abs 1 1 0
#> in % 33.3 50 0
#> VALID CASES abs 3 2 1
#> in % 100 100 100
#>