Skip to content

Commit

Permalink
Merge branch 'rc0.2.0' into develop
Browse files Browse the repository at this point in the history
  • Loading branch information
kaz-yos committed Feb 14, 2014
2 parents 1858995 + d64aaaf commit c3e49d8
Show file tree
Hide file tree
Showing 14 changed files with 212 additions and 144 deletions.
15 changes: 8 additions & 7 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ Package: tableone
Type: Package
Title: Create the "Table 1" to describe baseline characteristics
Version: 0.2.0
Date: 2014-02-12
Date: 2014-02-13
Author: Kazuki Yoshida, Justin Bohn
Maintainer: Kazuki Yoshida <[email protected]>
Description: This package creates create the "Table1", i.e.,
description of baseline characteristics in R although it is
essential in every medical research. The package was insipired by
and based on descriptive statistics functions in Deducer by Ian Fellows,
a Java-based GUI package. This package does not require GUI or Java,
and intended for CUI users.
Description: This package creates "Table 1", i.e., description of baseline
patient characteristics, which is essential every medical research. This
package provides functions to create such summaries for continuous and
categorical variables, optionally with subgroups and groupwise comparison.
The package was insipired by and based on descriptive statistics functions
in Deducer, a Java-based GUI package by Ian Fellows. This package does not
require GUI or Java, and intended for CUI users.
License: GPL-2
Depends:
e1071,
Expand Down
15 changes: 13 additions & 2 deletions NEWS
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
tableone 0.1.3 (not released yet)
tableone 0.2.0
----------------------------------------------------------------
NEW FEATURES

* CreateTableOne and related print/summary methods were added.

* CreateTableOne can crate a table with both categorical and
continuous variables.


tableone 0.1.3
----------------------------------------------------------------
NEW FEATURES

Expand All @@ -17,7 +27,8 @@ BUG FIXES

* Fixed incorrect specification of S3 method export.

* To make an object an S3 method and export it both at_S3method and at_export tags are needed in the Roxygen part of the code.
* To make an object an S3 method and export it both at_S3method and at_export
tags are needed in the Roxygen part of the code.

* Passed all the default tests by R CMD check file.tar.gz

