Skip to content

Commit 1519afe

Browse files
committed
Added helper functions and linting
1 parent cd6d4fd commit 1519afe

File tree

1 file changed

+44
-16
lines changed

1 file changed

+44
-16
lines changed

R/utils.R

Lines changed: 44 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ num_to_superscript <- function(x) {
9595
x <- as.character(x)
9696
splitx <- strsplit(x, "", fixed = TRUE)
9797

98-
vapply(splitx, function(y) paste0(nums[y], collapse = ""), character(1L))
98+
vapply(splitx, function(y) paste(nums[y], collapse = ""), character(1L))
9999
}
100100
ordinal <- function(x) {
101101
if (is_null(x) || !is.numeric(x)) {
@@ -152,12 +152,12 @@ round_df_char <- function(df, digits, pad = "0", na_vals = "") {
152152

153153
if (!identical(as.character(pad), "0") && any(grepl(".", df[[i]], fixed = TRUE))) {
154154
s <- strsplit(df[[i]], ".", fixed = TRUE)
155-
lengths <- lengths(s)
155+
s_lengths <- lengths(s)
156156
digits.r.of.. <- rep.int(0, NROW(df))
157-
digits.r.of..[lengths > 1L] <- nchar(vapply(s[lengths > 1L], `[[`, character(1L), 2L))
157+
digits.r.of..[s_lengths > 1L] <- nchar(vapply(s[s_lengths > 1L], `[[`, character(1L), 2L))
158158
max.dig <- max(digits.r.of..)
159159

160-
dots <- ifelse(lengths > 1L, "", if (as.character(pad) != "") "." else pad)
160+
dots <- ifelse(s_lengths > 1L, "", if (nzchar(as.character(pad))) "." else pad)
161161
pads <- vapply(max.dig - digits.r.of.., function(n) strrep(pad, n), character(1L))
162162

163163
df[[i]] <- paste0(df[[i]], dots, pads)
@@ -407,7 +407,7 @@ col.w.m <- function(mat, w = NULL, na.rm = TRUE) {
407407
col.w.v <- function(mat, w = NULL, bin.vars = NULL, na.rm = TRUE) {
408408
if (!is.matrix(mat)) {
409409
if (is.data.frame(mat)) {
410-
if (any(vapply(mat, chk::vld_character_or_factor, logical(1L)))) {
410+
if (any_apply(mat, chk::vld_character_or_factor)) {
411411
stop("'mat' must be a numeric matrix.")
412412
}
413413

@@ -556,16 +556,16 @@ w.quantile <- function(x, probs = seq(0, 1, 0.25), w = NULL, na.rm = FALSE, ...)
556556
}
557557
}
558558

559-
order <- order(x)
560-
x <- x[order]
561-
w <- w[order]
559+
ord <- order(x)
560+
x <- x[ord]
561+
w <- w[ord]
562562

563563
rw <- {
564564
if (is_null(w)) seq_len(n) / n
565565
else cumsum(w) / sum(w)
566566
}
567567

568-
q <- vapply(probs, function(p) {
568+
unname(vapply(probs, function(p) {
569569
if (p == 0) {
570570
return(x[1L])
571571
}
@@ -578,9 +578,7 @@ w.quantile <- function(x, probs = seq(0, 1, 0.25), w = NULL, na.rm = FALSE, ...)
578578

579579
if (rw[select] == p) mean(x[c(select, select + 1L)])
580580
else x[select]
581-
}, x[1L])
582-
583-
unname(q)
581+
}, x[1L]))
584582
}
585583

586584
#Formulas
@@ -768,7 +766,7 @@ get_covs_and_treat_from_formula <- function(f, data = NULL, terms = FALSE, sep =
768766
stop("'sep' must be a string of length 1.", call. = FALSE)
769767
}
770768

771-
s <- !identical(sep, "")
769+
s <- nzchar(sep)
772770

773771
if (s) original.covs.levels <- make_list(names(covs))
774772

@@ -1208,10 +1206,10 @@ Invert <- function(f) {
12081206
return(mat.list[[1L]])
12091207
}
12101208

1211-
nrow <- sum(vapply(mat.list, nrow, numeric(1L)))
1212-
ncol <- sum(vapply(mat.list, ncol, numeric(1L)))
1209+
out <- matrix(0,
1210+
nrow = sum(vapply(mat.list, nrow, numeric(1L))),
1211+
ncol = sum(vapply(mat.list, ncol, numeric(1L))))
12131212

1214-
out <- matrix(0, nrow = nrow, ncol = ncol)
12151213
row_start <- 1L
12161214
col_start <- 1L
12171215

@@ -1238,3 +1236,33 @@ Invert <- function(f) {
12381236
vec[start[i]:end[i]]
12391237
})
12401238
}
1239+
1240+
any_apply <- function(X, FUN, ...) {
1241+
FUN <- match.fun(FUN)
1242+
if (!is.vector(X) || is.object(X)) {
1243+
X <- as.list(X)
1244+
}
1245+
1246+
for (x in X) {
1247+
if (isTRUE(FUN(x, ...))) {
1248+
return(TRUE)
1249+
}
1250+
}
1251+
1252+
FALSE
1253+
}
1254+
1255+
all_apply <- function(X, FUN, ...) {
1256+
FUN <- match.fun(FUN)
1257+
if (!is.vector(X) || is.object(X)) {
1258+
X <- as.list(X)
1259+
}
1260+
1261+
for (x in X) {
1262+
if (isFALSE(FUN(x, ...))) {
1263+
return(FALSE)
1264+
}
1265+
}
1266+
1267+
TRUE
1268+
}

0 commit comments

Comments
 (0)