Skip to content

Commit 3efb2f9

Browse files
authored
Merge pull request #66 from jfisher-usgs/master
Merge with upstream
2 parents 784be0a + 12b9304 commit 3efb2f9

31 files changed

+381
-124
lines changed

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: inlmisc
22
Title: Miscellaneous Functions for the USGS INL Project Office
3-
Version: 0.3.5.9000
3+
Version: 0.4.0
44
Authors@R: person(given=c("Jason", "C."), family="Fisher", role=c("aut", "cre"), email="jfisher@usgs.gov", comment=c(ORCID="0000-0001-9032-8912"))
55
Description: A collection of functions for creating high-level graphics,
66
performing raster-based analysis, processing MODFLOW-based models,

NAMESPACE

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
export(AddClusterButton)
44
export(AddColorKey)
55
export(AddGradientLegend)
6+
export(AddHomeButton)
67
export(AddInsetMap)
78
export(AddLegend)
89
export(AddPoints)
9-
export(AddRefreshButton)
1010
export(AddScaleBar)
1111
export(AddSearchButton)
1212
export(BumpDisconnectCells)
@@ -19,6 +19,7 @@ export(ExtractAlongTransect)
1919
export(FindOptimalSubset)
2020
export(FormatPval)
2121
export(GetDaysInMonth)
22+
export(GetTolColors)
2223
export(Grid2Polygons)
2324
export(IsPackageInstalled)
2425
export(POSIXct2Character)

NEWS.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# inlmisc 0.3.5.9000
1+
# inlmisc 0.4.0
2+
3+
- Add `GetTolColors` function, used to access color palettes by Paul Tol.
24

35
- Avoid importing all functions from a package.
46

@@ -29,7 +31,7 @@
2931

3032
- In `CreateWebMap` function, remove coordinates and zoom level information from top of map.
3133

32-
- Add `AddRefreshButton`, `AddClusterButton`, and `AddSearchButton`, and `AddLegend` functions,
34+
- Add `AddHomeButton`, `AddClusterButton`, and `AddSearchButton`, and `AddLegend` functions,
3335
used to add additional web map elements.
3436

3537
- In `FindOptimalSubset` function, allow integer chromosomes to be specified for the `suggestions` argument.

R/AddGradientLegend.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
#' scientific = TRUE, strip.dim = c(1, 14))
5050
#'
5151

