Skip to content

Commit

Permalink
kassambara#578 update tests and replace a few select and as.tibbles
Browse files Browse the repository at this point in the history
  • Loading branch information
tbaer-c7ks7s committed Apr 5, 2022
1 parent c4a3de1 commit 359c0c0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 12 deletions.
6 changes: 3 additions & 3 deletions R/ggsurvplot_combine.R
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ggsurvplot_combine <- function(fit, data,
factor(levels = strata.levels)
survtable %>%
dplyr::select(strata, time, n.risk, pct.risk, n.event, cum.n.event,
n.censor, cum.n.censor, strata_size)))
n.censor, cum.n.censor, strata_size)
}

grouped.d <- grouped.d %>%
Expand Down Expand Up @@ -198,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
17 changes: 10 additions & 7 deletions tests/testthat/test-ggsurvplot_combine.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
context("test-ggsurvplot_combine")

library(dplyr)
library(survival)
data("lung")
start_time <- 250
fit1 <- survfit(Surv(time, status) ~ sex, data = lung)
fit2 <- survfit(Surv(time, status) ~ sex, data = lung, start.time=start_time)
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(
Expand All @@ -14,5 +9,13 @@ test_that("survplot_combine plots successfully into 4 lines; second 2 fits have
.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)
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']])))
})

0 comments on commit 359c0c0

Please sign in to comment.