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

Add na.rm option to remove the strata variable in rows #368

Merged
merged 11 commits into from
May 22, 2022
4 changes: 2 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Type: Package
Package: visR
Title: Clinical Graphs and Tables Adhering to Graphical Principles
Version: 0.2.0.9003
Version: 0.2.0.9004
Authors@R: c(
person(given = "Mark",
family = "Baillie",
Expand Down Expand Up @@ -101,6 +101,6 @@ VignetteBuilder:
biocViews:
Encoding: UTF-8
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.1.2
RoxygenNote: 7.2.0
LazyData: true
Language: en-US
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
### Other Updates
* Stratifying variable names have been removed from legend in `visr.survfit()` figures, and the legend title now describes the stratifying variable(s). (#343)
* `add_risktable` has a rowgutter argument to allow spacing between plot and risktables
* The strata variable is now removed from the body of `tableone()` results. (#254)

* Improved documentation for `visr()` and other generic functions. (#301)

Expand Down
4 changes: 3 additions & 1 deletion R/get_tableone.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ get_tableone.default <- function(data, strata = NULL, overall=TRUE, summary_func
summary_FUN <- match.fun(summary_function)

if(overall & !is.null(strata)){
overall_table1 <- get_tableone(data, strata = NULL, overall = FALSE, summary_function = summary_function)
overall_table1 <- get_tableone(data, strata = NULL, overall = FALSE, summary_function = summary_function) %>%
dplyr::filter(!(variable %in% strata))

combine_dfs <- TRUE
}
else{
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ coverage](https://codecov.io/gh/openpharma/visR/branch/develop/graph/badge.svg)]
[![pkgdown](https://github.com/openpharma/visR/actions/workflows/makedocs.yml/badge.svg)](https://github.com/openpharma/visR/actions/workflows/makedocs.yml)
[![CRAN
status](https://www.r-pkg.org/badges/version/visR)](https://CRAN.R-project.org/package=visR)
<a href=https://github.com/pharmaR/riskmetric><img src=https://img.shields.io/badge/riskmetric-0.54-green></img></a>
<a href=https://github.com/pharmaR/riskmetric><img src=https://img.shields.io/badge/riskmetric-0.53-green></img></a>
<!-- badges: end -->

## Installation
Expand Down
10 changes: 10 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Chisq
Codecov
Covid
DT
FFFFFF
Kaplan
Karnakata
Lifecycle
Expand Down Expand Up @@ -65,19 +66,26 @@ io
kable
linetype
magrittr
md
mskcc
nums
openpharma
pkgdown
psp
pst
purrr
pvalue
px
repo
rgba
rgmrgujyos
riskmetric
risktable
risktables
rowgutter
rtf
sourcenote
sourcenotes
src
statlist
strata's
Expand All @@ -86,9 +94,11 @@ stype
survdiff
survfit
tableone
td
th
tibble
tidycmprsk
uncert
visR's
visualisation
visualisations
Expand Down
4 changes: 2 additions & 2 deletions man/visR-package.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

74 changes: 54 additions & 20 deletions tests/testthat/test-get_tableone.R
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#' T4.3 An error when the `summary_function` is a function not build for it
#' T4.4 An error when the `summary_function` is `summarize_long`
#' T4.5 No error when the `summary_function` is `summarize_short`
#' T5. The tableone removes strata variables in rows that leads NA values
#' T5.1 An error when the the table includes one strata variable
#' T5.2 An error when the the table includes multiple strata variables

# Requirement T1 ----------------------------------------------------------

Expand All @@ -39,18 +42,18 @@ testthat::test_that("T1.1. No error when `data` is of class `data.frame`", {
testthat::test_that("T1.2. No error when `data` is of class `tibble`", {

data <- dplyr::as_tibble(adtte)

testthat::expect_error(visR::get_tableone(data = data), NA)

})

testthat::test_that("T1.3 No error when `data` is of class `data.table`", {

if (nzchar(find.package("data.table"))) {

data <- data.table::as.data.table(adtte)
testthat::expect_error(visR::get_tableone(data = data), NA)

}
})

Expand Down Expand Up @@ -85,18 +88,18 @@ testthat::test_that("T2.2 An error when `strata` is a string that is not a colna

testthat::test_that("T2.3 Additional colnames in the tableone are the `strata` values (for one `strata`)", {

trtp_colnames <- colnames(visR::get_tableone(data = adtte,
trtp_colnames <- colnames(visR::get_tableone(data = adtte,
strata = c("TRTP")))[4:6]
testthat::expect_equal(trtp_colnames, levels(adtte$TRTP))

})

testthat::test_that("T2.4 Additional colnames in the tableone are the crossproduct of all `strata` values (for more than one `strata`)", {

mapply_colnames <- c(mapply(function(x, y) paste(x, y, sep = "_"),
mapply_colnames <- c(mapply(function(x, y) paste(x, y, sep = "_"),
levels(adtte$TRTP),
MoreArgs = list(levels(adtte$SEX))))
visR_colnames <- colnames(visR::get_tableone(data = adtte,
visR_colnames <- colnames(visR::get_tableone(data = adtte,
strata = c("TRTP", "SEX")))[4:9]
testthat::expect_equal(mapply_colnames, visR_colnames)

Expand All @@ -107,30 +110,30 @@ testthat::test_that("T2.4 Additional colnames in the tableone are the crossprodu
testthat::context("get_tableone - T3. The tableone includes expected columnnames")

testthat::test_that("T3.1 Tableone by default includes columns `variable`, `statistic`, and `Total`", {

tableone_colnames <- colnames(visR::get_tableone(data = adtte))

testthat::expect_equal(tableone_colnames, c("variable", "statistic", "Total"))

})

testthat::test_that("T3.2 Tableone still includes the colum `Total` if `overall` is FALSE but no `strata` is given", {

tableone_colnames <- colnames(visR::get_tableone(data = adtte, overall = FALSE))

testthat::expect_equal(tableone_colnames, c("variable", "statistic", "Total"))

})

testthat::test_that("T3.3 Tableone does not include the colum `Total` if `overall` is FALSE and a `strata` is given", {

tblone_colnames <- colnames(visR::get_tableone(data = adtte,
overall = FALSE,
tblone_colnames <- colnames(visR::get_tableone(data = adtte,
overall = FALSE,
strata = c("TRTP")))
testthat::expect_equal(tblone_colnames,

testthat::expect_equal(tblone_colnames,
c("variable", "statistic", levels(adtte$TRTP)))

})

# Requirement T4 ---------------------------------------------------------------
Expand All @@ -139,38 +142,69 @@ testthat::context("get_tableone - T4. The function only accepts suitable summary

testthat::test_that("T4.1 An error when the `summary_function` is NULL", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = NULL))

})

testthat::test_that("T4.2 An error when the `summary_function` is a string", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = "A"))

})

testthat::test_that("T4.3 An error when the `summary_function` is a function not build for it", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = sum))

})

testthat::test_that("T4.4 An error when the `summary_function` is `summarize_long`", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = summarize_long))

})


testthat::test_that("T4.5 No error when the `summary_function` is `summarize_short`", {

testthat::expect_error(visR::get_tableone(data = adtte,
testthat::expect_error(visR::get_tableone(data = adtte,
summary_function = summarize_short), NA)

})

# Requirement T5 ---------------------------------------------------------------

testthat::context("get_tableone - T5. The tableone removes strata variables in rows that leads NA values")

testthat::test_that("T5.1 An error when the the table includes one strata variable", {

strata <- c("SEX")

table <- adtte %>%
visR::get_tableone(strata = strata)

testthat::expect_true(sum(strata %in% unique(table$variable)) == 0)

})

testthat::test_that("T5.2 An error when the the table includes multiple strata variables", {

strata <- c("SEX", "RACE")

table <- adtte %>%
visR::get_tableone(strata = strata)

testthat::expect_true(sum(strata %in% unique(table$variable)) == 0)

table <- adtte %>%
visR::get_tableone(strata = strata, overall = FALSE)

testthat::expect_true(sum(strata %in% unique(table$variable)) == 0)

})

# END OF CODE -------------------------------------------------------------