ucr.base.tab2flextable: turn ucr.base.tab object into a flextable

View source: R/base_tab2flextable.R

ucr.base.tab2flextableR Documentation

turn ucr.base.tab object into a flextable

Description

this function cleans a ucr.base.tab of its LaTeX elements and produced a flextable roughly corresponding to what you get from latex.ucr.base.tab. Optionally, you can use a template to obtain an order and row-grouping of the variables.

Usage

ucr.base.tab2flextable(
  object,
  template = NULL,
  caption = NULL,
  use.groups = NULL,
  indent = c(3, 3),
  gray = TRUE,
  fontsize = c(11, 11, 9)
)

Arguments

object

an object of class "ucr.base.tab"

template

optional; a data.frame with variables 'group' and 'label' representing the order in which to display the variables and (optional) the grouping of the rows

caption

character; the table caption

use.groups

logical; create table with grouped rows? (The groups must be specified with 'template')

indent

number of whitespace indentations under rowgroups (if any), and under categorical data variables (displaying values)

gray

logical; should every other variable be highlighted?

fontsize

fontsize for header, body and footer of table. Choose smaller ones if fitting onto the page is a problem

Value

a flextable

Examples

n <- 6000
D <- data.frame(
    ## id = sprintf("id%s", 1:n),
    gr2 = factor(rep(c("Group 1", "Group 2"), each = n / 2)),
    gr3 = factor(rep(c(sprintf("Group %s", LETTERS[1:3])), each = n / 3)),
    age = runif(n, 35, 95),
    sex = factor(sample(c("M", "F"), size = n, replace = TRUE)),
    NotInTemplate = runif(n),
    measx = 100*rbeta(n, 2, 1),
    measy = rexp(n, 1/100),
    catgx = factor(sample(0:1, size = n, replace = TRUE)),
    catgy = factor(sample(c("foo", "bar", "baz"), size = n, replace = TRUE))
)
D$age[97] <- NA
D$measx[c(501, 842)] <- NA
d <- data.frame(
    group = rep(c("Demographics", "A set of interesting covariates"), each = 4),
    variable = c("sex", "age", "gr2", "gr3",
                 "catgx", "catgy", "measx", "measy"),
    label = c("Sex", "Baseline age", "Foo group", "Bar group",
              "Zero one variable", "Programming names",
              "Higgs density", "Flogiston")
)
X <- D
for(v in names(X)){
    attr(X[[v]], "label") <- d$label[d$variable == v]
}

x <- ucr.base.tab2flextable(
    object = ucR::ucr.base.tab(data = X,
                               group.name = "gr2",
                               include.p = FALSE,
                               include.n = FALSE),
    template = d
)
## ## view results
## library(flextable)
## print(x, preview = "html")
## print(x, preview = "docx")
## print(x, preview = "pdf", latex_engine = "xelatex")

renlund/ucR documentation built on March 25, 2023, 10:10 a.m.