Skip to content

Commit cc0e3b5

Browse files
committed
plot_MoranScatterplot():
+ fx plot parameter collusion + ad input verification + up tests
1 parent 4a73542 commit cc0e3b5

File tree

2 files changed

+30
-17
lines changed

2 files changed

+30
-17
lines changed

R/plot_MoranScatterplot.R

+25-12
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,9 @@ plot_MoranScatterplot <- function(
6363

6464
## Validate input arguments; set variables -----------------------
6565
.validate_class(object, c("RLum.Results", "numeric", "integer"))
66-
## To add:
67-
# - should contain a numerical vector of length 100
66+
67+
if(inherits(object, "numeric") || inherits(object, "integer") )
68+
.validate_length(object, 100)
6869

6970
## get ... arguments
7071
plot_settings <- modifyList(
@@ -96,11 +97,14 @@ plot_MoranScatterplot <- function(
9697

9798
show_location_ids <- FALSE
9899
show_n_neighbours <- FALSE
99-
if(plot_settings$pch == "show_location_ids")
100+
if(plot_settings$pch == "show_location_ids") {
100101
show_location_ids <- TRUE
101-
else if (plot_settings$pch == "show_n_neighbours")
102+
103+
} else if (plot_settings$pch == "show_n_neighbours") {
102104
show_n_neighbours <- TRUE
103105

106+
}
107+
104108
vs_log <- plot_settings$log # because we need the function "log" later
105109

106110
df_moran_plot <- data.frame(
@@ -196,14 +200,22 @@ plot_MoranScatterplot <- function(
196200
plot_settings$pch
197201
}
198202

199-
plot(df_moran_plot[,1:2],
200-
pch = vs_pch,
201-
log = vs_log,
202-
xlab = plot_settings$xlab,
203-
ylab = plot_settings$ylab,
204-
cex = plot_settings$cex,
205-
...
206-
)
203+
## plot
204+
## remove used arguments to avoid collusion
205+
args <- list(...)
206+
args <- args[!...names() %in% c("pch", "log", "xlab", "ylab", "cex", "legend")]
207+
208+
do.call(
209+
what = plot,
210+
args = c(
211+
args,
212+
list(
213+
df_moran_plot[,1:2],
214+
pch = vs_pch,
215+
log = vs_log,
216+
xlab = plot_settings$xlab,
217+
ylab = plot_settings$ylab,
218+
cex = plot_settings$cex)))
207219

208220
if(vs_points_appearance == "show_location_ids") {
209221
text(x = df_moran_plot[,1:2],
@@ -268,3 +280,4 @@ plot_MoranScatterplot <- function(
268280

269281
invisible(df_moran_plot)
270282
}
283+

tests/testthat/test_plot_MoranScatterplot.R

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ test_that("input validation", {
1111
"'df_neighbours' should be of class 'data.frame'")
1212
expect_error(plot_MoranScatterplot(obj, df_neighbours = data.frame()),
1313
"'df_neighbours' should be a data frame with 3 columns")
14-
expect_error(plot_MoranScatterplot(obj, show_legend = "error"),
15-
"'show_legend' should be of class 'logical'")
1614
expect_error(plot_MoranScatterplot(obj, str_y_def = "error"),
1715
"'str_y_def' should be one of 'mean_neighbours' or")
16+
expect_error(plot_MoranScatterplot(1:10),
17+
"'object' should have length 100")
1818
})
1919

2020
test_that("check functionality", {
@@ -30,14 +30,14 @@ test_that("check functionality", {
3030

3131
## problematic values for the log transform
3232
expect_warning(plot_MoranScatterplot(1:100 - 1, log = "xy",
33-
show_legend = FALSE),
33+
legend = FALSE),
3434
"x-axis values rescaled because of log transform")
3535
expect_warning(plot_MoranScatterplot(1:100 - 2, log = "xy",
36-
show_legend = FALSE),
36+
legend = FALSE),
3737
"x-axis values rescaled because of log transform")
3838

3939
obj.na <- obj
4040
obj.na@data$vn_values[c(24, 73)] <- NA
41-
expect_warning(plot_MoranScatterplot(obj.na, show_legend = TRUE),
41+
expect_warning(plot_MoranScatterplot(obj.na, legend = TRUE),
4242
"Grain observations removed from plot because no neighbours")
4343
})

0 commit comments

Comments
 (0)