Skip to content

Commit

Permalink
Merge pull request #1 from FrancisCrickInstitute/classes
Browse files Browse the repository at this point in the history
remove S3 class cg_skl_tbl
  • Loading branch information
EdjCarr authored Jul 26, 2024
2 parents 4b49a14 + 488fb9b commit e83cc40
Show file tree
Hide file tree
Showing 20 changed files with 294 additions and 301 deletions.
4 changes: 3 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
Imports:
cli,
DBI,
dbplyr,
dplyr,
Expand All @@ -29,7 +30,8 @@ Imports:
lobstr,
lubridate,
magrittr,
patchwork,
patchwork,
pillar,
purrr,
rlang (>= 0.4.11),
RSQLite,
Expand Down
9 changes: 6 additions & 3 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ S3method(glimpse,grouped_cg_df)
S3method(glimpse_experiment_data,cg_tbl)
S3method(glimpse_metadata,cg_tbl)
S3method(group_by,cg_tbl)
S3method(print,cg_skl_tbl)
S3method(print,cg_tbl)
S3method(print,grouped_cg_df)
S3method(summary,cg_tbl)
S3method(summary,grouped_cg_df)
S3method(tbl_format_footer,cg_tbl)
S3method(tbl_sum,cg_tbl)
S3method(ungroup,grouped_cg_df)
export("%>%")
export(":=")
Expand Down Expand Up @@ -59,6 +60,8 @@ importFrom(dplyr,group_by)
importFrom(dplyr,group_data)
importFrom(dplyr,ungroup)
importFrom(magrittr,"%>%")
importFrom(pillar,tbl_format_footer)
importFrom(pillar,tbl_sum)
importFrom(rlang,":=")
importFrom(rlang,.data)
importFrom(rlang,as_label)
Expand Down
22 changes: 22 additions & 0 deletions R/check_metadata.R
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,27 @@ check_metadata <- function(x, cg_skeleton = NULL) {
)
}

### metadata should not have duplicated ids ###
if (!is.null(ids_column_name)) {
stopifnot(
{
n <- NULL # suppress global binding note
"Invalid metadata: IDs are repeated" =
xx <- x %>%
dplyr::group_by(
dplyr::across(
dplyr::all_of(
c(ids_column_name)
)
)
) %>%
dplyr::tally() %>%
dplyr::pull(n)

all(xx < 2) == TRUE
}
)
}

