Skip to content

Commit 32ce3c8

Browse files
committed
R CMD Check fixes
1 parent e8ee117 commit 32ce3c8

13 files changed

+264
-267
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ Suggests:
3535
rmarkdown,
3636
survival,
3737
keras,
38-
C50,
3938
xgboost,
40-
covr
39+
covr,
40+
sparklyr
4141

R/misc.R

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,9 +79,14 @@ model_printer <- function(x, ...) {
7979
}
8080
}
8181

82-
load_libs <- function(x, quiet) {
83-
for (pkg in x$method$libs)
82+
load_libs <- function(x, quiet, attach = FALSE) {
83+
for (pkg in x$method$libs) {
84+
if(attach) {
8485
suppressPackageStartupMessages(requireNamespace(pkg, quietly = quiet))
86+
} else {
87+
library(pkg, character.only = TRUE)
88+
}
89+
}
8590
invisible(x)
8691
}
8792

@@ -216,3 +221,22 @@ new_model_spec <- function(cls, args, eng_args, mode, method, engine) {
216221
class(out) <- make_classes(cls)
217222
out
218223
}
224+
225+
# ------------------------------------------------------------------------------
226+
227+
check_outcome <- function(y, spec) {
228+
if (spec$mode == "unknown") {
229+
return(invisible(NULL))
230+
} else if (spec$mode == "regression") {
231+
if (!is.numeric(y))
232+
stop("The model outcome should be numeric for regression models.",
233+
call. = FALSE)
234+
} else if (spec$mode == "classification") {
235+
if (!is.factor(y)) {
236+
stop("The model outcome should be a factor for regression models.",
237+
call. = FALSE)
238+
}
239+
}
240+
invisible(NULL)
241+
}
242+

tests/testthat/test_linear_reg_keras.R

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,25 @@ source("helpers.R")
1010

1111
# ------------------------------------------------------------------------------
1212

13-
basic_mod <-
14-
linear_reg() %>%
13+
basic_mod <-
14+
linear_reg() %>%
1515
set_engine("keras", epochs = 50, verbose = 0)
1616

17-
ridge_mod <-
18-
linear_reg(penalty = 0.1) %>%
17+
ridge_mod <-
18+
linear_reg(penalty = 0.1) %>%
1919
set_engine("keras", epochs = 50, verbose = 0)
2020

2121
ctrl <- fit_control(verbosity = 0, catch = FALSE)
2222

2323
# ------------------------------------------------------------------------------
2424

2525
test_that('model fitting', {
26-
26+
skip_on_cran()
2727
skip_if_not_installed("keras")
28-
28+
2929
set.seed(257)
3030
expect_error(
31-
fit1 <-
31+
fit1 <-
3232
fit_xy(
3333
basic_mod,
3434
control = ctrl,
@@ -37,10 +37,10 @@ test_that('model fitting', {
3737
),
3838
regexp = NA
3939
)
40-
40+
4141
set.seed(257)
4242
expect_error(
43-
fit2 <-
43+
fit2 <-
4444
fit_xy(
4545
basic_mod,
4646
control = ctrl,
@@ -50,7 +50,7 @@ test_that('model fitting', {
5050
regexp = NA
5151
)
5252
expect_equal(fit1, fit2)
53-
53+
5454
expect_error(
5555
fit(
5656
basic_mod,
@@ -60,9 +60,9 @@ test_that('model fitting', {
6060
),
6161
regexp = NA
6262
)
63-
63+
6464
expect_error(
65-
fit1 <-
65+
fit1 <-
6666
fit_xy(
6767
ridge_mod,
6868
control = ctrl,
@@ -71,7 +71,7 @@ test_that('model fitting', {
7171
),
7272
regexp = NA
7373
)
74-
74+
7575
expect_error(
7676
fit(
7777
ridge_mod,
@@ -81,46 +81,46 @@ test_that('model fitting', {
8181
),
8282
regexp = NA
8383
)
84-
84+
8585
})
8686

8787

8888
test_that('regression prediction', {
89-
89+
skip_on_cran()
9090
skip_if_not_installed("keras")
91-
91+
9292
library(keras)
93-
93+
9494
set.seed(257)
95-
lm_fit <-
95+
lm_fit <-
9696
fit_xy(
9797
basic_mod,
9898
control = ctrl,
9999
x = iris[,2:4],
100100
y = iris$Sepal.Length
101101
)
102-
102+
103103
keras_pred <-
104104
predict(lm_fit$fit, as.matrix(iris[1:3,2:4])) %>%
105105
as_tibble() %>%
106106
setNames(".pred")
107107
parsnip_pred <- predict(lm_fit, iris[1:3,2:4])
108108
expect_equal(as.data.frame(keras_pred), as.data.frame(parsnip_pred))
109-
109+
110110
set.seed(257)
111-
rr_fit <-
111+
rr_fit <-
112112
fit_xy(
113113
ridge_mod,
114114
control = ctrl,
115115
x = iris[,2:4],
116116
y = iris$Sepal.Length
117117
)
118-
118+
119119
keras_pred <-
120120
predict(rr_fit$fit, as.matrix(iris[1:3,2:4])) %>%
121121
as_tibble() %>%
122122
setNames(".pred")
123123
parsnip_pred <- predict(rr_fit, iris[1:3,2:4])
124124
expect_equal(as.data.frame(keras_pred), as.data.frame(parsnip_pred))
125-
125+
126126
})

tests/testthat/test_linear_reg_stan.R

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ quiet_ctrl <- fit_control(verbosity = 0L, catch = TRUE)
2020
test_that('stan_glm execution', {
2121
skip_if_not_installed("rstanarm")
2222

23-
library(rstanarm)
24-
2523
expect_error(
2624
res <- fit(
2725
iris_basic,
@@ -55,7 +53,6 @@ test_that('stan_glm execution', {
5553

5654
test_that('stan prediction', {
5755
skip_if_not_installed("rstanarm")
58-
library(rstanarm)
5956

6057
uni_stan <- stan_glm(Sepal.Length ~ Sepal.Width + Petal.Width + Petal.Length, data = iris, seed = 123)
6158
uni_pred <- unname(predict(uni_stan, newdata = iris[1:5, ]))
@@ -84,7 +81,6 @@ test_that('stan prediction', {
8481

8582
test_that('stan intervals', {
8683
skip_if_not_installed("rstanarm")
87-
library(rstanarm)
8884

8985
res_xy <- fit_xy(
9086
linear_reg() %>%

0 commit comments

Comments
 (0)