Skip to content

Commit

Permalink
Last requireNamespace by rlang::check_installed(). Can also use `rl…
Browse files Browse the repository at this point in the history
…ang::is_installed()` if a different behavior, when packages are not installed.
  • Loading branch information
olivroy committed Sep 20, 2023
1 parent de49668 commit dd97caa
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 31 deletions.
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 @@ -40,9 +40,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 @@ -80,32 +80,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

0 comments on commit dd97caa

Please sign in to comment.