#' @title colnames merge to header
#' @description this function is use to merge the dataframe colname the same element as head. first, i seprate the colnames with sep as dataframe typology. them I use flextable function to merge the same element.
#' @param df dataframe
#' @param sep the separator
#'
#' @return flextable object
#'
#' @examples
#' head(iris) %>%
#' ft_merge_colnames2header(., sep = "\\.")
#' @importFrom magrittr %>%
#'
#' @rdname ft_merge_colnames2header
#' @export
#'
ft_merge_colnames2header <- function(df, sep = "\\.") {
typology <-
data.frame(col_keys = colnames(df),
stringsAsFactors = FALSE ) %>%
dplyr::mutate(to_sep = col_keys) %>%
tidyr::separate(to_sep, into=c("top","second"), sep = sep) %>%
dplyr::mutate(second = dplyr::if_else(is.na(second), top, second))
flextable::flextable(df) %>%
flextable::set_header_df(mapping = typology, key = "col_keys" ) %>%
flextable::merge_h(part = "header") %>%
flextable::merge_v(part = "header") %>%
flextable::theme_booktabs() %>%
flextable::autofit() %>%
flextable::fix_border_issues() %>%
flextable::align(align = "center", part = "header") %>%
flextable::align(align = "center", part = "body")
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.