| Title: | Generate Crosstabs of Labelled Data Sets with Excel Files |
|---|---|
| Description: | An R package to use commands in Excel files to generate crosstabs of labelled data sets (usually survey data). The crosstabs can be printed to the console, and serve as an input for an app to plot them interactively. |
| Authors: | Urs Wilke [aut, cre] (ORCID: <https://orcid.org/0000-0001-7257-2524>), Wolf Wilke [aut] |
| Maintainer: | Urs Wilke <[email protected]> |
| License: | AGPL (>= 3) |
| Version: | 0.0.0.9000 |
| Built: | 2026-06-02 20:56:02 UTC |
| Source: | https://codeberg.org/urswilke/crosstabser |
This function first calls datadaptor::create_mapping_workbook().
Additionally to the sheets "Variables", "Label", "Verbatims" & "Free",
it will insert 2 more sheets "Macro" & "Questions".
The Excel workbook is then written to the file mapping_file.
Please refer to vignette("crosstabser") & vignette("questions-parameters")
for details how to use the mapping.
create_tabula(df_raw, mapping_file, mapping_type = "excel")create_tabula(df_raw, mapping_file, mapping_type = "excel")
df_raw |
dataframe with labelled variables, e.g. resulting from haven::read_sav |
mapping_file |
name of the Excel file to be created |
mapping_type |
String specifying the mapping type. Either "excel" or "list". Defaults to "excel". |
spss_file <- system.file( "extdata", "fruit_survey.sav", package = "datadaptor" ) df <- haven::read_sav(spss_file) # The next command creates an empty mapping file `mapping.xlsx`: ## Not run: create_tabula(df, "mapping.xlsx") ## End(Not run)spss_file <- system.file( "extdata", "fruit_survey.sav", package = "datadaptor" ) df <- haven::read_sav(spss_file) # The next command creates an empty mapping file `mapping.xlsx`: ## Not run: create_tabula(df, "mapping.xlsx") ## End(Not run)
This helper function generates a json string containing
the data produced by Tabula$get_crosstabs_data(),
and which then can be put as the "data" attribute
of the <table-charter> app. It is also used by Tabula$save_html_app()
gen_data_json(l)gen_data_json(l)
l |
list object generated by |
A character string
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$get_crosstabs_data() |> gen_data_json()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$get_crosstabs_data() |> gen_data_json()
Tabula objectGenerate list of options for a Tabula object
get_tabula_options(tabula, book_no = NULL, ...)get_tabula_options(tabula, book_no = NULL, ...)
tabula |
An object generated with |
book_no |
An integer skalar to identify the |
... |
Arguments passed to methods |
## Not run: # TODO: document! - for this we should add an example excel mapping file to the crosstabser package - and then add a docs example, something like this: # Only for documentation purposes: # (`get_mapping_options()` isn't supposed to be be called directly). mapping_file <- system.file( "extdata", "<file-to-be-created-mapping.xlsx>", package = "crosstabser" ) m <- Tabula$new(mapping_file = mapping_file) # Result of datadaptor::get_mapping_options() in `da` field: m$opts$da # Result of get_tabula_options() in `da` field: m$opts$ct ## End(Not run)## Not run: # TODO: document! - for this we should add an example excel mapping file to the crosstabser package - and then add a docs example, something like this: # Only for documentation purposes: # (`get_mapping_options()` isn't supposed to be be called directly). mapping_file <- system.file( "extdata", "<file-to-be-created-mapping.xlsx>", package = "crosstabser" ) m <- Tabula$new(mapping_file = mapping_file) # Result of datadaptor::get_mapping_options() in `da` field: m$opts$da # Result of get_tabula_options() in `da` field: m$opts$ct ## End(Not run)
Print object of class "Qtab"
## S3 method for class 'Qtab' print(x, ...)## S3 method for class 'Qtab' print(x, ...)
x |
|
... |
Arguments passed to |
# see `?Tabula`# see `?Tabula`
This is not supposed to be used directly.
When creating a "Tabula" object,
this will generate a list of Qrow objects in its $qrows field.
pparameters extracted from df_qrow
mTabula object
qtabslist of Qtabs objects
loglog entries
ditwThis 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.
new()
Qrow$new(df_qrow, mapping, ...)
df_qrowrow of the Questions dataframe
mappingTabula object
...Not used at the moment.
clone()
The objects of this class are cloneable with this method.
Qrow$clone(deep = FALSE)
deepWhether to make a deep clone.
# see `?Tabula`# see `?Tabula`
This is not supposed to be used directly.
When creating a "Tabula" object,
this will generate a list of Qrow objects in its $qrows field,
themselves each containing a list of Qtab objects in their $qtabs fields.
Qtab objects have a custom print method print.Qtab
(see examples in ?Tabula).
pparameters
ddata
mTabula object
new()
Qtab$new(params, mapping, ...)
paramsParameters from Qrow object
mappingTabula object
...Not used at the moment.
clone()
The objects of this class are cloneable with this method.
Qtab$clone(deep = FALSE)
deepWhether to make a deep clone.
# see `?Tabula`# see `?Tabula`
The class Tabula can be used to calculate the crosstabs
specified on the Questions sheet of the Excel mapping file.
datadaptor::Mapping -> Tabula
dat_moddat_mod modified data field of the super-class datadaptor::Mapping.
mapping_filemapping_file file path field of the super-class datadaptor::Mapping.
datdat input data field of the super-class datadaptor::Mapping.
If this is specified, dat_mod will be ignored,
and instead generated with datadaptor::Mapping$modify_data()
qrowsA list() of Qrow objects
ditwThis 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$ditw field;
the list field additionally contains the ct element.
new()
Initialize a Tabula object
Tabula$new( dat_mod = NULL, mapping_file = NULL, row = NULL, dat = NULL, tabulate = TRUE, ... )
dat_moddat_mod modified data field of the super-class datadaptor::Mapping.
mapping_filemapping_file file path field of the super-class datadaptor::Mapping.
rowNumeric vector with the row numbers in the Questions sheet,
where crosstabs should be calculated, when calling Tabula$calc_crosstabs().
Or NULL (the default) resulting in the selection of all row numbers
where Type is specified.
datdat input data field of the super-class datadaptor::Mapping.
If this is specified, dat_mod will be ignored,
and instead generated with datadaptor::Mapping$modify_data()
tabulateLogical, whether to call the Tabula$calc_crosstabs()
method when initializing (defaults to TRUE).
...Arguments passed to Tabula$set_options()
set_options()
Set Tabula options.
This overwrites datadaptor::Mapping$set_options()
Tabula$set_options(...)
...Arguments passed to get_tabula_options().
calc_crosstabs()
Calculate the crosstabs
Tabula$calc_crosstabs(row = NULL)
rowNumeric vector with the row numbers in the Questions sheet,
where crosstabs should be calculated, when calling Tabula$calc_crosstabs().
Or NULL (the default) resulting in the selection of all row numbers
where Type is specified.
save_html_app()
Write a table_charter app html file of the crosstab data
Tabula$save_html_app(
template_file =
"https://gitlab.com/urswilke/table_charter/-/raw/main/example_dashboard.html",
output_file = "dashboard.html",
project_data = NULL
)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()), or NULL (the default).
If NULL, nothing is done.
The fields will modify the elements in the header of the dashboard.
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-build
and then using the template file created in the dist/ sub-directory.
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.
Tabula$get_crosstabs_data()
A list of dataframes with the data of the crosstabs;
see vignette("data-format").
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.
Tabula$print(...)
...Not used for now.
clone()
The objects of this class are cloneable with this method.
Tabula$clone(deep = FALSE)
deepWhether to make a deep clone.
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 # 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 # For instance, this prints the list of `Qtab` elements # of the first `Qrow` element: m$qrows[[1]]$qtabs |> print()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 # 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 # For instance, this prints the list of `Qtab` elements # of the first `Qrow` element: m$qrows[[1]]$qtabs |> print()
This helper function is used by Tabula$save_html_app()
to write a table_charter app html file of crosstab data.
write_html_app( data_string, template_file = "https://gitlab.com/urswilke/table_charter/-/raw/main/example_dashboard.html", output_file = "dashboard.html", project_data = NULL )write_html_app( data_string, template_file = "https://gitlab.com/urswilke/table_charter/-/raw/main/example_dashboard.html", output_file = "dashboard.html", project_data = NULL )
data_string |
Crosstab data produced with
|
template_file |
Path to the template file (see description). |
output_file |
File path to the table_charter app html file. |
project_data |
Either a |
No value's returned. This function writes a file.
# See documentation of `Tabula$save_html_app()`# See documentation of `Tabula$save_html_app()`