knitr::opts_chunk$set( collapse = TRUE, comment = "#>" )
library(mRclwhip)
The idea behind mRclwhip::polish()
is to create a more 'complete product' than what is returned
from broom::tidy()
, for example.
The function can either return a flextable::flextable()
object if the argument
.flextable = TRUE
.
Or, if the argument .flextable = FALSE
, then a list with two objects is returned where the first
object in the list is a dataframe of the polished results that can be further manipulated.
The second object in the list is a vector of row numbers that can then be passed into
flextable::padding()
(after passing the dataframe into mRclwhip::format_flextable()
of course)
to pad the rows with factor levels.
The example at the end will provide further clarification.
coxph()
mockstudy <- arsenal::mockstudy coxph_res <- survival::coxph(survival::Surv(fu.time, fu.stat) ~ race + sex + bmi, data = mockstudy) Hmisc::label(mockstudy$sex) <- "Gender" coxph_res %>% polish( .header1 = list(values = c("", "HR (95% CI)")), exponentiate = T )
lm()
mtcars <- mtcars %>% dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character) mtcars <- Hmisc::upData( mtcars, labels = c(mpg = "MPG", hp = "HP", gear = "# of gears", am = "Automatic", wt = "Weight", carb = "Carb" ), print = F ) lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars) lm_res %>% polish( .header1 = list(values = c("", "Estimate (95% CI)")) )
lm()
with labels argument used correctlymtcars <- mtcars %>% dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character) labs <- c(hp = "HP", gear = "# of gears", am = "Automatic", wt = "Weight", carb = "Carb" ) lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars) lm_res %>% polish( .header1 = list(values = c("", "Estimate (95% CI)")), .labels = labs )
lm()
with labels argument used incorrectlymtcars <- mtcars %>% dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character) labs <- c(mpg = "MPG", hp = "HP", gear = "# of gears", am = "Automatic", wt = "Weight", carb = "Carb" ) lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars) lm_res %>% polish( .header1 = list(values = c("", "Estimate (95% CI)")), .labels = labs )
.flextable = FALSE
where a list with a dataframe and a vector of row numbers is returnedmtcars <- mtcars %>% dplyr::mutate_at(dplyr::vars(gear, am, carb), as.character) mtcars <- Hmisc::upData( mtcars, labels = c(mpg = "MPG", hp = "HP", gear = "# of gears", am = "Automatic", wt = "Weight", carb = "Carb" ), print = F ) lm_res <- lm(mpg ~ hp + gear + am + wt + carb, data = mtcars) res <- lm_res %>% polish( .flextable = FALSE ) res
Returning the dataframe allows further manipulation if desired. The row numbers returned for the
rows with factor levels can then be used in flextable::padding()
to add padding
res[[1]] %>% mRclwhip::format_flextable(header1 = list(values = c("", "Estimate (95% CI)"))) %>% flextable::padding(i = res[[2]], j = 1, padding.left = 25)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.