From 925661b1bf873c9849584558ebea170bc5f8bc1c Mon Sep 17 00:00:00 2001 From: Dan Chaltiel <15105152+DanChaltiel@users.noreply.github.com> Date: Fri, 22 Nov 2024 16:04:02 +0100 Subject: [PATCH] import flextable --- DESCRIPTION | 2 +- NAMESPACE | 4 +++- R/ae_table_grade.R | 6 +----- R/ae_table_soc.R | 37 +++++++++++++++++-------------------- R/grstat-package.R | 3 +++ man/ae_table_grade.Rd | 6 +----- man/ae_table_soc.Rd | 3 --- man/reexports.Rd | 3 +++ 8 files changed, 29 insertions(+), 35 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index a337e02..20a1746 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -28,6 +28,7 @@ Depends: Imports: cli, dplyr, + flextable, forcats, fs, ggplot2, @@ -43,7 +44,6 @@ Imports: Suggests: callr, crosstable, - flextable, gtools, patchwork, plotly, diff --git a/NAMESPACE b/NAMESPACE index b25b052..370774a 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -1,6 +1,6 @@ # Generated by roxygen2: do not edit by hand -S3method(flextable::as_flextable,ae_table_soc) +S3method(as_flextable,ae_table_soc) export("%>%") export(ae_plot_grade) export(ae_plot_grade_n) @@ -8,6 +8,7 @@ export(ae_plot_grade_sum) export(ae_plot_soc) export(ae_table_grade) export(ae_table_soc) +export(as_flextable) export(butterfly_plot) export(fct_yesno) export(gr_new_project) @@ -52,6 +53,7 @@ importFrom(dplyr,setdiff) importFrom(dplyr,setequal) importFrom(dplyr,summarise) importFrom(dplyr,transmute) +importFrom(flextable,as_flextable) importFrom(forcats,as_factor) importFrom(forcats,fct_infreq) importFrom(forcats,fct_relevel) diff --git a/R/ae_table_grade.R b/R/ae_table_grade.R index bbac49b..834bcde 100644 --- a/R/ae_table_grade.R +++ b/R/ae_table_grade.R @@ -25,8 +25,6 @@ #' tm = grstat_example() #' attach(tm, warn.conflicts=FALSE) #' -#' if(require(flextable)){ -#' #' ae_table_grade(df_ae=ae, df_enrol=enrolres, arm=NULL) %>% #' as_flextable(header_show_n=TRUE) #' @@ -38,10 +36,8 @@ #' ae %>% #' filter(sae=="Yes") %>% #' ae_table_grade(df_enrol=enrolres, arm="ARM") %>% -#' dplyr::mutate_all(~stringr::str_replace(.x, "AE", "SAE")) %>% +#' mutate_all(~stringr::str_replace(.x, "AE", "SAE")) %>% #' as_flextable(header_show_n=TRUE) -#' -#' } ae_table_grade = function( df_ae, ..., df_enrol, variant=c("max", "sup", "eq"), diff --git a/R/ae_table_soc.R b/R/ae_table_soc.R index b9b650d..3f9dfdb 100644 --- a/R/ae_table_soc.R +++ b/R/ae_table_soc.R @@ -48,8 +48,6 @@ #' #sub population #' ae_table_soc(df_ae=ae, df_enrol=head(enrolres, 10), arm="arm") #' -#' if (require("flextable")) { -#' #' #the resulting flextable can be customized using the flextable package #' ae_table_soc(ae, df_enrol=enrolres, total=FALSE) %>% #' as_flextable() %>% @@ -60,7 +58,6 @@ #' ae_table_soc(ae, df_enrol=enrolres, term=NULL, arm=NULL) %>% #' as_flextable() %>% #' highlight(i=~soc=="Hepatobiliary disorders", j="all_patients_Tot") -#' } ae_table_soc = function( df_ae, ..., df_enrol, variant=c("max", "sup", "eq"), @@ -171,6 +168,7 @@ ae_table_soc = function( #' @return a formatted flextable #' @rdname ae_table_soc #' @exportS3Method flextable::as_flextable +#' @export #' #' @importFrom dplyr case_match lag lead transmute #' @importFrom purrr map map_int @@ -181,7 +179,6 @@ ae_table_soc = function( as_flextable.ae_table_soc = function(x, arm_colors=c("#f2dcdb", "#dbe5f1", "#ebf1dd", "#e5e0ec"), padding_v = NULL){ - check_installed("flextable") if (missing(padding_v)) padding_v = getOption("crosstable_padding_v", padding_v) table_ae_header = attr(x, "header") if(FALSE){ @@ -221,31 +218,31 @@ as_flextable.ae_table_soc = function(x, which() %>% unname() %>% c(ncol(x)) rtn = x %>% - flextable::flextable() %>% - flextable::set_header_df(mapping=header_df) %>% - # flextable::hline_top(part="header") %>% - flextable::hline_bottom(part="header") %>% - flextable::merge_h(part="header") %>% - # flextable::set_header_labels(values=header_labels) %>% - # flextable::add_header_row(values=c(" ", table_ae_header), colwidths = colwidths) %>% - flextable::align(i=1, part="header", align="center") %>% - flextable::align(j=seq(col1), part="all", align="right") %>% - flextable::padding(padding.top=0, padding.bottom=0) %>% - flextable::set_table_properties(layout="autofit") %>% - flextable::fontsize(size=8, part="all") %>% - flextable::bold(part="header") + flextable() %>% + set_header_df(mapping=header_df) %>% + # hline_top(part="header") %>% + hline_bottom(part="header") %>% + merge_h(part="header") %>% + # set_header_labels(values=header_labels) %>% + # add_header_row(values=c(" ", table_ae_header), colwidths = colwidths) %>% + align(i=1, part="header", align="center") %>% + align(j=seq(col1), part="all", align="right") %>% + padding(padding.top=0, padding.bottom=0) %>% + set_table_properties(layout="autofit") %>% + fontsize(size=8, part="all") %>% + bold(part="header") if (length(padding_v) >= 1) { - rtn = flextable::padding(rtn, padding.top=padding_v[1], padding.bottom=padding_v[1], part="body") + rtn = padding(rtn, padding.top=padding_v[1], padding.bottom=padding_v[1], part="body") } if (length(padding_v) == 2) { - rtn = flextable::padding(rtn, padding.top=padding_v[2], padding.bottom=padding_v[2], part="header") + rtn = padding(rtn, padding.top=padding_v[2], padding.bottom=padding_v[2], part="header") } # a = cumsum(colwidths)[-1] a = sep_cols for(i in seq_along(a)){ from = lag(a, default=col1)[i] + 1 to = a[i] - rtn = rtn %>% flextable::bg(j=seq(from, to), bg = arm_colors[i], part="all") + rtn = rtn %>% bg(j=seq(from, to), bg = arm_colors[i], part="all") } rtn diff --git a/R/grstat-package.R b/R/grstat-package.R index 61f1f45..6661ecc 100644 --- a/R/grstat-package.R +++ b/R/grstat-package.R @@ -16,6 +16,9 @@ dplyr::`%>%` #' @export tibble::tibble +#' @importFrom flextable as_flextable +#' @export +flextable::as_flextable # Global settings ----------------------------------------------------------------------------- diff --git a/man/ae_table_grade.Rd b/man/ae_table_grade.Rd index d581eec..c87cbc0 100644 --- a/man/ae_table_grade.Rd +++ b/man/ae_table_grade.Rd @@ -45,8 +45,6 @@ Summary tables for AE tm = grstat_example() attach(tm, warn.conflicts=FALSE) -if(require(flextable)){ - ae_table_grade(df_ae=ae, df_enrol=enrolres, arm=NULL) \%>\% as_flextable(header_show_n=TRUE) @@ -58,10 +56,8 @@ library(dplyr) ae \%>\% filter(sae=="Yes") \%>\% ae_table_grade(df_enrol=enrolres, arm="ARM") \%>\% - dplyr::mutate_all(~stringr::str_replace(.x, "AE", "SAE")) \%>\% + mutate_all(~stringr::str_replace(.x, "AE", "SAE")) \%>\% as_flextable(header_show_n=TRUE) - -} } \seealso{ \code{\link[=ae_table_grade]{ae_table_grade()}}, \code{\link[=ae_table_soc]{ae_table_soc()}}, \code{\link[=ae_plot_grade]{ae_plot_grade()}}, \code{\link[=ae_plot_grade_sum]{ae_plot_grade_sum()}}, \code{\link[=butterfly_plot]{butterfly_plot()}} diff --git a/man/ae_table_soc.Rd b/man/ae_table_soc.Rd index bcbb1da..6abf8c3 100644 --- a/man/ae_table_soc.Rd +++ b/man/ae_table_soc.Rd @@ -82,8 +82,6 @@ ae_table_soc(df_ae=ae, df_enrol=enrolres, arm="arm") #sub population ae_table_soc(df_ae=ae, df_enrol=head(enrolres, 10), arm="arm") -if (require("flextable")) { - #the resulting flextable can be customized using the flextable package ae_table_soc(ae, df_enrol=enrolres, total=FALSE) \%>\% as_flextable() \%>\% @@ -95,7 +93,6 @@ ae_table_soc(ae, df_enrol=enrolres, term=NULL, arm=NULL) \%>\% as_flextable() \%>\% highlight(i=~soc=="Hepatobiliary disorders", j="all_patients_Tot") } -} \seealso{ \code{\link[=ae_table_grade]{ae_table_grade()}}, \code{\link[=ae_table_soc]{ae_table_soc()}}, \code{\link[=ae_plot_grade]{ae_plot_grade()}}, \code{\link[=ae_plot_grade_sum]{ae_plot_grade_sum()}}, \code{\link[=butterfly_plot]{butterfly_plot()}} } diff --git a/man/reexports.Rd b/man/reexports.Rd index e31dda0..f0281b4 100644 --- a/man/reexports.Rd +++ b/man/reexports.Rd @@ -5,6 +5,7 @@ \alias{reexports} \alias{\%>\%} \alias{tibble} +\alias{as_flextable} \title{Objects exported from other packages} \keyword{internal} \description{ @@ -14,6 +15,8 @@ below to see their documentation. \describe{ \item{dplyr}{\code{\link[dplyr:reexports]{\%>\%}}} + \item{flextable}{\code{\link[flextable]{as_flextable}}} + \item{tibble}{\code{\link[tibble]{tibble}}} }}