Skip to content

Commit

Permalink
gtsummary v2.0 preparation (#110)
Browse files Browse the repository at this point in the history
* gtsummary v2.0 preparation

* Update R-CMD-check.yaml

* Update DESCRIPTION

* doc update

* Update DESCRIPTION

* Update DESCRIPTION

* Update NEWS.md

* Adding new functions to _pkgdown.yml.

* updates

---------

Co-authored-by: Teng Fei <[email protected]>
  • Loading branch information
ddsjoberg and tengfei-emory authored Aug 3, 2024
1 parent 543851c commit 2ba26f1
Show file tree
Hide file tree
Showing 14 changed files with 417 additions and 9 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/R-CMD-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- {os: ubuntu-latest, r: 'devel', http-user-agent: 'release'}
- {os: ubuntu-latest, r: 'release'}
- {os: ubuntu-latest, r: 'oldrel-1'}
- {os: ubuntu-latest, r: 'oldrel-2'}
- {os: ubuntu-latest, r: 'oldrel-3'}
- {os: ubuntu-latest, r: 'oldrel-4'}

env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
Expand Down
13 changes: 8 additions & 5 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: tidycmprsk
Title: Competing Risks Estimation
Version: 1.0.0.9000
Version: 1.0.0.9001
Authors@R: c(
person(c("Daniel", "D."), "Sjoberg", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0003-0862-2018")),
Expand All @@ -16,22 +16,25 @@ License: AGPL (>= 3)
URL: https://mskcc-epi-bio.github.io/tidycmprsk/, https://github.com/MSKCC-Epi-Bio/tidycmprsk
BugReports: https://github.com/MSKCC-Epi-Bio/tidycmprsk/issues
Depends:
R (>= 3.4)
R (>= 4.2)
Imports:
broom (>= 1.0.1),
cli (>= 3.1.0),
cmprsk (>= 2.2.10),
dplyr (>= 1.0.7),
ggplot2 (>= 3.3.5),
gtsummary (>= 1.7.2),
gtsummary (>= 2.0.0),
hardhat (>= 1.3.0),
purrr (>= 0.3.4),
rlang (>= 1.0.0),
stringr (>= 1.4.0),
survival,
tibble (>= 3.1.6),
tidyr (>= 1.1.4)
Suggests:
Suggests:
aod,
broom.helpers (>= 1.15.0),
cardx (>= 0.2.0),
covr (>= 3.5.1),
ggsurvfit,
knitr (>= 1.36),
Expand All @@ -42,4 +45,4 @@ Encoding: UTF-8
Language: en-US
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.2
6 changes: 6 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ S3method(cuminc,default)
S3method(cuminc,formula)
S3method(glance,tidycrr)
S3method(glance,tidycuminc)
S3method(global_pvalue_fun,tidycrr)
S3method(inline_text,tbl_cuminc)
S3method(model.frame,tidycrr)
S3method(model.frame,tidycuminc)
Expand All @@ -21,6 +22,7 @@ S3method(predict,tidycrr)
S3method(print,tidycrr)
S3method(print,tidycuminc)
S3method(tbl_cuminc,tidycuminc)
S3method(tbl_regression,tidycrr)
S3method(terms,tidycrr)
S3method(tidy,tidycrr)
S3method(tidy,tidycuminc)
Expand All @@ -35,8 +37,10 @@ export(autoplot)
export(crr)
export(cuminc)
export(glance)
export(global_pvalue_fun)
export(inline_text)
export(tbl_cuminc)
export(tbl_regression)
export(tidy)
importFrom(broom,augment)
importFrom(broom,glance)
Expand All @@ -58,7 +62,9 @@ importFrom(ggplot2,ggplot)
importFrom(gtsummary,add_n)
importFrom(gtsummary,add_nevent)
importFrom(gtsummary,add_p)
importFrom(gtsummary,global_pvalue_fun)
importFrom(gtsummary,inline_text)
importFrom(gtsummary,tbl_regression)
importFrom(purrr,"%||%")
importFrom(rlang,":=")
importFrom(rlang,.data)
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# tidycmprsk (development version)

* Updating package to account for changes in {gtsummary} v2.0. To use `gtsummary::tbl_regression()` with `tidycmprsk::crr()` models, the {tidycmprsk} package must be loaded (to access the S3 methods below). In the next release of gtsummary, tidycmprsk will no longer be a dependency.
- Added {broom.helpers}, {cardx}, and {aod} to Suggests field.
- Added `tbl_regression.tidycrr()` method.
- Added `global_pvalue_fun.tidycrr()` method, which changes the default calculation in `gtsummary::add_global_p(anova_fun)` to use the Wald test.

# tidycmprsk 1.0.0

* Corrected a previous regression bug and `cuminc()` can again handle models with no observed censoring. (#89)
Expand Down
41 changes: 41 additions & 0 deletions R/gtsummary_s3_methods.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#' gtsummary methods
#'
#' @description
#' These functions are S3 methods for working with [`tidycmprsk::crr()`] model
#' results.
#'
#' - `tbl_regression.tidycrr()`: This function sets the tidycmprsk tidier for `crr()` models.
#' - `global_pvalue_fun.tidycrr()`: This function ensures that `gtsummary::add_global_p(anova_fun)` uses
#' the Wald test by default (instead of `car::Anova()`, which does not support this model type).
#' The Wald test is executed with `cardx::ard_aod_wald_test()`, which wraps `aod::wald.test()`.
#'
#' @param x (`tidycrr`)\cr
#' `tidycmprsk::crr()` regression object
#' @param tidy_fun (`function`)\cr
#' Tidier function for the model. Default is `tidycmprsk::tidy()`.
#' @param type not used
#' @inheritParams gtsummary::tbl_regression
#'
#' @name gtsummary_s3_methods
#' @return gtsummary table or data frame of results
#'
#' @examples
#' crr(Surv(ttdeath, death_cr) ~ age + grade, trial) |>
#' gtsummary::tbl_regression() |>
#' gtsummary::add_global_p() |>
#' gtsummary::as_gt()
NULL

#' @rdname gtsummary_s3_methods
#' @export
tbl_regression.tidycrr <- function(x, tidy_fun = tidycmprsk::tidy, ...) {
asNamespace("gtsummary")[["tbl_regression.default"]](x = x, tidy_fun = tidy_fun, ...)
}

#' @rdname gtsummary_s3_methods
#' @export
global_pvalue_fun.tidycrr <- function(x, type, ...) {
check_pkg_installed("cardx", reference_pkg = "tidycmprisk")

cardx::ard_aod_wald_test(x, ...)
}
191 changes: 191 additions & 0 deletions R/import-standalone-check_pkg_installed.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,191 @@
# Standalone file: do not edit by hand
# Source: <https://github.com/ddsjoberg/standalone/blob/main/R/standalone-check_pkg_installed.R>
# ----------------------------------------------------------------------
#
# ---
# repo: ddsjoberg/standalone
# file: standalone-check_pkg_installed.R
# last-updated: 2024-04-19
# license: https://unlicense.org
# dependencies: standalone-cli_call_env.R
# imports: [rlang, dplyr, tidyr]
# ---
#
# This file provides functions to check package installation.
#
# ## Changelog
# nocov start
# styler: off

#' Check Package Installation
#'
#' @description
#' - `check_pkg_installed()`: checks whether a package is installed and
#' returns an error if not available, or interactively asks user to install
#' missing dependency. If a package search is provided,
#' the function will check whether a minimum version of a package is required and installed.
#'
#' - `is_pkg_installed()`: checks whether a package is installed and
#' returns `TRUE` or `FALSE` depending on availability. If a package search is provided,
#' the function will check whether a minimum version of a package is required and installed.
#'
#' - `get_pkg_dependencies()` returns a tibble with all
#' dependencies of a specific package.
#'
#' - `get_min_version_required()` will return, if any, the minimum version
#' of `pkg` required by `reference_pkg`.
#'
#' @param pkg (`character`)\cr
#' vector of package names to check.
#' @param call (`environment`)\cr
#' frame for error messaging. Default is [get_cli_abort_call()].
#' @param reference_pkg (`string`)\cr
#' name of the package the function will search for a minimum required version from.
#' @param lib.loc (`path`)\cr
#' location of `R` library trees to search through, see [utils::packageDescription()].
#'
#' @return `is_pkg_installed()` and `check_pkg_installed()` returns a logical or error,
#' `get_min_version_required()` returns a data frame with the minimum version required,
#' `get_pkg_dependencies()` returns a tibble.
#'
#' @examples
#' check_pkg_installed("dplyr")
#'
#' is_pkg_installed("dplyr")
#'
#' get_pkg_dependencies()
#'
#' get_min_version_required("dplyr")
#'
#' @name check_pkg_installed
#' @noRd
NULL

#' @inheritParams check_pkg_installed
#' @keywords internal
#' @noRd
check_pkg_installed <- function(pkg,
reference_pkg = "cards",
call = get_cli_abort_call()) {
# get min version data -------------------------------------------------------
df_pkg_min_version <-
get_min_version_required(pkg = pkg, reference_pkg = reference_pkg, call = call)

# prompt user to install package ---------------------------------------------
rlang::check_installed(
pkg = df_pkg_min_version$pkg,
version = df_pkg_min_version$version,
compare = df_pkg_min_version$compare
) |>
# this can be removed after this issue is resolved https://github.com/r-lib/rlang/issues/1694
suppressWarnings()
}

#' @inheritParams check_pkg_installed
#' @keywords internal
#' @noRd
is_pkg_installed <- function(pkg,
reference_pkg = "cards",
call = get_cli_abort_call()) {
# get min version data -------------------------------------------------------
df_pkg_min_version <-
get_min_version_required(pkg = pkg, reference_pkg = reference_pkg, call = call)

# check installation TRUE/FALSE ----------------------------------------------
rlang::is_installed(
pkg = df_pkg_min_version$pkg,
version = df_pkg_min_version$version,
compare = df_pkg_min_version$compare
) |>
# this can be removed after this issue is resolved https://github.com/r-lib/rlang/issues/1694
suppressWarnings()
}

#' @inheritParams check_pkg_installed
#' @keywords internal
#' @noRd
get_pkg_dependencies <- function(reference_pkg = "cards", lib.loc = NULL, call = get_cli_abort_call()) {
if (rlang::is_empty(reference_pkg)) {
return(.empty_pkg_deps_df())
}

description <- utils::packageDescription(reference_pkg, lib.loc = lib.loc) |> suppressWarnings()
if (identical(description, NA)) {
return(.empty_pkg_deps_df())
}
description |>
unclass() |>
dplyr::as_tibble() |>
dplyr::select(
dplyr::any_of(c(
"Package", "Version", "Imports", "Depends",
"Suggests", "Enhances", "LinkingTo"
))
) |>
dplyr::rename(
reference_pkg = "Package",
reference_pkg_version = "Version"
) |>
tidyr::pivot_longer(
-dplyr::all_of(c("reference_pkg", "reference_pkg_version")),
values_to = "pkg",
names_to = "dependency_type",
) |>
tidyr::separate_rows("pkg", sep = ",") |>
dplyr::mutate(
pkg = trimws(
x = gsub(x = .data$pkg, pattern = "\\s+", replacement = " "),
which = "both", whitespace = "[ \t\r\n]"
)
) |>
dplyr::filter(!is.na(.data$pkg)) |>
tidyr::separate(
.data$pkg,
into = c("pkg", "version"),
sep = " ", extra = "merge", fill = "right"
) |>
dplyr::mutate(
compare = .data$version |> stringr::str_extract(pattern = "[>=<]+"),
version = .data$version |> stringr::str_remove_all(pattern = "[\\(\\) >=<]")
)
}

.empty_pkg_deps_df <- function() {
dplyr::tibble(
reference_pkg = character(0L), reference_pkg_version = character(0L),
dependency_type = character(0L), pkg = character(0L),
version = character(0L), compare = character(0L)
)
}

#' @inheritParams check_pkg_installed
#' @keywords internal
#' @noRd
get_min_version_required <- function(pkg, reference_pkg = "cards",
lib.loc = NULL, call = get_cli_abort_call()) {
# if no package reference, return a df with just the pkg names
if (rlang::is_empty(reference_pkg)) {
return(
.empty_pkg_deps_df() |>
dplyr::full_join(
dplyr::tibble(pkg = pkg),
by = "pkg"
)
)
}

# get the package_ref deps and subset on requested pkgs, also supplement df with pkgs
# that may not be proper deps of the reference package (these pkgs don't have min versions)
res <-
get_pkg_dependencies(reference_pkg, lib.loc = lib.loc) |>
dplyr::filter(.data$pkg %in% .env$pkg) |>
dplyr::full_join(
dplyr::tibble(pkg = pkg),
by = "pkg"
)

res
}

# nocov end
# styler: on
53 changes: 53 additions & 0 deletions R/import-standalone-cli_call_env.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Standalone file: do not edit by hand
# Source: <https://github.com/ddsjoberg/standalone/blob/main/R/standalone-cli_call_env.R>
# ----------------------------------------------------------------------
#
# ---
# repo: ddsjoberg/standalone
# file: standalone-cli_call_env.R
# last-updated: 2024-04-10
# license: https://unlicense.org
# imports: [rlang, cli]
# ---
#
# This file provides functions to set and access the call environment to use in cli::cli_abort() in check functions.
#
# ## Changelog
# nocov start
# styler: off

#' Set Call Environment for [cli::cli_abort()]
#'
#' Set a call environment to be used as the `call` parameter in [cli::cli_abort()] for package checks. This function
#' is used to ensure that the correct user-facing function is reported for errors generated by internal checks that
#' use [cli::cli_abort()].
#'
#' @param env (`enviroment`)\cr
#' call environment used as the `call` parameter in [cli::cli_abort()] for package checks
#'
#' @seealso [get_cli_abort_call()]
#'
#' @keywords internal
#' @noRd
set_cli_abort_call <- function(env = rlang::caller_env()) {
if (getOption("cli_abort_call") |> is.null()) {
options(cli_abort_call = env)
set_call <- as.call(list(function() options(cli_abort_call = NULL)))
do.call(on.exit, list(expr = set_call, add = TRUE, after = FALSE), envir = env)
}
invisible()
}

#' Get Call Environment for [cli::cli_abort()]
#'
#' @inheritParams set_cli_abort_call
#' @seealso [set_cli_abort_call()]
#'
#' @keywords internal
#' @noRd
get_cli_abort_call <- function() {
getOption("cli_abort_call", default = parent.frame())
}

# nocov end
# styler: on
Loading

0 comments on commit 2ba26f1

Please sign in to comment.