Skip to content

Commit cca0260

Browse files
authored
Merge pull request #95 from jfisher-usgs/master
Merge with upstream
2 parents 2128374 + 36aeec3 commit cca0260

19 files changed

+232
-137
lines changed

DESCRIPTION

Lines changed: 2 additions & 2 deletions
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.4.3.9000
3+
Version: 0.4.4
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,
@@ -12,7 +12,7 @@ Depends:
1212
R (>= 3.4.0)
1313
Imports:
1414
checkmate,
15-
dplyr,
15+
data.table,
1616
GA,
1717
graphics,
1818
grDevices,

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ PKGSRC := $(shell basename `pwd`)
77
all: docs install check
88

99
docs:
10-
R -q -e 'devtools::document()';\
11-
R -q -e 'devtools::clean_dll()';\
10+
R -q -e 'Rd2roxygen::roxygen_and_build('\''.'\'', build=FALSE, reformat=FALSE)';\
11+
R -q -e 'pkgbuild::clean_dll()';\
1212

1313
build:
1414
cd ..;\

NAMESPACE

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ export(AddNorthArrow)
1111
export(AddPoints)
1212
export(AddScaleBar)
1313
export(AddSearchButton)
14+
export(BuildVignettes)
1415
export(BumpDisconnectCells)
1516
export(BumpRiverStage)
1617
export(CreateWebMap)
@@ -44,5 +45,6 @@ export(SetPolygons)
4445
export(SummariseBudget)
4546
export(ToScientific)
4647
import(rgdal)
48+
importFrom(data.table,data.table)
4749
importFrom(igraph,clusters)
4850
useDynLib(inlmisc, .registration=TRUE, .fixes="C_")

NEWS.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
# inlmisc 0.4.3.9000
1+
# inlmisc 0.4.4
2+
3+
- Add `BuildVignettes` function, used to build package vignettes.
4+
5+
- In `SummariseBudget` function, improve memory management.
6+
7+
- Change package imports by adding **data.table** and removing **dplyr**.
28

39
- Add `SetHinge` function, used to specify a hinge location in a color palette.
410

R/BuildVignettes.R

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#' Build Package Vignettes
2+
#'
3+
#' Build package vignettes from their sources and place in the \code{/inst/doc} folder.
4+
#'
5+
#' @param pkg 'character' string.
6+
#' Package path, by default the \link[=getwd]{working directory}.
7+
#' @param quiet 'logical' flag.
8+
#' Whether to supress most output.
9+
#' @param clean 'logical' flag.
10+
#' Whether to remove all intermediate files generated by the build.
11+
#' @param gs_quality 'character' string.
12+
#' Quality of compacted PDF files,
13+
#' see \code{\link[tools]{compactPDF}} function for details.
14+
#'
15+
#' @author J.C. Fisher, U.S. Geological Survey, Idaho Water Science Center
16+
#'
17+
#' @seealso \code{\link[tools]{buildVignettes}}
18+
#'
19+
#' @keywords utilities
20+
#'
21+
#' @export
22+
#'
23+
#' @examples
24+
#' \dontrun{
25+
#' BuildVignettes("<path/to/package>", gs_quality = "ebook")
26+
#' }
27+
#'
28+
29+
BuildVignettes <- function(pkg=".", quiet=TRUE, clean=TRUE, gs_quality=NULL) {
30+
31+
checkmate::assertFileExists(file.path(pkg, "DESCRIPTION"))
32+
checkmate::assertFlag(quiet)
33+
checkmate::assertFlag(clean)
34+
if (!is.null(gs_quality))
35+
gs_quality <- match.arg(gs_quality, c("none", "printer", "ebook", "screen"))
36+
37+
tools::buildVignettes(dir=pkg, quiet=quiet, clean=clean, tangle=TRUE)
38+
39+
v <- tools::pkgVignettes(dir=pkg, output=TRUE, source=TRUE)
40+
if (length(v) == 0) return(invisible(NULL))
41+
out <- c(v$outputs, unique(unlist(v$sources, use.names=FALSE)))
42+
43+
doc <- file.path(pkg, "inst/doc")
44+
45+
dir.create(doc, showWarnings=!quiet, recursive=TRUE)
46+
file.copy(c(v$docs, out), doc, overwrite=TRUE)
47+
file.remove(out)
48+
49+
if (!is.null(gs_quality))
50+
tools::compactPDF(paths=doc, gs_quality=gs_quality)
51+
52+
invisible(TRUE)
53+
}

