Skip to content

Commit

Permalink
Remove mockthat
Browse files Browse the repository at this point in the history
  • Loading branch information
doccstat committed Apr 15, 2024
1 parent f893771 commit f087793
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 138 deletions.
3 changes: 1 addition & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Imports:
methods,
Rcpp (>= 0.11.0),
stats,
tseries,
utils
tseries
Suggests:
abind,
breakfast,
Expand Down
4 changes: 4 additions & 0 deletions R/check.R
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
require_namespace <- function(package_name) {
requireNamespace(package_name, quietly = TRUE)
}

check_family <- function(family, allowed_families) {
error_message <- paste0(
"The family should be one of ",
Expand Down
177 changes: 83 additions & 94 deletions R/fastcpd_class.R
Original file line number Diff line number Diff line change
Expand Up @@ -66,116 +66,105 @@ plot.fastcpd <- function( # nolint: cyclomatic complexity
stop("Can not plot mean change points with p > 1.")
}
if (!require_namespace("ggplot2")) {
if (utils_menu() == 1) {
tryCatch(
expr = install_packages("ggplot2"),
error = function(e) {
stop("ggplot2 could not be installed.")
}
)
} else {
stop("ggplot2 is not installed. No plot is made.")
}
stop("ggplot2 is not installed. No plot is made.")
}
if (require_namespace("ggplot2")) {
n <- nrow(x@data)
family <- x@family
change_points <- sort(c(0, x@cp_set, n))
color <- rep(seq_along(change_points[-1]), diff(change_points))
color <- as.factor(color %% color_max_count)
n <- nrow(x@data)
family <- x@family
change_points <- sort(c(0, x@cp_set, n))
color <- rep(seq_along(change_points[-1]), diff(change_points))
color <- as.factor(color %% color_max_count)

y <- x@data[, 1]
p <- ggplot2::ggplot() +
ggplot2::geom_vline(
xintercept = x@cp_set,
color = segment_separator_color,
linetype = segment_separator_linetype,
alpha = segment_separator_alpha
)
y <- x@data[, 1]
p <- ggplot2::ggplot() +
ggplot2::geom_vline(
xintercept = x@cp_set,
color = segment_separator_color,
linetype = segment_separator_linetype,
alpha = segment_separator_alpha
)

# Draw lines for time series data and points for other data.
if (family %in% c("ar", "arma", "arima", "garch")) {
y_label <-
paste0(toupper(family), "(", paste0(x@order, collapse = ", "), ")")
} else if (family %in% c("mean", "variance", "meanvariance")) {
y_label <- "data"
} else {
y_label <- "data response"
}
# Draw lines for time series data and points for other data.
if (family %in% c("ar", "arma", "arima", "garch")) {
y_label <-
paste0(toupper(family), "(", paste0(x@order, collapse = ", "), ")")
} else if (family %in% c("mean", "variance", "meanvariance")) {
y_label <- "data"
} else {
y_label <- "data response"
}

data_label_color <- data.frame(
x = seq_len(n), y = y, label = y_label, color = color
)
residual_label_color <- data.frame(
x = seq_len(n),
y = x@residuals,
label = "residual",
color = color
)
covariate_label_color <- data.frame(
x = seq_len(n),
y = x@data[, ncol(x@data)],
label = "covariate",
color = color
)
aesthetic_mapping <- ggplot2::aes(x = x, y = y, color = color)

data_label_color <- data.frame(
x = seq_len(n), y = y, label = y_label, color = color
if (family %in% c("ar", "arma", "arima", "garch")) {
p <- p + ggplot2::geom_line(
data = data_label_color, aesthetic_mapping, alpha = data_point_alpha,
linewidth = data_point_linewidth
)
residual_label_color <- data.frame(
x = seq_len(n),
y = x@residuals,
label = "residual",
color = color
} else {
p <- p + ggplot2::geom_point(
data = data_label_color, aesthetic_mapping, alpha = data_point_alpha,
size = data_point_size
)
covariate_label_color <- data.frame(
x = seq_len(n),
y = x@data[, ncol(x@data)],
label = "covariate",
color = color
}

if (family != "var" && !x@cp_only) {
p <- p + ggplot2::geom_point(
data = residual_label_color,
aesthetic_mapping,
na.rm = TRUE,
alpha = data_point_alpha,
size = data_point_size
)
aesthetic_mapping <- ggplot2::aes(x = x, y = y, color = color)
if (ncol(x@data) == 2 || (family == "ar" && nrow(x@thetas) == 1)) {
xend <- c(x@cp_set, n)
yend <- as.numeric(x@thetas)

if (family %in% c("ar", "arma", "arima", "garch")) {
p <- p + ggplot2::geom_line(
data = data_label_color, aesthetic_mapping, alpha = data_point_alpha,
linewidth = data_point_linewidth
)
} else {
p <- p + ggplot2::geom_point(
data = data_label_color, aesthetic_mapping, alpha = data_point_alpha,
size = data_point_size
coefficient_label <- data.frame(
x = c(1, x@cp_set),
y = yend,
xend = xend,
yend = yend,
label = "coefficient"
)
}

if (family != "var" && !x@cp_only) {
p <- p + ggplot2::geom_point(
data = residual_label_color,
data = covariate_label_color,
aesthetic_mapping,
na.rm = TRUE,
alpha = data_point_alpha,
size = data_point_size
)
if (ncol(x@data) == 2 || (family == "ar" && nrow(x@thetas) == 1)) {
xend <- c(x@cp_set, n)
yend <- as.numeric(x@thetas)

coefficient_label <- data.frame(
x = c(1, x@cp_set),
y = yend,
xend = xend,
yend = yend,
label = "coefficient"
)

p <- p + ggplot2::geom_point(
data = covariate_label_color,
aesthetic_mapping,
size = data_point_size
)
p <- p + ggplot2::geom_segment(
data = coefficient_label,
ggplot2::aes(x = x, y = y, xend = xend, yend = yend),
col = "blue"
)
}
p <- p + ggplot2::facet_wrap("label", nrow = 2, scales = "free_y")
p <- p + ggplot2::geom_segment(
data = coefficient_label,
ggplot2::aes(x = x, y = y, xend = xend, yend = yend),
col = "blue"
)
}
p <- p + ggplot2::theme(
legend.position = legend_position,
panel.background = panel_background,
panel.border = panel_border,
panel.grid.major = panel_grid_major,
panel.grid.minor = panel_grid_minor,
strip.background = strip_background,
)
p <- p + ggplot2::xlab(xlab) + ggplot2::ylab(ylab)
print(p)
p <- p + ggplot2::facet_wrap("label", nrow = 2, scales = "free_y")
}
p <- p + ggplot2::theme(
legend.position = legend_position,
panel.background = panel_background,
panel.border = panel_border,
panel.grid.major = panel_grid_major,
panel.grid.minor = panel_grid_minor,
strip.background = strip_background,
)
p <- p + ggplot2::xlab(xlab) + ggplot2::ylab(ylab)
print(p)
invisible()
}

Expand Down
16 changes: 0 additions & 16 deletions R/mockthat.R

This file was deleted.

4 changes: 0 additions & 4 deletions README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@ knitr::opts_chunk$set(
fig.path = "man/figures/README-"
)
options(cli.hyperlink = FALSE, eval = TRUE)
if (!requireNamespace("ggplot2", quietly = TRUE)) utils::install.packages(
"ggplot2", repos = "https://cloud.r-project.org", quiet = TRUE
)
```

# fastcpd <a href="https://fastcpd.xingchi.li"><img src="man/figures/logo.svg" align="right" height="138" /></a>
Expand Down
22 changes: 0 additions & 22 deletions tests/testthat/test-fastcpd-class.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,32 +21,10 @@ testthat::test_that(
testthat::expect_error(
mockthat::with_mock(
`require_namespace` = function(...) FALSE,
`utils_menu` = function(...) 2,
plot(class_instance)
),
"ggplot2 is not installed. No plot is made."
)

testthat::expect_no_error(
mockthat::with_mock(
`require_namespace` = function(...) FALSE,
`utils_menu` = function(...) 1,
`install_packages` = function(...) TRUE,
plot(class_instance)
)
)

testthat::expect_error(
mockthat::with_mock(
`require_namespace` = function(...) FALSE,
`utils_menu` = function(...) 1,
`install_packages` = function(...) {
stop("ggplot2 could not be installed.")
},
plot(class_instance)
),
"ggplot2 could not be installed."
)
}
)

Expand Down

0 comments on commit f087793

Please sign in to comment.