Skip to content

Commit f845477

Browse files
author
Jordan S Read
committed
Merge pull request #309 from jiwalker-usgs/config_cleanup
Config cleanup
2 parents 768766f + 36b5bbd commit f845477

File tree

2 files changed

+52
-29
lines changed

2 files changed

+52
-29
lines changed

R/config.R

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,26 @@
11
gsconfig <- new.env(parent = emptyenv())
22

3+
overrides <- list("par" = "par",
4+
"points" = "plot.xy",
5+
"lines" = "plot.xy",
6+
"axis" = "axis",
7+
"plot" = "plot.xy",
8+
"abline" = "abline",
9+
"legend" = "legend",
10+
"title" = "title",
11+
"text" = "text",
12+
"mtext" = "mtext",
13+
"grid" = "grid",
14+
"segments" = "segments",
15+
"error_bar" = "error_bar",
16+
"arrows" = "arrows",
17+
"bgCol" = "bgCol",
18+
"callouts" = "callouts",
19+
"rect" = "rect",
20+
"polygon" = "polygon",
21+
"symbols" = "symbols",
22+
"curve" = "curve")
23+
324
#' @title Load gsplot config
425
#'
526
#' @description Loads the config file into options which are
@@ -40,12 +61,7 @@ loadConfig = function(filename) {
4061
#' @importFrom graphics par
4162
#' @export
4263
config <- function(type, ..., persist=FALSE){
43-
allowedTypes <- c("par","points","lines","axis","plot",
44-
"abline","legend","title","text",
45-
"mtext","grid","segments",
46-
"error_bar","arrows","bgCol","callouts",
47-
"rect", "polygon", "symbols",
48-
"curve", "orderToPlot")
64+
allowedTypes <- names(overrides)
4965

5066
type <- match.arg(type, choices = allowedTypes)
5167

@@ -57,30 +73,8 @@ config <- function(type, ..., persist=FALSE){
5773

5874
globalConfig <- config_list[!(names(config_list) %in% allowedTypes[allowedTypes != "par"])]
5975

60-
formalsNames <- names(formals(plot.xy))
76+
formalsNames <- formal_names(type)
6177

62-
formalsNames <- switch(type,
63-
par=names(par(no.readonly = TRUE)),
64-
axis=names(formals(graphics::axis)),
65-
legend=names(formals(graphics::legend)),
66-
abline=names(formals(graphics::abline)),
67-
title=names(formals(graphics::title)),
68-
text=names(formals(graphics::text)),
69-
mtext=names(formals(graphics::mtext)),
70-
grid=names(formals(graphics::grid)),
71-
segments=names(formals(graphics::segments)),
72-
error_bar=names(formals(error_bar.default)),
73-
bgCol=names(formals(bgCol.default)),
74-
callouts=names(formals(callouts.default)),
75-
rect=names(formals(graphics::rect)),
76-
polygon=names(formals(graphics::polygon)),
77-
symbols=names(formals(graphics::symbols)),
78-
curve=names(formals(graphics::curve)),
79-
orderToPlot='order',
80-
formalsNames)
81-
82-
formalsNames <- formalsNames[formalsNames != "..."]
83-
8478
globalConfig <- globalConfig[names(globalConfig) %in% formalsNames]
8579

8680
if(type %in% names(config_list)){
@@ -105,4 +99,23 @@ config <- function(type, ..., persist=FALSE){
10599
}
106100

107101
return(globalConfig)
102+
}
103+
104+
formal_names <- function(type) {
105+
formals <- NULL
106+
107+
if (type == "par") {
108+
formals <- names(par(no.readonly=TRUE))
109+
} else {
110+
func <- tryCatch(getFromNamespace(overrides[[type]], "graphics"), error=function(e){})
111+
112+
if (!is.null(func)) {
113+
formals <- names(formals(func))
114+
} else {
115+
formals <- names(formals(overrides[[type]]))
116+
}
117+
}
118+
formals <- formals[formals != "..."]
119+
120+
return(formals)
108121
}

tests/testthat/tests-config.R

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,13 @@ test_that("persisting to config alters environment", {
2020
expect_equal(gsplot:::config("points")$col, "blue")
2121
cleanup()
2222
})
23+
24+
test_that("formals are correctly retrieved", {
25+
expect_equal(length(config("par")), 6)
26+
expect_equal(names(config("points")), c("pch", "col"))
27+
expect_equal(length(config("arrows")), 0)
28+
})
29+
30+
test_that("non-existant type hits error", {
31+
expect_error(config("foo"))
32+
})

0 commit comments

Comments
 (0)