From b8b4c9437bc165b56821d094510c28e36ca6469c Mon Sep 17 00:00:00 2001 From: Carson Sievert Date: Thu, 31 May 2018 15:58:45 -0500 Subject: [PATCH] add offline argument to plot_geo(); closes #356 --- DESCRIPTION | 3 ++- R/plotly.R | 19 ++++++++++++++++++- man/plot_geo.Rd | 6 +++++- 3 files changed, 25 insertions(+), 3 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 964cabb375..5f78da7aa8 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -67,7 +67,8 @@ Suggests: rgeos, png, IRdisplay, - processx + processx, + plotlyGeoAssets Remotes: tidyverse/ggplot2 LazyData: true diff --git a/R/plotly.R b/R/plotly.R index e29177f1f6..966f24ce5f 100644 --- a/R/plotly.R +++ b/R/plotly.R @@ -253,6 +253,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()] @@ -263,8 +266,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