Skip to content

Commit

Permalink
STYLE: More lintr style
Browse files Browse the repository at this point in the history
  • Loading branch information
HenrikBengtsson committed Mar 29, 2017
1 parent 7e3dfa2 commit 99432bf
Show file tree
Hide file tree
Showing 37 changed files with 236 additions and 122 deletions.
18 changes: 18 additions & 0 deletions .make/Makefile
Expand Up @@ -56,6 +56,7 @@ R_NO_INIT := --no-init-file
R_VERSION_STATUS := $(shell $(R_SCRIPT) -e "status <- tolower(R.version[['status']]); if (regexpr('unstable', status) != -1L) status <- 'devel'; cat(status)")
R_VERSION_X_Y := $(shell $(R_SCRIPT) -e "cat(gsub('[.][0-9]+$$', '', getRversion()))")
R_VERSION := $(shell $(R_SCRIPT) -e "cat(as.character(getRversion()))")
R_VERSION_3_3_OR_NEWER := $(shell $(R_SCRIPT) -e "cat(getRversion() >= '3.3.0')")
R_VERSION_FULL := $(R_VERSION)$(R_VERSION_STATUS)
R_LIBS_USER_X := $(shell $(R_SCRIPT) -e "cat(.libPaths()[1])")
R_INCLUDE := $(shell $(R_SCRIPT) -e "cat(R.home('include'))")
Expand All @@ -79,6 +80,16 @@ GIT_COMMIT := $(shell $(GIT) log -1 --format="%h")
R_LIBS_BRANCH := $(CURDIR)/.R/$(GIT_BRANCH)


# Asserts proper Windows toolchain in R (>= 3.3.0)
ifeq ($(OS), Windows_NT)
ifeq ($(R_VERSION_3_3_OR_NEWER), TRUE)
ifndef BINPREF
$(error R (>= 3.3.0) on Windows: BINPREF not set)
endif
endif
endif


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Main
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down Expand Up @@ -123,6 +134,7 @@ debug:
@echo R_RD4PDF=\'$(R_RD4PDF)\'
@echo
@echo R_CRAN_OUTDIR=\'$(R_CRAN_OUTDIR)\'
@echo R_VERSION_3_3_OR_NEWER=\'$(R_VERSION_3_3_OR_NEWER)\'
@echo


Expand Down Expand Up @@ -326,6 +338,12 @@ $(R_OUTDIR)/vigns: install
vignettes: $(R_OUTDIR)/vigns


# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Static code validation etc
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
lint:
$(R_SCRIPT) -e "library(lintr); lint_package(linters = with_defaults(commented_code_linter = NULL, closed_curly_linter = closed_curly_linter(allow_single_line = TRUE), open_curly_linter = open_curly_linter(allow_single_line = TRUE)))"

# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Run package tests
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Expand Down
2 changes: 1 addition & 1 deletion R/rowIQRs.R
Expand Up @@ -61,7 +61,7 @@ iqr <- function(x, idxs = NULL, na.rm = FALSE, ...) {
# Apply subset
if (!is.null(idxs)) x <- x[idxs]

if(na.rm) {
if (na.rm) {
x <- x[!is.na(x)]
} else if (anyMissing(x)) {
return(NA_real_)
Expand Down
2 changes: 1 addition & 1 deletion R/varDiff.R
Expand Up @@ -188,7 +188,7 @@ iqrDiff <- function(x, idxs = NULL, na.rm = FALSE, diff = 1L, trim = 0, ...) {
# Apply subset
if (!is.null(idxs)) x <- x[idxs]

if(na.rm) {
if (na.rm) {
x <- x[!is.na(x)]
} else if (anyMissing(x)) {
return(NA_real_)
Expand Down
4 changes: 2 additions & 2 deletions man/binCounts.Rd

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

4 changes: 2 additions & 2 deletions man/binMeans.Rd

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

2 changes: 1 addition & 1 deletion man/rowCounts.Rd

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

4 changes: 3 additions & 1 deletion tests/allocArray.R
Expand Up @@ -6,7 +6,9 @@ allocArray_R <- function(nrow, ncol, value = NA) {

values <- list(
-1L, 0L, +1L, NA_integer_, .Machine$integer.max,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf, .Machine$double.xmin, .Machine$double.xmax, .Machine$double.eps, .Machine$double.neg.eps,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf,
.Machine$double.xmin, .Machine$double.xmax,
.Machine$double.eps, .Machine$double.neg.eps,
FALSE, TRUE, NA
)

Expand Down
4 changes: 3 additions & 1 deletion tests/allocMatrix.R
Expand Up @@ -6,7 +6,9 @@ allocMatrix_R <- function(nrow, ncol, value = NA) {

values <- list(
-1L, 0L, +1L, NA_integer_, .Machine$integer.max,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf, .Machine$double.xmin, .Machine$double.xmax, .Machine$double.eps, .Machine$double.neg.eps,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf,
.Machine$double.xmin, .Machine$double.xmax,
.Machine$double.eps, .Machine$double.neg.eps,
FALSE, TRUE, NA
)

Expand Down
4 changes: 3 additions & 1 deletion tests/allocVector.R
Expand Up @@ -8,7 +8,9 @@ allocVector_R <- function(length, value = NA) {

values <- list(
-1L, 0L, +1L, NA_integer_, .Machine$integer.max,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf, .Machine$double.xmin, .Machine$double.xmax, .Machine$double.eps, .Machine$double.neg.eps,
-1, 0, +1, NA_real_, NaN, -Inf, +Inf,
.Machine$double.xmin, .Machine$double.xmax,
.Machine$double.eps, .Machine$double.neg.eps,
FALSE, TRUE, NA
)

Expand Down
3 changes: 2 additions & 1 deletion tests/anyMissing_subset.R
Expand Up @@ -9,7 +9,8 @@ x[2] <- NA
for (mode in c("integer", "numeric")) {
storage.mode(x) <- mode
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = anyMissing, fsure = function(x, ...) {
validateIndicesTestVector(x, idxs,
ftest = anyMissing, fsure = function(x, ...) {
anyValue(x, value = NA)
})
}
Expand Down
3 changes: 2 additions & 1 deletion tests/binCounts.R
Expand Up @@ -5,7 +5,8 @@ library("stats")
# Local functions
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
binCounts_hist <- function(x, bx, right = FALSE, ...) {
n0 <- graphics::hist(x, breaks = bx, right = right, include.lowest = TRUE, plot = FALSE)$counts
n0 <- graphics::hist(x, breaks = bx, right = right,
include.lowest = TRUE, plot = FALSE)$counts
}


Expand Down
10 changes: 8 additions & 2 deletions tests/binMeans,binCounts_subset.R
Expand Up @@ -42,7 +42,13 @@ storage.mode(x) <- "integer"
bx <- c(-6, 0, 3, 4, 10)
for (idxs in indexCases) {
for (na.rm in c(TRUE, FALSE)) {
validateIndicesTestVector_w(y, x, idxs, ftest = binMeans, fsure = binMeans0, bx = bx, na.rm = na.rm, count = TRUE, right = FALSE)
validateIndicesTestVector_w(y, x, idxs, ftest = binMeans, fsure = binMeans0, bx = bx, na.rm = na.rm, count = TRUE, right = TRUE)
validateIndicesTestVector_w(y, x, idxs,
ftest = binMeans, fsure = binMeans0,
bx = bx, na.rm = na.rm,
count = TRUE, right = FALSE)
validateIndicesTestVector_w(y, x, idxs,
ftest = binMeans, fsure = binMeans0,
bx = bx, na.rm = na.rm,
count = TRUE, right = TRUE)
}
}
12 changes: 9 additions & 3 deletions tests/count_subset.R
Expand Up @@ -17,7 +17,13 @@ source("utils/validateIndicesFramework.R")
x <- runif(6, min = -3, max = 3)
storage.mode(x) <- "integer"
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = count, fsure = count_R, value = 0, na.rm = TRUE)
validateIndicesTestVector(x, idxs, ftest = count, fsure = count_R, value = 0, na.rm = FALSE)
validateIndicesTestVector(x, idxs, ftest = count, fsure = count_R, value = NA_integer_)
validateIndicesTestVector(x, idxs,
ftest = count, fsure = count_R,
value = 0, na.rm = TRUE)
validateIndicesTestVector(x, idxs,
ftest = count, fsure = count_R,
value = 0, na.rm = FALSE)
validateIndicesTestVector(x, idxs,
ftest = count, fsure = count_R,
value = NA_integer_)
}
3 changes: 2 additions & 1 deletion tests/diff2.R
Expand Up @@ -14,7 +14,8 @@ for (mode in c("integer", "double")) {

for (l in 1:3) {
for (d in 1:4) {
cat(sprintf("%s: NAs = %s, lag = %d, differences = %d\n", mode, hasNA, l, d))
cat(sprintf("%s: NAs = %s, lag = %d, differences = %d\n",
mode, hasNA, l, d))
y0 <- diff(x, lag = l, differences = d)
str(y0)
y1 <- diff2(x, lag = l, differences = d)
Expand Down
4 changes: 3 additions & 1 deletion tests/diff2_subset.R
Expand Up @@ -8,7 +8,9 @@ x <- runif(6, min = -6, max = 6)
for (l in 1:2) {
for (d in 1:2) {
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = diff2, fsure = diff, lag = l, differences = d)
validateIndicesTestVector(x, idxs,
ftest = diff2, fsure = diff,
lag = l, differences = d)
}
}
}
8 changes: 6 additions & 2 deletions tests/logSumExp_subset.R
Expand Up @@ -11,6 +11,10 @@ logSumExp_R <- function(lx, na.rm = FALSE) {
source("utils/validateIndicesFramework.R")
x <- runif(6, min = -6, max = 6)
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = logSumExp, fsure = logSumExp_R, na.rm = FALSE)
validateIndicesTestVector(x, idxs, ftest = logSumExp, fsure = logSumExp_R, na.rm = TRUE)
validateIndicesTestVector(x, idxs,
ftest = logSumExp, fsure = logSumExp_R,
na.rm = FALSE)
validateIndicesTestVector(x, idxs,
ftest = logSumExp, fsure = logSumExp_R,
na.rm = TRUE)
}
3 changes: 2 additions & 1 deletion tests/meanOver.R
Expand Up @@ -27,7 +27,8 @@ for (kk in 1:20) {
cat("Adding NAs\n")
nna <- sample(n, size = 1L)
naValues <- c(NA_real_, NaN)
x[sample(length(x), size = nna)] <- sample(naValues, size = nna, replace = TRUE)
t <- sample(naValues, size = nna, replace = TRUE)
x[sample(length(x), size = nna)] <- t
}

# Integer or double?
Expand Down
8 changes: 6 additions & 2 deletions tests/meanOver_subset.R
Expand Up @@ -7,6 +7,10 @@ source("utils/validateIndicesFramework.R")
x <- runif(6, min = -6, max = 6)
storage.mode(x) <- "integer"
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = meanOver, fsure = mean, na.rm = FALSE)
validateIndicesTestVector(x, idxs, ftest = meanOver, fsure = mean, na.rm = TRUE)
validateIndicesTestVector(x, idxs,
ftest = meanOver, fsure = mean,
na.rm = FALSE)
validateIndicesTestVector(x, idxs,
ftest = meanOver, fsure = mean,
na.rm = TRUE)
}
8 changes: 6 additions & 2 deletions tests/product_subset.R
Expand Up @@ -7,6 +7,10 @@ source("utils/validateIndicesFramework.R")
x <- runif(6, min = -6, max = 6)
storage.mode(x) <- "integer"
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = product, fsure = prod, na.rm = TRUE)
validateIndicesTestVector(x, idxs, ftest = product, fsure = prod, na.rm = FALSE)
validateIndicesTestVector(x, idxs,
ftest = product, fsure = prod,
na.rm = TRUE)
validateIndicesTestVector(x, idxs,
ftest = product, fsure = prod,
na.rm = FALSE)
}
2 changes: 1 addition & 1 deletion tests/psortKM.R
@@ -1,5 +1,5 @@
library("matrixStats")
library("utils")
library("utils") ## utils::str

# - - - - - - - - - - - - - - - - - - - - - - - - - - - -
# Local functions
Expand Down
3 changes: 2 additions & 1 deletion tests/rowCollapse.R
Expand Up @@ -23,7 +23,8 @@ stopifnot(identical(y2, y))
idxs <- 1:3
y <- rowCollapse(x, idxs)
print(y)
yT <- c(x[1, 1], x[2, 2], x[3, 3], x[4, 1], x[5, 2], x[6, 3], x[7, 1], x[8, 2], x[9, 3])
yT <- c(x[1, 1], x[2, 2], x[3, 3], x[4, 1], x[5, 2],
x[6, 3], x[7, 1], x[8, 2], x[9, 3])
stopifnot(identical(y, yT))
y2 <- colCollapse(t(x), idxs)
stopifnot(identical(y2, y))
8 changes: 6 additions & 2 deletions tests/rowCounts.R
Expand Up @@ -2,9 +2,13 @@ library("matrixStats")

rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ...) {
if (is.na(value)) {
counts <- apply(x, MARGIN = 1L, FUN = function(x) sum(is.na(x)))
counts <- apply(x, MARGIN = 1L, FUN = function(x)
sum(is.na(x))
)
} else {
counts <- apply(x, MARGIN = 1L, FUN = function(x) sum(x == value, na.rm = na.rm))
counts <- apply(x, MARGIN = 1L, FUN = function(x)
sum(x == value, na.rm = na.rm)
)
}
as.integer(counts)
} # rowCounts_R()
Expand Down
8 changes: 6 additions & 2 deletions tests/rowCounts_subset.R
Expand Up @@ -2,9 +2,13 @@ library("matrixStats")

rowCounts_R <- function(x, value = TRUE, na.rm = FALSE, ...) {
if (is.na(value)) {
counts <- apply(x, MARGIN = 1L, FUN = function(x) sum(is.na(x)))
counts <- apply(x, MARGIN = 1L, FUN = function(x)
sum(is.na(x))
)
} else {
counts <- apply(x, MARGIN = 1L, FUN = function(x) sum(x == value, na.rm = na.rm))
counts <- apply(x, MARGIN = 1L, FUN = function(x)
sum(x == value, na.rm = na.rm)
)
}
as.integer(counts)
} # rowCounts_R()
Expand Down
3 changes: 2 additions & 1 deletion tests/rowDiffs.R
Expand Up @@ -34,7 +34,8 @@ for (mode in c("integer", "double")) {

for (lag in 1:4) {
for (differences in 1:3) {
cat(sprintf("mode: %s, lag = %d, differences = %d\n", mode, lag, differences))
cat(sprintf("mode: %s, lag = %d, differences = %d\n",
mode, lag, differences))
# Row/column ranges
r0 <- rowDiffs_R(x, lag = lag, differences = differences)
r1 <- rowDiffs(x, lag = lag, differences = differences)
Expand Down
3 changes: 2 additions & 1 deletion tests/rowMedians.R
Expand Up @@ -195,7 +195,8 @@ for (kk in seq_len(K)) {
cat("Adding NAs\n")
nna <- sample(n, size = 1)
naValues <- c(NA_real_, NaN)
x[sample(length(x), size = nna)] <- sample(naValues, size = nna, replace = TRUE)
t <- sample(naValues, size = nna, replace = TRUE)
x[sample(length(x), size = nna)] <- t
}

# Integer or double?
Expand Down
3 changes: 1 addition & 2 deletions tests/rowProds_subset.R
@@ -1,8 +1,7 @@
library("matrixStats")

rowProds_R <- function(x, FUN = prod, na.rm = FALSE, ...) {
y <- apply(x, MARGIN = 1L, FUN = FUN, na.rm = na.rm)
y
apply(x, MARGIN = 1L, FUN = FUN, na.rm = na.rm)
}


Expand Down
3 changes: 2 additions & 1 deletion tests/rowQuantiles.R
Expand Up @@ -87,7 +87,8 @@ for (kk in seq_len(K)) {
cat("Adding NAs\n")
nna <- sample(n, size = 1)
naValues <- c(NA_real_, NaN)
x[sample(length(x), size = nna)] <- sample(naValues, size = nna, replace = TRUE)
t <- sample(naValues, size = nna, replace = TRUE)
x[sample(length(x), size = nna)] <- t
}

# Integer or double?
Expand Down
6 changes: 4 additions & 2 deletions tests/rowRanks.R
Expand Up @@ -31,15 +31,17 @@ for (kk in 1:4) {
cat(sprintf("ties.method = %s\n", ties))
# rowRanks():
y1 <- matrixStats::rowRanks(x, ties.method = ties)
y2 <- t(apply(x, MARGIN = 1L, FUN = rank, na.last = "keep", ties.method = ties))
y2 <- t(apply(x, MARGIN = 1L, FUN = rank, na.last = "keep",
ties.method = ties))
stopifnot(identical(y1, y2))

y3 <- matrixStats::colRanks(t(x), ties.method = ties)
stopifnot(identical(y1, y3))

# colRanks():
y1 <- matrixStats::colRanks(x, ties.method = ties)
y2 <- t(apply(x, MARGIN = 2L, FUN = rank, na.last = "keep", ties.method = ties))
y2 <- t(apply(x, MARGIN = 2L, FUN = rank, na.last = "keep",
ties.method = ties))
stopifnot(identical(y1, y2))

y3 <- matrixStats::rowRanks(t(x), ties.method = ties)
Expand Down
2 changes: 1 addition & 1 deletion tests/rowWeightedMedians.R
Expand Up @@ -51,7 +51,7 @@ stopifnot(all.equal(xM2, xM1))


# Weighted row medians with missing values
xM0 <- apply(x, MARGIN = 1, FUN = weightedMedian, w = w, na.rm = TRUE)
xM0 <- apply(x, MARGIN = 1L, FUN = weightedMedian, w = w, na.rm = TRUE)
print(xM0)
xM1 <- rowWeightedMedians(x, w = w, na.rm = TRUE)
print(xM1)
Expand Down
3 changes: 2 additions & 1 deletion tests/sumOver.R
Expand Up @@ -27,7 +27,8 @@ for (kk in 1:20) {
cat("Adding NAs\n")
nna <- sample(n, size = 1L)
naValues <- c(NA_real_, NaN)
x[sample(length(x), size = nna)] <- sample(naValues, size = nna, replace = TRUE)
t <- sample(naValues, size = nna, replace = TRUE)
x[sample(length(x), size = nna)] <- t
}

# Integer or double?
Expand Down
8 changes: 6 additions & 2 deletions tests/sumOver_subset.R
Expand Up @@ -7,6 +7,10 @@ source("utils/validateIndicesFramework.R")
x <- runif(6, min = -6, max = 6)
storage.mode(x) <- "integer"
for (idxs in indexCases) {
validateIndicesTestVector(x, idxs, ftest = sumOver, fsure = sum, na.rm = FALSE)
validateIndicesTestVector(x, idxs, ftest = sumOver, fsure = sum, na.rm = TRUE)
validateIndicesTestVector(x, idxs,
ftest = sumOver, fsure = sum,
na.rm = FALSE)
validateIndicesTestVector(x, idxs,
ftest = sumOver, fsure = sum,
na.rm = TRUE)
}

0 comments on commit 99432bf

Please sign in to comment.