diff --git a/DESCRIPTION b/DESCRIPTION index db923c0b58..7dad4ccddb 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -69,6 +69,7 @@ Suggests: png, IRdisplay, processx, + plotlyGeoAssets, rstudioapi Remotes: tidyverse/ggplot2 diff --git a/R/plotly.R b/R/plotly.R index f072698502..7119a0fb78 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -257,6 +257,9 @@ plot_mapbox <- function(data = data.frame(), ...) { #' #' @param data A data frame (optional). #' @param ... arguments passed along to [plot_ly()]. +#' @param offline whether or not to include geo assets so that the map +#' can be viewed with or without an internet connection. The plotlyGeoAssets +#' package is required for this functionality. #' @export #' @author Carson Sievert #' @seealso [plot_ly()], [plot_mapbox()], [ggplotly()] @@ -267,8 +270,22 @@ plot_mapbox <- function(data = data.frame(), ...) { #' plot_geo(x = ~long, y = ~lat) %>% #' add_markers(size = I(1)) #' -plot_geo <- function(data = data.frame(), ...) { +plot_geo <- function(data = data.frame(), ..., offline = FALSE) { p <- plot_ly(data, ...) + + if (isTRUE(offline)) { + if (system.file(package = "plotlyGeoAssets") == "") { + stop( + "The plotlyGeoAssets package is required to make 'offline' maps. ", + "Please install and try again.", + call. = FALSE + ) + } + p$dependencies <- c( + list(plotlyGeoAssets::geo_assets()), + p$dependencies + ) + } # not only do we use this for is_geo(), but also setting the layout attr # https://plot.ly/r/reference/#layout-geo p$x$layout$mapType <- "geo" diff --git a/man/plot_geo.Rd b/man/plot_geo.Rd index 5669676d8d..e851ea7e97 100644 --- a/man/plot_geo.Rd +++ b/man/plot_geo.Rd @@ -4,12 +4,16 @@ \alias{plot_geo} \title{Initiate a plotly-geo object} \usage{ -plot_geo(data = data.frame(), ...) +plot_geo(data = data.frame(), ..., offline = FALSE) } \arguments{ \item{data}{A data frame (optional).} \item{...}{arguments passed along to \code{\link[=plot_ly]{plot_ly()}}.} + +\item{offline}{whether or not to include geo assets so that the map +can be viewed with or without an internet connection. The plotlyGeoAssets +package is required for this functionality.} } \description{ Use this function instead of \code{\link[=plot_ly]{plot_ly()}} to initialize