R/my_ft.R

Defines functions my_ft

Documented in my_ft

#' @title formated the dataframe or flextable as my favorite
#' @description formated the dataframe or flextable as my favorite
#' @param df dataframe or flextable
#'
#' @return flextable
#'
#' @examples
#'
#'
#' @rdname my_ft
#' @export

my_ft <- function(df,
                  col.keys = NULL,
                  col.digits = NULL) {
    # create basic flextable
    if (is.data.frame(df)) {
        f.table <- flextable::flextable(df)
    } else {
        f.table <- df
    }


    # set numbers of decimals for numeric variables, if specified
    if (!is.null(col.keys)) {
        for (j in 1:length(col.keys)) {
            f.table = flextable::colformat_num(x = f.table,
                                               col_keys = col.keys[j],
                                               digits = col.digits[j])
        }
    }

    f.table <-
    f.table  %>%
        # set table borders
        flextable::border_outer(part = "all",
                                border = officer::fp_border(color = "black", width = 1)) %>%
        flextable::border_inner_h(part = "all",
                                  border = officer::fp_border(color = "black", width = 1)) %>%
        flextable::border_inner_v(part = "all",
                                  border = officer::fp_border(color = "black", width = 1)) %>%

        # set fonts
        flextable::font(fontname = "Times", part = "all") %>%
        # also set the table's header font as bold
        flextable::bold(part = "header") %>%
        flextable::align(
            i = NULL,
            j = col.keys,
            align = "center",
            part = "all"
        ) %>%
        flextable::autofit(.)
        #flextable::autofit(., add_w = - 0.175, add_h = 0)

        return(f.table)
}
jixing475/manuscriptsJX documentation built on April 20, 2020, 9:55 a.m.