Expand Down
73 changes: 45 additions & 28 deletions R/CreateTableOne.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
### 2014-02-09 Unifying function suggested by Justin Bohn
##' Create an object summarizing both categorical and continuous variables
##'
##' Create an object|output summarizing both categorical and continuous variables
##'
##' Create an object|output summarizing categorical variables optionally stratifying
##' Create an object summarizing categorical variables optionally stratifying
##' by one or more startifying variables and performing statistical tests. The
##' object gives a table that is easy to use in medical research papers.
##'
##' object gives a table that is easy to use in medical research papers. See also \code{\link{print.TableOne}} and \code{\link{summary.TableOne}}.
##'
##' @param vars Variables to be summarized given as a character vector. Factors are
##' handled as categorical variables, whereas numeric variables are handled as continuous variables.
##' @param strata Stratifying (grouping) variable name(s) given as a character
Expand All @@ -31,8 +29,7 @@
##' memory limitation. In this situation, the large sample approximation based
##' should suffice.
##' @param argsExact A named list of arguments passed to the function specified in testExact. The default is \code{list(workspace = 2*10^5)}, which specifies the memory space allocated for \code{\link{fisher.test}}.
##' @return An object of class \code{TableOne}, which really is a list of two \code{\link{by}} objects with
##' additional attributes. These correspond to structures holding results for
##' @return An object of class \code{TableOne}, which really is a list of three objects. The first object named \code{object$TableOne} is the categorical-continuous mixture table formatted and printed by the \code{\link{print.TableOne}} method. The second object named \code{object$ContTable} is the table object containing continuous variables only. The third object named \code{object$CatTable} is the table object containing categorical variables only. The second and third objects can be then be examined with the \code{print} and \code{summary} method, for example, \code{summary(object$CatTable)} to examine the categorical variables in detail.
##' @author Justin Bohn, Kazuki Yoshida
##' @seealso
##' \code{\link{CreateTableOne}}, \code{\link{print.TableOne}}, \code{\link{summary.TableOne}},
Expand All @@ -53,12 +50,15 @@
##' varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
##' pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)
##'
##' ## Create Table 1 stratified by sex and trt
##' tableOne <- CreateTableOne(vars = c("time","status","age","ascites","hepato",
##' "spiders","edema","bili","chol","albumin",
##' "copper","alk.phos","ast","trig","platelet",
##' "protime","stage"),
##' strata = c("sex","trt"), data = pbc)
##' ## Create a variable list
##' dput(names(pbc))
##' vars <- c("time","status","age","sex","ascites","hepato",
##' "spiders","edema","bili","chol","albumin",
##' "copper","alk.phos","ast","trig","platelet",
##' "protime","stage")
##'
##' ## Create Table 1 stratified by trt
##' tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)
##'
##' ## Just typing the object name will invoke the print.TableOne method
##' tableOne
Expand All @@ -68,12 +68,20 @@
##' ## argument to obtain the exact test p-values.
##' print(tableOne, nonnormal = c("time"), exact = c("ascites"))
##'
##' ## Use the summary.TableOne method for depth summary
##' ## Use the summary.TableOne method for detailed summary
##' summary(tableOne)
##'
##' ## See the categorical part only using $ operator
##' tableOne$CatTable
##'
##' ## See the continuous part only using $ operator
##' tableOne$ContTable
##'
##' @export
CreateTableOne <-
function(vars, strata, data,
function(vars, # character vector of variable names
strata, # character vector of variable names
data, # data frame
test = TRUE, # whether to put p-values
## Test configuration for categorical data
testApprox = chisq.test, # function for approximation test
Expand All @@ -97,6 +105,9 @@ CreateTableOne <-
## Abort if no variables exist at this point
ModuleStopIfNoVarsLeft(vars)

## Toggle test FALSE if no strata is given
test <- ModuleReturnFalseIfNoStrata(strata, test)

## Get the classes of the variables
varClasses <- sapply(data[vars], class)
varFactors <- names(varClasses[varClasses == "factor"])
Expand All @@ -106,20 +117,26 @@ CreateTableOne <-
logiFactors <- sapply(data[vars], is.factor)

## Create lists of arguments
argsCreateContTable <- list(strata = strata, data = data,
test = test,
testNormal = testNormal,
argsNormal = argsNormal,
argsCreateContTable <- list(data = data,
test = test,
testNormal = testNormal,
argsNormal = argsNormal,
testNonNormal = testNonNormal,
argsNonNormal = argsNonNormal
)
argsCreateCatTable <- list(strata = strata, data = data,
test = test,
testApprox = testApprox,
argsApprox = argsApprox,
testExact = testExact,
argsExact = argsExact
argsCreateCatTable <- list(data = data,
test = test,
testApprox = testApprox,
argsApprox = argsApprox,
testExact = testExact,
argsExact = argsExact
)
## Add strata = strata for argument only if strata is given
if(!missing(strata)) {

argsCreateContTable <- c(list(strata = strata), argsCreateContTable)
argsCreateCatTable <- c(list(strata = strata), argsCreateCatTable)
}


## Condition on the absence of factor/numeric
Expand Down Expand Up @@ -172,8 +189,8 @@ CreateTableOne <-
ContTable <- do.call(CreateContTable,
args = c(list(vars = varNumerics), argsCreateContTable))
## Aggregated CatTable
CatTable <- do.call(CreateCatTable,
args = c(list(vars = varFactors), argsCreateCatTable))
CatTable <- do.call(CreateCatTable,
args = c(list(vars = varFactors), argsCreateCatTable))

## Create a list
listOfTables <- list(TableOne = TableOne,
Expand Down
6 changes: 3 additions & 3 deletions R/modules.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ ModuleStopIfNoVarsLeft <- function(vars) {
##
ModuleReturnFalseIfNoStrata <- function(strata, test) { # Give strata variable names

if(missing(strata)){
if(missing(strata)) {
## test cannot be performed if no strata
test <- FALSE
}
Expand All @@ -44,7 +44,7 @@ ModuleReturnFalseIfNoStrata <- function(strata, test) { # Give strata variable n
## Check statra variables and conditionally create
ModuleReturnStrata <- function(strata, data, dat) { # Give strata variable names

if(missing(strata)){
if(missing(strata)) {
## If there is no strata, give "Overall" to every subject
strata <- rep("Overall", dim(dat)[1]) # Check if dim(dat)[[1]] is correct.
} else {
Expand Down Expand Up @@ -107,7 +107,7 @@ ModuleCreateStrataVarName <- function(obj) {
## Used to define non-failing functions, that return NA when there is an error
tryCatch.W.E <- function(expr) {
W <- NULL
w.handler <- function(w){ # warning handler
w.handler <- function(w) { # warning handler
W <<- w
invokeRestart("muffleWarning")
}
Expand Down
6 changes: 5 additions & 1 deletion R/print.CatTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -456,11 +456,15 @@ print.CatTable <- function(x, missing = FALSE,

## Name the row dimension with it. 1st dimension name should be empty.
names(dimnames(out)) <- c("", strataString)
} else {

names(dimnames(out)) <- c("", "")
}


## Modular version of quote/print toggle.
out <- ModuleQuoteAndPrintMat(matObj = out, quote = quote, printToggle = printToggle)
out <- ModuleQuoteAndPrintMat(matObj = out,
quote = quote, printToggle = printToggle)

## Print CrossTable() if requested
if (CrossTable) {
Expand Down
6 changes: 5 additions & 1 deletion R/print.ContTable.R
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,15 @@ print.ContTable <- function(x, missing = FALSE,

## Name the row dimension with it. 1st dimension name should be empty.
names(dimnames(out)) <- c("", strataString)
} else {

names(dimnames(out)) <- c("", "")
}


## (module) Takes an matrix object format, print if requested
out <- ModuleQuoteAndPrintMat(matObj = out, quote = quote, printToggle = printToggle)
out <- ModuleQuoteAndPrintMat(matObj = out,
quote = quote, printToggle = printToggle)

## Add attributes for column widths in characters
attributes(out) <- c(attributes(out),
Expand Down
38 changes: 18 additions & 20 deletions R/print.TableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
##' ## argument to obtain the exact test p-values.
##' print(tableOne, nonnormal = c("time"), exact = c("ascites"))
##'
##' ## Use the summary.TableOne method for depth summary
##' ## Use the summary.TableOne method for detailed summary
##' summary(tableOne)
##'
##' @export
Expand Down Expand Up @@ -115,7 +115,8 @@ print.TableOne <- function(x, missing = FALSE,

attributes(matObj)$vecColWidths
},
simplify = TRUE)
simplify = FALSE)
columnWidthInfo <- do.call(cbind, columnWidthInfo)

## Get the max values for each stratum
vecMaxValues <- apply(columnWidthInfo, MARGIN = 1, FUN = max, na.rm = TRUE)
Expand Down Expand Up @@ -163,28 +164,25 @@ print.TableOne <- function(x, missing = FALSE,
## Row-combin n and all variables
out <- do.call(rbind, c(list(stratumSizesRow), spaceFormattedTables))

## Add stratification information to the column header (This is also in the constructor)
if (length(TableOne[[1]]) > 1 ) {
## Combine variable names with : in between
strataVarName <- attributes(TableOne[[1]])$strataVarName

## Create strata string
strataString <- paste0("Stratified by ", strataVarName)

## Name the row dimension with it. 1st dimension name should be empty.
names(dimnames(out)) <- c("", strataString)
} else {

names(dimnames(out)) <- c("", "")
}

## Modular version of quote/print toggle.
out <- ModuleQuoteAndPrintMat(matObj = out,
quote = quote, printToggle = printToggle)

## Return the result
return(invisible(out))
}


















2 changes: 1 addition & 1 deletion R/summary.TableOne.R
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
##' ## argument to obtain the exact test p-values.
##' print(tableOne, nonnormal = c("time"), exact = c("ascites"))
##'
##' ## Use the summary.TableOne method for depth summary
##' ## Use the summary.TableOne method for detailed summary
##' summary(tableOne)
##'
##' @export
Expand Down
61 changes: 32 additions & 29 deletions R/tableone-package.R
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
##' Create "Table 1" to describe baseline characteristics
##'
##' Table 1, i.e., description of baseline patient characteristics is essential
##' every medical research. This package provides functions to create such
##' summaries for continuous and categorical variables, optionally with subgroups
##' and groupwise comparison. The package was insipired by descriptive statistics
##' functions in Deducer, Java-based GUI package. This package does not require
##' GUI or Java, and intended for CUI users.
##'
##' \tabular{ll}{ Package: \tab tableone\cr Type: \tab Package\cr Version: \tab
##' 0.1.3\cr Date: \tab 2014-02-08\cr License: \tab GPL-2\cr } Create an object
##' summarizing continous and categorical variables optionally stratifying by
##' one or more startifying variables and performing statistical tests. The
##' object gives a table that is easy to use in medical research papers.
##' Continuous variables are handled by CreateContTable, and categorical
##' variables are handled by CreateCatTable.
##' This package creates "Table 1", i.e., description of baseline patient characteristics, which is essential every medical research. This package provides functions to create such summaries for continuous and categorical variables, optionally with subgroups and groupwise comparison. The package was insipired by and based on descriptive statistics functions in Deducer, a Java-based GUI package by Ian Fellows. This package does not require GUI or Java, and intended for CUI users.
##'
##' @name tableone-package
##' @aliases tableone-package tableone
##' @docType package
##' @note Special Thanks:
##'
##' Ian Fellows for developing the Deducer package, which this package is based on.
##' Hadley Wickham for packaging advice and for creating tools this package was made with.
##'
##' Hadley Wickham for packaging advice and for creating tools this package was made with (roxygen2, devtools, testthat).
##'
##' Developmental repository is on github. Your contributions are appreciated.
##'
##' https://github.com/kaz-yos/tableone
##'
##' @author Kazuki Yoshida, Justin Bohn
Expand All @@ -44,22 +34,35 @@
##' ## Check variables
##' head(pbc)
##'
##' ## Create an overall table for categorical variables
##' catVars <- c("status","ascites","hepato","spiders","edema","stage")
##' catTableOverall <- CreateCatTable(vars = catVars, data = pbc)
##' ## Make categorical variables factors
##' varsToFactor <- c("status","trt","ascites","hepato","spiders","edema","stage")
##' pbc[varsToFactor] <- lapply(pbc[varsToFactor], factor)
##'
##' ## Create a variable list
##' dput(names(pbc))
##' vars <- c("time","status","age","sex","ascites","hepato",
##' "spiders","edema","bili","chol","albumin",
##' "copper","alk.phos","ast","trig","platelet",
##' "protime","stage")
##'
##' ## Create Table 1 stratified by trt
##' tableOne <- CreateTableOne(vars = vars, strata = c("trt"), data = pbc)
##'
##' ## Just typing the object name will invoke the print.TableOne method
##' tableOne
##'
##' ## Simply typing the object name will invoke the print.CatTable method,
##' ## which will show the sample size, frequencies and percentages.
##' ## For 2-level variables, only the higher level is shown for simplicity.
##' catTableOverall
##' ## Specifying nonnormal variables will show the variables appropriately,
##' ## and show nonparametric test p-values. Specify variables in the exact
##' ## argument to obtain the exact test p-values.
##' print(tableOne, nonnormal = c("time"), exact = c("ascites"))
##'
##' ## Create an overall table for continuous variables
##' contVars <- c("time","age","bili","chol","albumin","copper","alk.phos","ast",
##' "trig","platelet","protime")
##' contTableOverall <- CreateContTable(vars = contVars, data = pbc)
##' ## Use the summary.TableOne method for detailed summary
##' summary(tableOne)
##'
##' ## See the categorical part only using $ operator
##' tableOne$CatTable
##'
##' ## Simply typing the object name will invoke the print.ContTable method,
##' ## which will show the sample size, means and standard deviations.
##' contTableOverall
##' ## See the continuous part only using $ operator
##' tableOne$ContTable
##'
NULL
Loading

0 comments on commit c3e49d8

Please sign in to comment.