Skip to content

Commit 0ce6efc

Browse files
author
Tyson Barrett
committed
Adding error catching to Table1
1 parent c153987 commit 0ce6efc

File tree

2 files changed

+18
-7
lines changed

2 files changed

+18
-7
lines changed

R/Table1.R

+14-6
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ table1 = function(.data, ..., splitby=NULL, splitby_labels = NULL, test=FALSE, t
2525
splitby_ = as.factor(1)
2626
d$split = droplevels(splitby_)
2727
} else {
28-
splitby_ = table1_(.data, splitby)
29-
d$split = droplevels(as.factor(unlist(splitby_)))
28+
splitby_ = table1_(.data, splitby, split=TRUE)
29+
d$split = droplevels(as.factor(splitby_[[1]]))
3030
}
3131

3232
if (test & length(levels(d$split))>1){
@@ -291,7 +291,7 @@ print.table1 <- function(x, ...){
291291
}
292292

293293

294-
table1_ <- function(d_, vars){
294+
table1_ <- function(d_, vars, split=FALSE){
295295
d1 = named = NULL
296296

297297
## for dots_capture
@@ -300,7 +300,6 @@ table1_ <- function(d_, vars){
300300
named <- paste(vars[[i]])
301301
d1[[i]] <- f_eval(vars[[i]], d_)
302302

303-
304303
## if is an index (built on assumption that lengths will differ)
305304
if (length(d1[[i]]) != length(d_[[1]]) & is.numeric(d1[[i]])){
306305
d2 <- d_[, d1[[i]]]
@@ -313,13 +312,22 @@ table1_ <- function(d_, vars){
313312
}
314313

315314
## for single vars
316-
} else if (is_formula(vars)){
315+
} else if (split){
317316
named <- paste(vars)
318317
d1 <- f_eval(vars, d_)
319-
d2 <- as.list(d1)
318+
if (!is.factor(d1))
319+
stop("'splitby' must be a formula of a factor variable (e.g. ~var1")
320+
d2 <- as.data.frame(d1)
320321
names(d2) <- named[[2]]
321322
}
322323

324+
## Error catching
325+
if (dim(d_)[1] != length(d2[[1]])){
326+
stop("There is a problem with the variable names supplied. Make sure the ... only includes unquoted var names or a single vector of indices or that splitby variable is stated as a formula (e.g. ~var1)",
327+
call.=FALSE)
328+
}
329+
323330
return(d2)
324331
}
325332

333+

man/table1_.Rd

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
\description{ Allows Non-standard evaluation of Table 1}
55

66
\usage{
7-
table1_(d_, vars)
7+
table1_(d_, vars, split=FALSE)
88
}
99

1010
\arguments{
@@ -13,6 +13,9 @@ table1_(d_, vars)
1313
}
1414
\item{vars}{
1515
formula or list of formulas containing the variables in the data set that are to be summarized
16+
}
17+
\item{split}{
18+
logical; is it the splitby variable?
1619
}
1720
}
1821

0 commit comments

Comments
 (0)