# For Console:
atable_compact(
atable::test_data,
target_cols = c("Numeric", "Numeric2", "Split2", "Factor", "Ordered"),
group_col = "Group2",
blocks = list("Primary Endpoint" = "Numeric",
"Secondary Endpoints" = c("Numeric2", "Split2", "Factor")),
add_margins = TRUE)
# The target_cols are "Numeric", "Numeric2", "Split2", "Factor", "Ordered".
# The group_col is "Group2".
# The data.frame is grouped by group_col and the summary statistcs of the target_cols are
# calculated: mean, sd for numeric, counts and percentages for factors.
# Some target_cols are blocked: the first block 'Primary Endpoint' contains the variable Numeric.
# The second block 'Secondary Endpoint' contains the variables "Numeric2", "Split2", "Factor".
# The blocks are intended.
# For variable Split2 only the first level is reported, as the variable has only two levels and
# the name 'Split2' does not appear in the table.
# The variable Factor has more than two levels, so all of them are
# reported and appropriately intended.
# The variable Ordered is not part of a block and thus not intended.
# For Latex:
# Same as for Console, but with different indent_character:
tab = atable_compact(atable::test_data,
target_cols = c("Numeric", "Numeric2", "Logical", "Factor", "Ordered"),
group_col = "Group2",
indent_character = "\\quad")
tab = atable::translate_to_LaTeX(tab)
# Then call e.g. Hmisc::latex(tab, ...)
# Example for Word format:
if (FALSE) {
tab = atable_compact(
atable::test_data,
target_cols = c("Numeric", "Numeric2", "Split2", "Factor", "Ordered", "Character"),
group_col = "Group2",
blocks = list("Primary Endpoint" = "Numeric",
"Secondary Endpoints" = c("Numeric2", "Split2", "Factor")),
add_margins = TRUE,
indent_character = paste0(rep(intToUtf8(160), 5), collapse = ""))
# The argument indent_character has the value intToUtf8(160) (non breakable space).
# This is the important part:
# Spaces at the beginning of a cell of a data.frame are somehow lost on the way to the docx.
# Other indent_characters may also do the job.
# doc = officer::read_docx()
# doc = officer::body_add_table(doc,tab)
# print(doc, target = "atable_Word.docx")
# Other packages may exist for Word-export.
}
Run the code above in your browser using DataLab