R/clean_raw.R

Defines functions clean_raw

Documented in clean_raw

#' Pass a panelsummary::panelsummary_raw dataframe into kableExtra::kbl() with typical defaults
#'
#' @description
#' `clean_raw` Passes a panelsummary::panelsummary_raw dataframe that has (or has not) been edited further into kableExtra::kbl() with default settings that look publication-ready. This includes changing the column names and fixing the alignment.
#'
#' @param data.frame The data.frame (or tibble) from panelsummary::panelsummary_raw() that has been manipulated.
#' @param colnames An optional vector of strings. The vector of strings should have the same length as the number columns of the table.
#'    * `NULL` (the default): colnames are defaulted to a whitespace, followed by (1), (2), ....etc.
#' @param caption A string. The table caption.
#' @param format A character string. Possible values are latex, html, pipe (Pandoc's pipe tables), simple (Pandoc's simple tables), and rst. The value of this argument will be automatically determined if the function is called within a knitr document. The format value can also be set in the global option knitr.table.format. If format is a function, it must return a character string.
#' @param pretty_num A logical. If TRUE, then numbers over 999 have a comma printing format.
#' @param alignment A character string. By default, it is set to left adjusting the first column, and centering the rest of the columns. For example, a model with three columns will have adjustment of "lcc".
#' @param booktabs T/F for whether to enable the booktabs format for tables. I personally would recommend you turn this on for every latex table except some special cases.
#' @param linesep By default, in booktabs tables, kable insert an extra space every five rows for clear display. If you don't want this feature or if you want to do it in a different pattern, you can consider change this option. The default is c(”, ”, ”, ”, '\\addlinespace'). Also, if you are not using booktabs, but you want a cleaner display, you can change this to ”.
#'
#' @returns A raw data frame that is ready for further manipulation.
#'
#' @examples
#'
#' ## Cleaning a panelsummary_raw dataframe with clean_raw
#'
#' ols_1 <- lm(mpg ~ hp + cyl, data = mtcars)
#'
#' panelsummary_raw(ols_1, ols_1) |> clean_raw()
#'
#'
#'
#'
#' @export
#'
#'
clean_raw <- function(data.frame,
                      alignment = NULL,
                      colnames = NULL,
                      format = NULL,
                      caption = NULL,
                      pretty_num = FALSE,
                      booktabs = FALSE,
                      linesep = if (booktabs) c("", "", "", "", "\\addlinespace") else "\\hline"){
  number_models <- ncol(data.frame)
  if (is.null(alignment)){
    alignment <- create_alignment(number_models)
  }
  if (is.null(colnames)){
    colnames <- create_column_names(number_models)
  }
  if (isTRUE(pretty_num)){
    data.frame <- create_pretty_numbers(data.frame)
    }
  panel_df_cleaned <- kableExtra::kbl(data.frame,
                                      col.names = colnames,
                                      align = alignment,
                                      caption = caption,
                                      format = format,
                                      booktabs = booktabs,
                                      linesep = linesep)
  return(panel_df_cleaned)
}

Try the panelsummary package in your browser

Any scripts or data that you put into this service are public.

panelsummary documentation built on March 20, 2026, 9:07 a.m.