52-
AddGradientLegend <- function(breaks, pal, at=NULL, n=5L, labels=TRUE,
52+
AddGradientLegend <- function(breaks, pal, at=NULL, n=5, labels=TRUE,
5353
scientific=FALSE, title=NULL,
5454
loc=c("bottomleft", "topleft", "topright", "bottomright"),
5555
inset=0, strip.dim=c(2, 8)) {

R/AddPoints.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,7 @@ AddPoints <- function(x, y=NULL, z=NULL, zcol=1, crs=NULL,
401401

402402
##
403403

404-
.Map2Color <- function(x, Pal, xlim=NULL, n=100L){
404+
.Map2Color <- function(x, Pal, xlim=NULL, n=100L) {
405405
if (length(x) == 0) return(NULL)
406406
if (is.null(xlim)) xlim <- range(x)
407407
Pal(n)[findInterval(x, seq(xlim[1], xlim[2], length.out=n), all.inside=TRUE)]

R/AddWebMapElements.R

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#' Add Miscellaneous Web Map Elements
22
#'
33
#' These functions can be used to augment a \href{http://leafletjs.com/}{Leaflet} web map with additional elements.
4-
#' The \code{AddRefreshButton} function adds a button that sets the map view to the original extent.
5-
#' The \code{AddClusterButton} function adds a button that toggles marker clusters between frozen and unfrozen states.
4+
#' The \code{AddHomeButton} function adds a button that zooms to the initial map extent.
5+
#' The \code{AddClusterButton} function adds a button that toggles marker clusters on and off.
66
#' The \code{AddSearchButton} function adds a search element that may be used to locate, and move to, a marker.
77
#' And the \code{AddCircleLegend} function adds a map legend.
88
#'
@@ -60,7 +60,7 @@
6060
#' map <- CreateWebMap("Topo")
6161
#' map <- leaflet::addMarkers(map, label = ~name, popup = ~name, clusterOptions = opt,
6262
#' clusterId = "cluster", group = "marker", data = spdf)
63-
#' map <- AddRefreshButton(map)
63+
#' map <- AddHomeButton(map)
6464
#' map <- AddClusterButton(map, clusterId = "cluster")
6565
#' map <- AddSearchButton(map, group = "marker", zoom = 15,
6666
#' textPlaceholder = "Search city names...")
@@ -83,7 +83,7 @@ NULL
8383
#' @rdname AddWebMapElements
8484
#' @export
8585

86-
AddRefreshButton <- function(map, extent=NULL, position="topleft") {
86+
AddHomeButton <- function(map, extent=NULL, position="topleft") {
8787

8888
# check arguments
8989
checkmate::assertClass(map, c("leaflet", "htmlwidget"))
@@ -99,8 +99,8 @@ AddRefreshButton <- function(map, extent=NULL, position="topleft") {
9999
js <- sprintf("function(btn, map) {
100100
map.fitBounds([[%f, %f],[%f, %f]]);
101101
}", e[3], e[1], e[4], e[2])
102-
button <- leaflet::easyButton(icon="fa-refresh",
103-
title="Refresh view",
102+
button <- leaflet::easyButton(icon="fa-home fa-lg",
103+
title="Zoom to initial map extent",
104104
onClick=htmlwidgets::JS(js),
105105
position=position)
106106

@@ -122,26 +122,26 @@ AddClusterButton <- function(map, clusterId, position="topleft") {
122122
# Javascript derived from https://rstudio.github.io/leaflet/morefeatures.html
123123
# accessed on 2017-11-06.
124124

125-
# unfrozen state
125+
# disable clusters
126126
js <- sprintf("function(btn, map) {
127127
var clusterManager = map.layerManager.getLayer('cluster', '%s');
128-
clusterManager.freezeAtZoom();
129-
btn.state('frozen-markers');
128+
clusterManager.disableClustering();
129+
btn.state('disable-cluster');
130130
}", clusterId)
131-
s0 <- leaflet::easyButtonState(stateName="unfrozen-markers",
132-
icon="fa-circle-o",
133-
title="Freeze clusters",
131+
s0 <- leaflet::easyButtonState(stateName="enable-cluster",
132+
icon="fa-circle",
133+
title="Disable clustering",
134134
onClick=htmlwidgets::JS(js))
135135

136-
# frozen state
136+
# enable clusters
137137
js <- sprintf("function(btn, map) {
138138
var clusterManager = map.layerManager.getLayer('cluster', '%s');
139-
clusterManager.unfreeze();
140-
btn.state('unfrozen-markers');
139+
clusterManager.enableClustering();
140+
btn.state('enable-cluster');
141141
}", clusterId)
142-
s1 <- leaflet::easyButtonState(stateName="frozen-markers",
143-
icon="fa-circle",
144-
title="Unfreeze clusters",
142+
s1 <- leaflet::easyButtonState(stateName="disable-cluster",
143+
icon="fa-circle-o",
144+
title="Enable clustering",
145145
onClick=htmlwidgets::JS(js))
146146

147147
# create button
@@ -182,11 +182,12 @@ AddSearchButton <- function(map, group, propertyName="label", zoom=NULL,
182182

183183
.SearchDependencies <- function() {
184184
src <- system.file("htmlwidgets/plugins/leaflet-search", package="inlmisc")
185+
css <- if (utils::packageVersion("leaflet") < 2) "leaflet-search-old.css" else "leaflet-search.css"
185186
list(htmltools::htmlDependency(name="leaflet-search",
186-
version="2.3.7",
187+
version="2.4.0",
187188
src=src,
188189
script=c("leaflet-search.min.js", "leaflet-search-binding.js"),
189-
stylesheet="leaflet-search.min.css"))
190+
stylesheet=css))
190191
}
191192

192193

R/ExportRasterStack.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
#' Therefore, the conversion of gridded data between cartographic projections may introduce a new source of error.
2525
#'
2626
#' To install \file{zip.exe} on windows, download the latest binary version from the
27-
#' \href{http://www.info-zip.org/Zip.html#Downloads}{Info-ZIP} website;
27+
#' \href{https://www.7-zip.org/download.html}{Info-ZIP} website;
2828
#' select one of the given FTP locations, enter directory \file{win32}, download \file{zip300xn.zip}, and extract.
2929
#'
3030
#' @return Used for the side-effect files written to disk.

R/ExtractAlongTransect.R

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -150,9 +150,8 @@ ExtractAlongTransect <- function(transect, r) {
150150
v.d <- c(v.d, dist.along.transect[i, i + 1L])
151151
}
152152

153-
FUN <- function(s) {
153+
return(lapply(segs, function(s) {
154154
sp::SpatialPointsDataFrame(s[, 1:2], data.frame(s[, -(1:2)], row.names=NULL),
155155
proj4string=crs, match.ID=FALSE)
156-
}
157-
return(lapply(segs, FUN))
156+
}))
158157
}

R/FindOptimalSubset.R

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -117,9 +117,9 @@
117117
#' }
118118
#'
119119

120-
FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
121-
migrationRate=0.1, migrationInterval=10L,
122-
pcrossover=0.8, pmutation=0.1, elitism=0L,
120+
FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100,
121+
migrationRate=0.1, migrationInterval=10,
122+
pcrossover=0.8, pmutation=0.1, elitism=0,
123123
maxiter=1000L, run=maxiter, suggestions=NULL,
124124
parallel=TRUE, seed=NULL) {
125125

@@ -159,8 +159,10 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
159159
set.seed(seed); m <- t(apply(m, 1, sample, size=k))
160160
} else if (k > ncol(m)) {
161161
idxs <- seq_len(n)
162-
FUN <- function(i) c(i, sample(idxs[-i], k - ncol(m)))
163-
set.seed(seed); m <- t(apply(m, 1, FUN))
162+
set.seed(seed)
163+
m <- t(apply(m, 1, function(i) {
164+
c(i, sample(idxs[-i], k - ncol(m)))
165+
}))
164166
}
165167
suggestions <- t(apply(m, 1, function(i) EncodeChromosome(i, n)))
166168
}
@@ -197,8 +199,9 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
197199
})
198200

199201
# decode solution
200-
FUN <- function(i) sort(DecodeChromosome(i, n))
201-
m <- t(apply(ga_output@solution, 1, FUN))
202+
m <- t(apply(ga_output@solution, 1, function(i) {
203+
sort(DecodeChromosome(i, n))
204+
}))
202205
solution <- m[!duplicated(m), , drop=FALSE]
203206

204207
# bundle output
@@ -237,8 +240,9 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
237240
.Crossover <- function(object, parents, n) {
238241
fitness_parents <- object@fitness[parents]
239242
encoded_parents <- object@population[parents, , drop=FALSE]
240-
FUN <- function(i) DecodeChromosome(i, n)
241-
decoded_parents <- t(apply(encoded_parents, 1, FUN))
243+
decoded_parents <- t(apply(encoded_parents, 1, function(i) {
244+
DecodeChromosome(i, n)
245+
}))
242246
p1 <- decoded_parents[1, ]
243247
p2 <- decoded_parents[2, ]
244248
c1 <- p1
@@ -249,8 +253,9 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
249253
c1[i1] <- p2[i1]
250254
c2[i2] <- p1[i2]
251255
decoded_children <- rbind(c1, c2)
252-
FUN <- function(i) EncodeChromosome(i, n)
253-
encoded_children <- t(apply(decoded_children, 1, FUN))
256+
encoded_children <- t(apply(decoded_children, 1, function(i) {
257+
EncodeChromosome(i, n)
258+
}))
254259
m <- t(apply(object@population, 1, function(i) sort(DecodeChromosome(i, n))))
255260
FindFitness <- function(child) {
256261
return(object@fitness[which(apply(m, 1, function(i) identical(i, child)))[1]])
@@ -297,15 +302,17 @@ FindOptimalSubset <- function(n, k, Fitness, ..., popSize=100L,
297302

298303
EncodeChromosome <- function(x, n) {
299304
width <- ceiling(log2(n + 1))
300-
FUN <- function(i) GA::decimal2binary(i, width)
301-
return(unlist(lapply(x, FUN)))
305+
return(unlist(lapply(x, function(i) {
306+
GA::decimal2binary(i, width)
307+
})))
302308
}
303309

304310
#' @rdname EncodeChromosome
305311
#' @export
306312

307313
DecodeChromosome <- function(y, n) {
308314
width <- ceiling(log2(n + 1))
309-
FUN <- function(i) GA::binary2decimal(y[i:(i + width - 1L)])
310-
return(vapply(seq(1, length(y), by=width), FUN, 0))
315+
return(vapply(seq(1, length(y), by=width), function(i) {
316+
GA::binary2decimal(y[i:(i + width - 1L)])
317+
}, 0))
311318
}

R/GetTolColors.R

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
#' Color Palette for Qualitative Data
2+
#'
3+
#' This function creates a vector of \code{n} contiguous colors from color schemes by Paul Tol (2012).
4+
#'
5+
#' @param n 'integer'.
6+
#' Number of colors to be in the palette, the maximum is 21.
7+
#' @param alpha 'numeric'.
8+
#' Alpha transparency, parameter values range from 0 (fully transparent) to 1 (fully opaque).
9+
#' Specify as \code{NULL} to exclude the alpha channel color component.
10+
#' @param plot 'logical'.
11+
#' Whether to display the color palette.
12+
#'
13+
#' @return Returns a 'character' vector of length \code{n} with elements of 7 or 9 characters,
14+
#' "#" followed by the red, blue, green, and optionally alpha values in hexadecimal.
15+
#'
16+
#' @author J.C. Fisher, U.S. Geological Survey, Idaho Water Science Center
17+
#'
18+
#' @references
19+
#' Tol, Paul, 2012, Colour Schemes:
20+
#' SRON Technical Note, doc. no. SRON/EPS/TN/09-002, issue 2.2, 16 p.,
21+
#' accesed January 26, 2018 at \url{https://personal.sron.nl/~pault/colourschemes.pdf}.
22+
#'
23+
#' @keywords color
24+
#'
25+
#' @export
26+
#'
27+
#' @examples
28+
#' GetTolColors(7, plot = TRUE)
29+
#'
30+
#' GetTolColors(21, alpha = 0.85, plot = TRUE)
31+
#'
32+
33+
GetTolColors <- function(n, alpha=1, plot=FALSE) {
34+
35+
checkmate::assertInt(n, lower=1, upper=21)
36+
checkmate::assertNumber(alpha, lower=0, upper=1, finite=TRUE, null.ok=TRUE)
37+
checkmate::assertFlag(plot)
38+
39+
# color schemes copied from Peter Carl's blog post, accessed January 26, 2018 at
40+
# https://tradeblotter.wordpress.com/2013/02/28/the-paul-tol-21-color-salute/
41+
pal <- list(c("#4477AA"),
42+
c("#4477AA", "#CC6677"),
43+
c("#4477AA", "#DDCC77", "#CC6677"),
44+
c("#4477AA", "#117733", "#DDCC77", "#CC6677"),
45+
c("#332288", "#88CCEE", "#117733", "#DDCC77", "#CC6677"),
46+
c("#332288", "#88CCEE", "#117733", "#DDCC77", "#CC6677", "#AA4499"),
47+
c("#332288", "#88CCEE", "#44AA99", "#117733", "#DDCC77", "#CC6677", "#AA4499"),
48+
c("#332288", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#CC6677", "#AA4499"),
49+
c("#332288", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#CC6677", "#882255", "#AA4499"),
50+
c("#332288", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#661100", "#CC6677", "#882255", "#AA4499"),
51+
c("#332288", "#6699CC", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#661100", "#CC6677", "#882255", "#AA4499"),
52+
c("#332288", "#6699CC", "#88CCEE", "#44AA99", "#117733", "#999933", "#DDCC77", "#661100", "#CC6677", "#AA4466", "#882255", "#AA4499"),
53+
c("#882E72", "#B178A6", "#D6C1DE", "#1965B0", "#5289C7", "#7BAFDE", "#4EB265", "#90C987", "#CAE0AB", "#F7EE55", "#F6C141", "#F1932D", "#E8601C"),
54+
c("#882E72", "#B178A6", "#D6C1DE", "#1965B0", "#5289C7", "#7BAFDE", "#4EB265", "#90C987", "#CAE0AB", "#F7EE55", "#F6C141", "#F1932D", "#E8601C", "#DC050C"),
55+
c("#114477", "#4477AA", "#77AADD", "#117755", "#44AA88", "#99CCBB", "#777711", "#AAAA44", "#DDDD77", "#771111", "#AA4444", "#DD7777", "#771144", "#AA4477", "#DD77AA"),
56+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122"),
57+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455"),
58+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455", "#DD7788"),
59+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#117744", "#44AA77", "#88CCAA", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122"),
60+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#117744", "#44AA77", "#88CCAA", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455"),
61+
c("#771155", "#AA4488", "#CC99BB", "#114477", "#4477AA", "#77AADD", "#117777", "#44AAAA", "#77CCCC", "#117744", "#44AA77", "#88CCAA", "#777711", "#AAAA44", "#DDDD77", "#774411", "#AA7744", "#DDAA77", "#771122", "#AA4455", "#DD7788"))
62+
col <- pal[[n]]
63+
64+
if (is.finite(alpha)) col <- grDevices::adjustcolor(col, alpha.f=alpha)
65+
66+
if (plot) {
67+
graphics::plot.default(0, 0, type="n", xlim=c(0, 1), ylim=c(0, 1), axes=FALSE, xlab="", ylab="")
68+
graphics::rect(0:(n - 1) / n, 0, 1:n / n, 1, col=col, border="#D3D3D3")
69+
}
70+
71+
return(col)
72+
}

0 commit comments

Comments
 (0)