return(TRUE)
}
11 changes: 3 additions & 8 deletions R/new_chronogram.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#' new chronogram
#'
#' @param x a tbl, a daughter of a join between a cg_skl_tbl and
#' @param x a tbl, a daughter of a join between a chronogram skeleton and
#' one (or more) objects coerced to a tbl.
#' @param metadata_cols the column names from the metadata tibble
#' @param ... passed to `new_tibble()`. Adds grouping support.
Expand All @@ -17,8 +17,8 @@ new_chronogram <- function(x,
metadata_cols,
...) {
stopifnot(
"provided object does not inherit cg_skl_tbl class" =
inherits(x, "cg_skl_tbl")
"provided object does not inherit tbl_df class" =
inherits(x, "tbl_df")
)

stopifnot(
Expand All @@ -29,11 +29,6 @@ new_chronogram <- function(x,
## Set first class to cg_tbl ##
class(x) <- c("cg_tbl", class(x))

## remove the cg_skl_tbl class ##
class(x) <- class(x)[
!grepl(class(x), pattern = "cg_skl_tbl")
]

## remove the col_ids from metadata column names ##
col_ids <- attributes(x)$col_ids
cols_metadata <- metadata_cols[
Expand Down
4 changes: 1 addition & 3 deletions R/new_chronogram_skeleton.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#' rlang::enquo(an_unquoted_string)
#' @param ids_col provided as rlang::enquo(an_unquoted_string)
#'
#' @return x a tibble with class cg_skl_tbl
#' @return x a tibble with attributes to identify `calendar_date_col` and `ids_col`.
#'
#' @seealso [chronogram::chronogram_skeleton()]
#'
Expand Down Expand Up @@ -55,7 +55,5 @@ new_chronogram_skeleton <- function(x,

attributes(x) <- attributes_x

class(x) <- c("cg_skl_tbl", class(x))

return(x)
}
168 changes: 52 additions & 116 deletions R/print_chronogram.R
Original file line number Diff line number Diff line change
@@ -1,130 +1,66 @@
#' print chronogram
#' Add a line to tbl header when printing a chronogram, or a grouped chronogram
#'
#' @param x a chronogram object (class tbl_chronogram)
#' @param ... additional arguments passed to tibble print S3 generic
#' @param x a chronogram, or a grouped chronogram
#' (class tbl_chronogram or grouped_cg_df)
#' @param ... passed to [pillar::tbl_sum()]
#'
#' @return print to console
#' @examples
#' \dontrun{
#' ## a 3-person chronogram_skeleton ##
#' small_study <- chronogram_skeleton(
#' col_ids = elig_study_id,
#' ids = c(1, 2, 3),
#' start_date = "01012020",
#' end_date = "10102021",
#' col_calendar_date = calendar_date
#' )
#'
#' ## Create a tibble containing some metadata for our 3 individuals ##
#' small_study_metadata <- tibble::tribble(
#' ~elig_study_id, ~age, ~sex, ~dose_1, ~date_dose_1, ~dose_2, ~date_dose_2,
#' 1, 40, "F", "AZD1222", "05/01/2021", "AZD1222", "05/02/2021",
#' 2, 45, "F", "BNT162b2", "05/01/2021", "BNT162b2", "05/02/2021",
#' 3, 35, "M", "BNT162b2", "10/01/2021", "BNT162b2", "10/03/2021"
#' )
#'
#' ## Set appropriate metadata column classes ##
#' library(dplyr)
#' small_study_metadata <- small_study_metadata %>%
#' mutate(across(c(sex, dose_1, dose_2), ~ as.factor(.x)))
#'
#' small_study_metadata <- small_study_metadata %>%
#' mutate(across(contains("date"), ~ lubridate::dmy(.x)))
#'
#' ## Make a chronogram ##
#' small_study_chronogram <- chronogram(
#' small_study,
#' small_study_metadata
#' )
#'
#' ## print, with default tibble options ##
#' small_study_chronogram
#'
#' ## print, with eg 3 rows ##
#' print(small_study_chronogram, n = 3)
#' }
#' @importFrom pillar tbl_sum
#' @seealso [summary()]
#' @export
print.cg_tbl <- function(x, ...) {
calendar_date <- attributes(x)$col_calendar_date
ids_column_name <- attributes(x)$col_ids
version_number <- attributes(x)$cg_pkg_version
windowed <- attributes(x)$windowed
meta <- attributes(x)$cols_metadata

zz <- NULL # suppress no visible binding warning

over_min <- x %>%
dplyr::group_by(
dplyr::across(
dplyr::all_of(ids_column_name)
)
) %>%
dplyr::summarise(zz = (dplyr::n())) %>%
dplyr::pull(zz) %>%
min(., na.rm = TRUE)
#' @noRd

over_max <- x %>%
dplyr::group_by(
dplyr::across(
dplyr::all_of(ids_column_name)
)
) %>%
dplyr::summarise(zz = (dplyr::n())) %>%
dplyr::pull(zz) %>%
max(., na.rm = TRUE)

cat(
## use paste to prevent cat re-formating dates
paste(
"A chronogram:\n",
"Dates column: ", calendar_date, "\n",
"IDs column: ", ids_column_name, "\n",
"Starts on: ",
min(x %>% dplyr::pull(calendar_date)), "\n",
"Ends on: ",
max(x %>% dplyr::pull(calendar_date)), "\n",
# "Spanning: ",
# max(x %>% dplyr::pull( calendar_date )) -
# min(x %>% dplyr::pull( calendar_date )),
# " days\n",
"Contains: ",
x %>% dplyr::pull(ids_column_name) %>%
factor(.) %>% nlevels(.),
" unique participant IDs\n",
"Windowed: ", windowed, "\n",
"Spanning: ",
over_min,
"-",
over_max, "days [min-max per participant]\n",
"Metadata: ",
paste(meta, collapse = ", "), "\n",
"Size: ",
rlang::parse_bytes(
as.character(
lobstr::obj_size(x)
)
), "\n",
"Pkg_version: ",
version_number, "[used to build this cg]\n\n"
)
)
NextMethod(x, ...)
tbl_sum.cg_tbl <- function(x, ...) {
default_header <- NextMethod()
c(default_header, "A chronogram" = cli::col_blue("try summary()"))
}

#' Add a line to tbl footer when printing a chronogram, or a grouped chronogram
#' @importFrom pillar tbl_format_footer
#' @param ... passed to [pillar::tbl_format_footer()]
#' @export
#' @noRd
tbl_format_footer.cg_tbl <- function(x, setup, ...) {
default_footer <- NextMethod()

meta <- paste(attributes(x)$cols_metadata, collapse = ", ")

extra_info <- paste(
cli::col_yellow("Dates:"),
attributes(x)$col_calendar_date,
" ",
cli::col_yellow(cli::symbol$star),
cli::col_yellow("IDs:"),
attributes(x)$col_ids)

#' @export
print.grouped_cg_df <- function(x, ...) {
groups <- dplyr::group_vars(x)
extra_info_meta <- paste(cli::col_yellow("metadata:"), meta)

cat(
paste(
"A grouped chronogram", "\n",
"Groups: ", paste(groups, collapse = ", "), "\n",
"... now printing chronogram view ...", "\n"
extra_footer <- paste0(
pillar::style_subtle(paste0("# ", cli::col_yellow(cli::symbol$star), " ", (extra_info))),
"\n",
pillar::style_subtle(paste0("# ", cli::col_yellow(cli::symbol$star), " ", (extra_info_meta)))
)
)

NextMethod(x, ...)
c(default_footer, extra_footer)
}


#' This is draft code to fine tune display of the pillars themselves.
#'
#' #' @importFrom pillar ctl_new_pillar
#' #' @importFrom pillar new_pillar_component
#' #' @importFrom pillar new_pillar
#' #' @export
#' ctl_new_pillar.cg_tbl <- function(controller, x, width, ..., title = NULL) {
#' out <- NextMethod()
#'
#' pillar::new_pillar(list(
#' top_rule = pillar::new_pillar_component(list("========"), width = 8),
#' title = out$title,
#' type = out$type,
#' mid_rule = pillar::new_pillar_component(list("--------"), width = 8),
#' data = out$data,
#' bottom_rule = pillar::new_pillar_component(list("========"), width = 8)
#' ))
#' }

61 changes: 0 additions & 61 deletions R/print_chronogram_skeleton.R

This file was deleted.

Loading

0 comments on commit e83cc40

Please sign in to comment.