R/ExportRasterStack.R

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,20 @@ ExportRasterStack <- function(rs, path, zip="", col=NULL) {
6060

6161
# check arguments
6262
stopifnot(inherits(rs, c("RasterStack", "RasterBrick")))
63-
checkmate::assertDirectoryExists(path)
63+
checkmate::assertString(path)
6464
checkmate::assertString(zip)
6565
if (zip != "") checkmate::assertFileExists(zip)
6666
checkmate::assertCharacter(col, null.ok=TRUE)
6767

68-
if (is.null(col)) col <- GetColors(255, stops=c(0.3, 0.9))
69-
7068
dir.create(path, showWarnings=FALSE, recursive=TRUE)
7169
dir.create(path.csv <- file.path(path, "csv"), showWarnings=FALSE)
7270
dir.create(path.png <- file.path(path, "png"), showWarnings=FALSE)
7371
dir.create(path.tif <- file.path(path, "tif"), showWarnings=FALSE)
7472
dir.create(path.rda <- file.path(path, "rda"), showWarnings=FALSE)
7573
dir.create(path.kml <- file.path(path, "kml"), showWarnings=FALSE)
7674

75+
if (is.null(col)) col <- GetColors(255, stops=c(0.3, 0.9))
76+
7777
n <- 0L
7878
for (i in names(rs)) {
7979
n <- n + 1L

R/GetColors.R

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,12 @@
9292
#' Generic Mapping Tools: Improved version released, AGU, v. 94, no. 45, p. 409--410
9393
#' doi:\href{https://doi.org/10.1002/2013EO450001}{10.1002/2013EO450001}
9494
#'
95-
#' @seealso \code{\link{SetHinge}}, \code{\link[grDevices]{col2rgb}}
95+
#' @seealso
96+
#' \code{\link{SetHinge}} function to set the hinge location in
97+
#' a color palette derived from one or two color schemes.
98+
#'
99+
#' \code{\link[grDevices]{col2rgb}} function to express palette
100+
#' colors represented in the hexadecimal format as RGB triplets (R, G, B).
96101
#'
97102
#' @keywords color
98103
#'

R/ReadCodeChunks.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#' @export
2626
#'
2727
#' @examples
28+
#' \dontrun{
2829
#' file <- system.file("misc", "knitr-markdown.Rmd", package = "inlmisc")
2930
#' chunks <- ReadCodeChunks(file)
3031
#'
@@ -33,6 +34,7 @@
3334
#' chunks[["named-chunk-2"]]
3435
#'
3536
#' eval(parse(text = unlist(chunks[c("unnamed-chunk-3", "named-chunk-4")])))
37+
#' }
3638
#'
3739

3840
ReadCodeChunks <- function(path) {

R/ReadModflowBinary.R

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,9 @@ ReadModflowBinary <- function(path, data.type=c("array", "flow"),
6767
endian <- match.arg(endian)
6868
checkmate::assertFlag(rm.totim.0)
6969

70-
ans <- try(.ReadBinary(path, data.type, endian, nbytes=4), silent=TRUE)
70+
ans <- try(.ReadBinary(path, data.type, endian, nbytes=4L), silent=TRUE)
7171
if (inherits(ans, "try-error"))
72-
ans <- .ReadBinary(path, data.type, endian, nbytes=8)
72+
ans <- .ReadBinary(path, data.type, endian, nbytes=8L)
7373
if (rm.totim.0)
7474
ans <- ans[vapply(ans, function(i) i$totim, 0) != 0]
7575
ans
@@ -81,7 +81,7 @@ ReadModflowBinary <- function(path, data.type=c("array", "flow"),
8181
checkmate::assertFileExists(path)
8282
checkmate::assertString(data.type)
8383
checkmate::assertString(endian)
84-
stopifnot(nbytes %in% c(4, 8))
84+
stopifnot(nbytes %in% c(4L, 8L))
8585

8686
con <- file(path, open="rb", encoding="bytes")
8787
on.exit(close(con, type="rb"))
@@ -136,33 +136,33 @@ ReadModflowBinary <- function(path, data.type=c("array", "flow"),
136136
"wells")
137137
lst <- list()
138138
repeat {
139-
kstp <- readBin(con, "integer", n=1, size=4, endian=endian)
139+
kstp <- readBin(con, "integer", n=1L, size=4L, endian=endian)
140140
if (length(kstp) == 0) break
141-
kper <- readBin(con, "integer", n=1, size=4, endian=endian)
141+
kper <- readBin(con, "integer", n=1L, size=4L, endian=endian)
142142

143143
if (data.type == "array") {
144-
pertim <- readBin(con, "numeric", n=1, size=nbytes, endian=endian)
145-
totim <- readBin(con, "numeric", n=1, size=nbytes, endian=endian)
146-
desc <- readBin(readBin(con, "raw", n=16, size=1, endian=endian),
147-
"character", n=1, endian=endian)
144+
pertim <- readBin(con, "numeric", n=1L, size=nbytes, endian=endian)
145+
totim <- readBin(con, "numeric", n=1L, size=nbytes, endian=endian)
146+
desc <- readBin(readBin(con, "raw", n=16L, size=1L, endian=endian),
147+
"character", n=1L, endian=endian)
148148
desc <- .TidyDescription(desc)
149149
if (!desc %in% valid.desc) break
150-
ncol <- readBin(con, "integer", n=1, size=4, endian=endian)
151-
nrow <- readBin(con, "integer", n=1, size=4, endian=endian)
152-
layer <- readBin(con, "integer", n=1, size=4, endian=endian)
150+
ncol <- readBin(con, "integer", n=1L, size=4L, endian=endian)
151+
nrow <- readBin(con, "integer", n=1L, size=4L, endian=endian)
152+
layer <- readBin(con, "integer", n=1L, size=4L, endian=endian)
153153
v <- readBin(con, "numeric", n=nrow * ncol, size=nbytes, endian=endian)
154154
d <- matrix(v, nrow=nrow, ncol=ncol, byrow=TRUE)
155155
lst[[length(lst) + 1]] <- list(d=d, kstp=kstp, kper=kper, desc=desc,
156156
layer=layer, pertim=pertim, totim=totim)
157157

158158
} else if (data.type == "flow") {
159-
desc <- readBin(readBin(con, "raw", n=16, size=1, endian=endian),
160-
"character", n=1, endian=endian)
159+
desc <- readBin(readBin(con, "raw", n=16L, size=1L, endian=endian),
160+
"character", n=1L, endian=endian)
161161
desc <- .TidyDescription(desc)
162162
if (!desc %in% valid.desc) break
163-
ncol <- readBin(con, "integer", n=1, size=4, endian=endian)
164-
nrow <- readBin(con, "integer", n=1, size=4, endian=endian)
165-
nlay <- readBin(con, "integer", n=1, size=4, endian=endian)
163+
ncol <- readBin(con, "integer", n=1L, size=4L, endian=endian)
164+
nrow <- readBin(con, "integer", n=1L, size=4L, endian=endian)
165+
nlay <- readBin(con, "integer", n=1L, size=4L, endian=endian)
166166

167167
if (nlay > 0) {
168168
x <- .Read3dArray(con, nrow, ncol, nlay, nbytes, endian)
@@ -173,73 +173,77 @@ ReadModflowBinary <- function(path, data.type=c("array", "flow"),
173173

174174
} else { # compact form is used
175175
nlay <- abs(nlay)
176-
itype <- readBin(con, "integer", n=1, size=4, endian=endian)
177-
delt <- readBin(con, "numeric", n=1, size=nbytes, endian=endian)
178-
pertim <- readBin(con, "numeric", n=1, size=nbytes, endian=endian)
179-
totim <- readBin(con, "numeric", n=1, size=nbytes, endian=endian)
176+
itype <- readBin(con, "integer", n=1L, size=4L, endian=endian)
177+
delt <- readBin(con, "numeric", n=1L, size=nbytes, endian=endian)
178+
pertim <- readBin(con, "numeric", n=1L, size=nbytes, endian=endian)
179+
totim <- readBin(con, "numeric", n=1L, size=nbytes, endian=endian)
180180

181-
if (itype == 5)
182-
nval <- readBin(con, "integer", n=1, size=4, endian=endian)
181+
if (itype == 5L)
182+
nval <- readBin(con, "integer", n=1L, size=4L, endian=endian)
183183
else
184184
nval <- 1L
185185
if (nval > 100) stop("more than one-hundred varaiables for each cell")
186186
if (nval > 1) {
187-
ctmp <- readBin(readBin(con, "raw", n=16, size=1, endian=endian),
188-
"character", n=nval - 1, endian=endian)
187+
ctmp <- readBin(readBin(con, "raw", n=16L, size=1L, endian=endian),
188+
"character", n=nval - 1L, endian=endian)
189189
ctmp <- .TidyDescription(ctmp)
190190
} else {
191191
ctmp <- NULL
192192
}
193193

194-
if (itype %in% c(0, 1)) {
195-
nvalues <- ncol * nrow * nlay
194+
if (itype %in% c(0L, 1L)) {
196195
d <- .Read3dArray(con, nrow, ncol, nlay, nbytes, endian)
197196
for (i in seq_along(d)) {
198197
lst[[length(lst) + 1]] <- list(d=d[[i]], kstp=kstp, kper=kper,
199198
desc=desc, layer=i, delt=delt,
200199
pertim=pertim, totim=totim)
201200
}
202201

203-
} else if (itype %in% c(2, 5)) {
204-
nlist <- readBin(con, "integer", n=1, size=4, endian=endian)
202+
} else if (itype %in% c(2L, 5L)) {
203+
nlist <- readBin(con, "integer", n=1L, size=4L, endian=endian)
205204
if (nlist > (nrow * ncol * nlay))
206205
stop("large number of cells for which values will be stored")
207206
if (nlist > 0) {
208-
d <- matrix(0, nrow=nlist, ncol=nval + 4)
207+
d <- matrix(0, nrow=nlist, ncol=nval + 4L)
209208
colnames(d) <- make.names(c("icell", "layer", "row", "column", "flow", ctmp),
210209
unique=TRUE)
211210
for (i in seq_len(nlist)) {
212-
d[i, 1] <- readBin(con, "integer", n=1, size=4, endian=endian)
211+
d[i, 1] <- readBin(con, "integer", n=1L, size=4L, endian=endian)
213212
d[i, seq_len(nval) + 4] <- readBin(con, "numeric", n=nval,
214213
size=nbytes, endian=endian)
215214
}
216215
nrc <- nrow * ncol
217-
d[, "layer"] <- as.integer((d[, "icell"] - 1) / nrc + 1)
218-
d[, "row"] <- as.integer(((d[, "icell"] - (d[, "layer"] - 1) * nrc) - 1) / ncol + 1)
219-
d[, "column"] <- as.integer(d[, "icell"] - (d[, "layer"] - 1) * nrc - (d[, "row"] - 1) * ncol)
216+
d[, "layer"] <- as.integer((d[, "icell"] - 1L) / nrc + 1L)
217+
d[, "row"] <- as.integer(((d[, "icell"] - (d[, "layer"] - 1L) * nrc)
218+
- 1L) / ncol + 1L)
219+
d[, "column"] <- as.integer(d[, "icell"] - (d[, "layer"] - 1L)
220+
* nrc - (d[, "row"] - 1L) * ncol)
220221
lst[[length(lst) + 1]] <- list(d=d, kstp=kstp, kper=kper, desc=desc,
221222
delt=delt, pertim=pertim, totim=totim)
222223
}
223224

224-
} else if (itype == 3) {
225-
layers <- readBin(con, "integer", n=nrow * ncol, size=4, endian=endian)
225+
} else if (itype == 3L) {
226+
layers <- readBin(con, "integer", n=nrow * ncol, size=4L, endian=endian)
226227
values <- readBin(con, "numeric", n=nrow * ncol, size=nbytes, endian=endian)
227228
for (i in sort(unique(layers))) {
228229
v <- values[layers == i]
229230
d <- matrix(v, nrow=nrow, ncol=ncol, byrow=TRUE)
230231
lst[[length(lst) + 1]] <- list(d=d, kstp=kstp, kper=kper, desc=desc,
231-
layer=i, delt=delt, pertim=pertim, totim=totim)
232+
layer=i, delt=delt, pertim=pertim,
233+
totim=totim)
232234
}
233235

234-
} else if (itype == 4) {
236+
} else if (itype == 4L) {
235237
v <- readBin(con, "numeric", n=nrow * ncol, size=nbytes, endian=endian)
236238
d <- matrix(v, nrow=nrow, ncol=ncol, byrow=TRUE)
237239
lst[[length(lst) + 1]] <- list(d=d, kstp=kstp, kper=kper, desc=desc,
238-
layer=1, delt=delt, pertim=pertim, totim=totim)
240+
layer=1L, delt=delt, pertim=pertim,
241+
totim=totim)
239242
d[, ] <- 0
240243
for (i in seq_len(nlay)[-1]) {
241244
lst[[length(lst) + 1]] <- list(d=d, kstp=kstp, kper=kper, desc=desc,
242-
layer=i, delt=delt, pertim=pertim, totim=totim)
245+
layer=i, delt=delt, pertim=pertim,
246+
totim=totim)
243247
}
244248

245249
} else {

R/SetHinge.R

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
#' The \emph{hinge} indicates a dramatic color change in a palette
44
#' that is typically located at the midpoint of the data range.
55
#' An asymmetrical data range can result in an undesired hinge location,
6-
#' where the location does not necessarily coincide with the break-point in the user's data.
7-
#' This function is used to specify a hinge location that is appropriate for your data.
6+
#' a location that does not necessarily coincide with the break-point in the user's data.
7+
#' This function can be used to specify a hinge location that is appropriate for your data.
88
#'
99
#' @param x 'numeric' object that can be passed to the \code{\link{range}}
1010
#' function with \code{NA}'s removed.
11-
#' That is, the user's data range (such as, at sea-level).
11+
#' The user's data range.
1212
#' @param hinge 'numeric' number.
13-
#' Hinge value in data units.
13+
#' Hinge value (such as, at sea-level) in data units.
1414
#' @param scheme 'character' vector of length 1 or 2, value is recycled as necessary.
1515
#' Name of color scheme(s).
1616
#' The color palette is derived from one or two color schemes.
@@ -29,7 +29,7 @@
2929
#' Values applied separately on either side of the hinge.
3030
#' @param stops 'numeric' vector of length 2.
3131
#' Color stops defined by interval endpoints (between 0 and 1)
32-
#' and used to select a subset of the color palette.
32+
#' and used to select a subset of the color palette(s).
3333
#' @param allow_bias 'logical' flag.
3434
#' Whether to allow bias in the color spacing.
3535
#'
@@ -181,7 +181,7 @@ SetHinge <- function(x, hinge, scheme="sunset", alpha=NULL, reverse=FALSE,
181181
s1 <- c(stp[1] + adj[1], ran - buf[1])
182182
s2 <- c(1 - ran + buf[2], 1 - stp[2] - adj[2])
183183

184-
if (s1[1] >= s1[2] | s2[1] >= s2[2])
184+
if (s1[1] >= s1[2] || s2[1] >= s2[2])
185185
stop("problem with color stops and (or) buffer values")
186186

187187
FUN <- function(...) {

0 commit comments

Comments
 (0)