Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

addresses #578 - use dplyr::select insstead of select_ #579

Merged
merged 11 commits into from
Oct 29, 2024
6 changes: 3 additions & 3 deletions R/ggcoxdiagnostics.R
Original file line number Diff line number Diff line change
Expand Up @@ -99,9 +99,9 @@ ggcoxdiagnostics <- function (fit,
else col_names <- names(stats::coef(fit))
colnames(res) <- col_names
res$xval <- xval
data2plot <- tidyr::gather_(res,
key_col = "covariate", value_col = "res",
gather_col = col_names)
data2plot <- tidyr::gather(res,
key = "covariate", value = "res",
col_names)

gplot <- ggplot(aes(xval, res), data = data2plot) +
geom_point(col = point.col, shape = point.shape,
Expand Down
11 changes: 5 additions & 6 deletions R/ggsurvplot_combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ ggsurvplot_combine <- function(fit, data,
all.levels <- c(all.levels, .levels(ss$strata))
# convert strata into character before binding
# avoid this warning: Unequal factor levels: coercing to character
grouped.d$survsummary <- map(grouped.d$survsummary,
grouped.d$survsummary <- purrr::map(grouped.d$survsummary,
function(x){
x$strata <- as.character(x$strata)
x
Expand Down Expand Up @@ -161,9 +161,8 @@ ggsurvplot_combine <- function(fit, data,
survtable$strata <- paste(fitname, "::", survtable$strata, sep = "") %>%
factor(levels = strata.levels)
survtable %>%
dplyr::select_( .dots = c("strata", "time", "n.risk", "pct.risk",
"n.event", "cum.n.event", "n.censor",
"cum.n.censor", "strata_size"))
dplyr::select(strata, time, n.risk, pct.risk, n.event, cum.n.event,
n.censor, cum.n.censor, strata_size)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To avoid R cmd check warning, quotes are needed around variables. The code should look like this:

dplyr::select( dplyr::all_of(c("strata", "time", "n.risk", "pct.risk",
                          "n.event", "cum.n.event", "n.censor",
                          "cum.n.censor", "strata_size")))

}

grouped.d <- grouped.d %>%
Expand Down Expand Up @@ -199,8 +198,8 @@ ggsurvplot_combine <- function(fit, data,
#::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

if(keep.data){
res$data.survplot <- tibble::as.tibble(all.survsummary)
res$data.survtable <- tibble::as.tibble(all.survtable)
res$data.survplot <- tibble::as_tibble(all.survsummary)
res$data.survtable <- tibble::as_tibble(all.survtable)
}


Expand Down
4 changes: 2 additions & 2 deletions R/ggsurvplot_facet.R
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,10 @@ ggsurvplot_facet <- function(fit, data, facet.by,
pvalue <- surv_pvalue(grouped.d$fit, grouped.d$data, pval.coord = pval.coord,
pval.method.coord = pval.method.coord,...) %>%
dplyr::bind_rows() %>%
tibble::as.tibble()
tibble::as_tibble()
# Select the grouping variable columns and cbind the corresponding pvalue
pvals.df <- grouped.d %>%
dplyr::select_( .dots = facet.by) %>%
dplyr::select(!!!syms(facet.by)) %>%
dplyr::bind_cols(pvalue)
pval.x <- pval.y <- pval.txt <- method.x <- method.y <- method <- NULL
p <- p +
Expand Down
2 changes: 1 addition & 1 deletion R/surv_median.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ surv_median <- function(fit, combine = FALSE){
.table$strata <- rownames(.table)

.table <- .table %>%
dplyr::select_(.dots = c("strata", "median", "`0.95LCL`", "`0.95UCL`"))
dplyr::select(strata, median, `0.95LCL`, `0.95UCL`)
Copy link
Owner

@kassambara kassambara Feb 14, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As above (to avoid R check warnings), the code should be:

dplyr::select(dplyr::all_of(c("strata",  "median", "0.95LCL", "0.95UCL")))

colnames(.table) <- c("strata", "median", "lower", "upper")
rownames(.table) <- NULL
.table
Expand Down
2 changes: 2 additions & 0 deletions tests/testthat/setup-load_data.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
library(survival)
data('cancer')
16 changes: 16 additions & 0 deletions tests/testthat/test-ggcoxdiagnostics.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test_that('ggcoxdiagnostics creates plot with all the observations', {
cph <- coxph(Surv(futime, fustat) ~ rx + age, data=ovarian)
p <- ggcoxdiagnostics(cph, type="deviance")
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[1]]), nrow(ovarian))
})

test_that('ggcoxdiagnostics with second type two rows for each observed event*term', {
cph <- coxph(Surv(futime, fustat) ~ rx + age, data=ovarian)
qty_terms <- length(attr(terms(cph$formula), "term.labels"))
qty_events <- sum(ovarian$fustat==1)
p <- ggcoxdiagnostics(cph, type="schoenfeld")
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[1]]), qty_terms*qty_events)
})

21 changes: 21 additions & 0 deletions tests/testthat/test-ggsurvplot_combine.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
start_time <- 250
fit1 <- survfit(Surv(time, status) ~ sex, data = lung)
fit2 <- survfit(Surv(time, status) ~ sex, data = lung, start.time = start_time)

test_that("survplot_combine plots successfully into 4 lines; second 2 fits have only (0,1) before start_time", {
p <- ggsurvplot_combine(list(
original=fit1, conditional=fit2
), data = lung)
.build <- ggplot_build(p$plot)
.build_data <- .build$data[[1]]
expect_equal(length(unique(.build_data[['group']])), 4)
expect_lt(nrow(.build_data[(.build_data[['group']] >= 3) &
(.build_data[['x']] < start_time), ]), 3)
})

test_that("survplot_combine includes dataframes when keep.data==TRUE", {
p <- ggsurvplot_combine(list(
original=fit1, conditional=fit2
), data = lung, keep.data = TRUE)
expect_equal(length(names(p)), 3)
})
21 changes: 21 additions & 0 deletions tests/testthat/test-ggsurvplot_facet.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
test_that("ggsurvplot_facet creates the correct quanitty of subplots", {
fit <- survfit(Surv(time, status) ~ sex, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='disease')
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[2]]),
length(unique(kidney[['disease']])))

fit <- survfit(Surv(time, status) ~ disease, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='sex')
.build <- ggplot_build(p)
expect_equal(nrow(.build$data[[2]]),
length(unique(kidney[['sex']])))
})

test_that("ggsurvplot_facet calculates pvalue for each facet", {
fit <- survfit(Surv(time, status) ~ sex, data=kidney)
p <- ggsurvplot_facet(fit, kidney, facet.by='disease', pval = TRUE)
.build <- ggplot_build(p)
expect_equal(nrow(.build$plot$layer[[4]][['data']]),
length(unique(kidney[['disease']])))
})
6 changes: 6 additions & 0 deletions tests/testthat/test-surv_median.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test_that("surv_median calculates medians", {
df_medians <- surv_median(survfit(Surv(time, status) ~ sex,
data=lung))
expect_equal(df_medians[['median']], c(270,426))
expect_lt(df_medians[['lower']][2], 426)
})