Skip to content

Commit

Permalink
Merge pull request #787 from olivroy/req-namespace
Browse files Browse the repository at this point in the history
Use check_installed() to handle suggests
  • Loading branch information
mtennekes authored Sep 22, 2023
2 parents 14a61e4 + 436d303 commit 222853e
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 69 deletions.
71 changes: 36 additions & 35 deletions R/theme_ps.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,39 +8,40 @@
#' @param plot.axes should the axes be shown?
#' @param plot.legend should the legend(s) be shown?
#' @export
theme_ps <- function(base_size = 12, base_family = "", plot.axes=FALSE, plot.legend=FALSE) {
if (!requireNamespace("ggplot2", quietly = TRUE)) {
stop("ggplot2 package needed for this function to work. Please install it.",
call. = FALSE)
} else {
replace <- function(e1, e2) {
e1[names(e2)] <- e2
e1
}

tps <- replace(ggplot2::theme_minimal(base_size = base_size, base_family = base_family),
ggplot2::theme(panel.background = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
strip.background = ggplot2::element_blank(),
plot.background = ggplot2::element_blank(),
plot.title = ggplot2::element_blank(),
strip.text = ggplot2::element_blank(),
panel.spacing = unit(0,"null"),
plot.margin = rep(unit(0,"null"),4)))

if (!plot.legend) {
tps <- replace(tps, ggplot2::theme(legend.position = "none"))
}

if (plot.axes) {
tps
} else {
replace(tps,
ggplot2::theme(axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_line(),
axis.ticks.length = unit(0, "lines")))
}
}
theme_ps <- function(base_size = 12, base_family = "", plot.axes = FALSE, plot.legend = FALSE) {
rlang::check_installed("ggplot2", reason = "to create plot themes.")
replace <- function(e1, e2) {
e1[names(e2)] <- e2
e1
}
tps <- replace(
ggplot2::theme_minimal(base_size = base_size, base_family = base_family),
ggplot2::theme(
panel.background = ggplot2::element_blank(),
panel.border = ggplot2::element_blank(),
panel.grid = ggplot2::element_blank(),
strip.background = ggplot2::element_blank(),
plot.background = ggplot2::element_blank(),
plot.title = ggplot2::element_blank(),
strip.text = ggplot2::element_blank(),
panel.spacing = unit(0, "null"),
plot.margin = rep(unit(0, "null"), 4)
)
)
if (!plot.legend) {
tps <- replace(tps, ggplot2::theme(legend.position = "none"))
}
if (plot.axes) {
tps
} else {
replace(
tps,
ggplot2::theme(
axis.text = ggplot2::element_blank(),
axis.title = ggplot2::element_blank(),
axis.ticks = ggplot2::element_line(),
axis.ticks.length = unit(0, "lines")
)
)
}
}
3 changes: 1 addition & 2 deletions R/tmapGridAux.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ findZoom = function(b) {

tmapGridTilesPrep = function(a, bs, id, o) {
g = get("g", envir = .TMAP_GRID)

if (!requireNamespace("maptiles")) stop("maptiles package is required", call. = FALSE)
rlang::check_installed("maptiles")


crs = sf::st_crs(bs[[1]])
Expand Down
3 changes: 1 addition & 2 deletions R/tmapGridInit.R
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
tmapGridInit = function(o, return.asp = FALSE, vp) {
if (!requireNamespace("grid")) stop("grid package required but not installed yet.")

rlang::check_installed("grid")

rows = with(o, {
x = c(outer.margins.top = outer.margins[3],
Expand Down
1 change: 0 additions & 1 deletion R/tmapLeafletInit.R
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ addOverlayGroup <- function(group, are.tiles = FALSE) {

tmapLeafletInit = function(o, return.asp = FALSE, vp) {
if (return.asp) return(1)
if (!requireNamespace("leaflet")) stop("leaflet package required but not installed yet.")

per_page = rep(o$ncols * o$nrows, o$npages)
k = o$ncols * o$nrows * o$npages
Expand Down
2 changes: 1 addition & 1 deletion R/tmapShape.R
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ tmapShape.Raster = function(shp, is.main, crs, bbox, unit, filter, shp_name, sme
#' @export
tmapShape.SpatRaster = function(shp, is.main, crs, bbox, unit, filter, shp_name, smeta, o, tmf) {
#tmapShape.stars(stars::st_as_stars(shp), is.main, crs, bbox, unit, filter, shp_name)
if (!requireNamespace("terra")) stop("terra package needed", call. = FALSE)
rlang::check_installed("terra")

#if (!inherits(bbox, "bbox")) bbox = sf::st_bbox(bbox)

Expand Down
2 changes: 1 addition & 1 deletion R/tmapTrans.R
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ tmapTransCartogram = function(shpTM, area, ord__, plot.order, args) {

x = sf::st_sf(geometry = s, weight = area, tmapID__ = shpTM$tmapID)

if (!requireNamespace("cartogram")) stop("cartogram pacakge needed", call. = FALSE)
rlang::check_installed("cartogram")

if (args$type == "cont") {
shp = suppressMessages(suppressWarnings({cartogram::cartogram_cont(x, weight = "weight", itermax = args$itermax)}))
Expand Down
4 changes: 2 additions & 2 deletions R/tmap_animation.R
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ tmap_animation <- function(tm, filename = NULL, width = NA, height = NA, dpi = N

# check system requirements
if (gif) {
if (!requireNamespace("gifski", quietly = TRUE)) stop("Package gifski is required for gif animations but not installed.")
rlang::check_installed("gifski", reason = "for creating gif animations.")
} else {
if (!requireNamespace("av", quietly = TRUE)) stop("Package av is required for ffmpeg animations but not installed.")
rlang::check_installed("av", reason = "for ffmpeg animations")
}

if (is.na(dpi)) dpi <- .tmapOptions$output.dpi.animation
Expand Down
40 changes: 18 additions & 22 deletions R/tmap_icons.R
Original file line number Diff line number Diff line change
Expand Up @@ -86,32 +86,28 @@ marker_icon <- function() {

# fix.borders is needed to prevent that right-hand side and bottom edges disappear
pngGrob <- function(file, fix.borders=FALSE, n=NULL, height.inch=NULL, target.dpi=NULL) {
if (!requireNamespace("png", quietly = TRUE)) {
stop("png package needed for this function to work. Please install it.",
call. = FALSE)
} else {
pu <- is_path_or_url(file)
if (is.na(pu)) {
stop(file, " is neither a valid path nor url", call.=FALSE)
}
rlang::check_installed("png", reason = "for this function to work.")
pu <- is_path_or_url(file)
if (is.na(pu)) {
stop(file, " is neither a valid path nor url", call.=FALSE)
}

if (!pu) {
tmpfile <- tempfile(fileext=".png")
download.file(file, destfile=tmpfile, mode="wb")
file <- tmpfile
}
if (!pu) {
tmpfile <- tempfile(fileext=".png")
download.file(file, destfile=tmpfile, mode="wb")
file <- tmpfile
}

x <- png::readPNG(file)
x <- png::readPNG(file)

if (fix.borders) {
if (dim(x)[3]==3) {
x <- array(c(x, rep(1, dim(x)[1]*dim(x)[2])), dim = c(dim(x)[1], dim(x)[2], dim(x)[3]+1))
}
x2 <- add_zero_borders_to_3d_array(x, n=n, height.inch=height.inch,target.dpi=target.dpi)
rasterGrob(x2, interpolate=TRUE)
} else {
rasterGrob(x, interpolate=TRUE)
if (fix.borders) {
if (dim(x)[3]==3) {
x <- array(c(x, rep(1, dim(x)[1]*dim(x)[2])), dim = c(dim(x)[1], dim(x)[2], dim(x)[3]+1))
}
x2 <- add_zero_borders_to_3d_array(x, n=n, height.inch=height.inch,target.dpi=target.dpi)
rasterGrob(x2, interpolate=TRUE)
} else {
rasterGrob(x, interpolate=TRUE)
}
}

Expand Down
3 changes: 0 additions & 3 deletions _pkgdown.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,3 @@ template:
bootstrap: 5
bslib:
primary: "#5f8640"

development:
mode: auto

0 comments on commit 222853e

Please sign in to comment.