Skip to content

Commit

Permalink
added r2d3map & template
Browse files Browse the repository at this point in the history
  • Loading branch information
pvictor committed Jun 1, 2018
1 parent f70dd51 commit e27a48d
Show file tree
Hide file tree
Showing 8 changed files with 258 additions and 2 deletions.
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: r2d3maps
Version: 0.0.0.9200
Version: 0.0.0.9300
Title: Create Maps with D3
Description: Tools to create interactive maps in D3 with 'r2d3'.
Authors@R: c(
Expand All @@ -20,6 +20,7 @@ Imports:
scales,
classInt,
glue,
magrittr
magrittr,
rstudioapi
Suggests: rnaturalearth, sf
RoxygenNote: 6.0.1
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ export(add_legend)
export(add_tooltip)
export(add_zoom)
export(d3_map)
export(r2d3map)
export(use_r2d3map)
importFrom(classInt,classIntervals)
importFrom(geojsonio,geo2topo)
importFrom(geojsonio,geojson_json)
Expand All @@ -20,7 +22,10 @@ importFrom(glue,glue)
importFrom(glue,glue_data)
importFrom(htmltools,htmlDependency)
importFrom(magrittr,"%>%")
importFrom(r2d3,default_sizing)
importFrom(r2d3,r2d3)
importFrom(rstudioapi,isAvailable)
importFrom(rstudioapi,navigateToFile)
importFrom(scales,brewer_pal)
importFrom(scales,col_numeric)
importFrom(scales,div_gradient_pal)
Expand Down
103 changes: 103 additions & 0 deletions R/r2d3map.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@

#' Wrapper of `r2d3` to create maps
#'
#' @param data An `sf` or `sp` object to convert to topojson.
#' @param script JavaScript file containing the D3 script.
#' @param css CSS file containing styles. The default value "auto" will use any CSS file
#' located alongside the script file with the same stem (e.g. "barplot.css" would be
#' used for "barplot.js") as well as any CSS file with the name "styles.css".
#' @param options Options to be passed to D3 script.
#' @param container The 'HTML' container of the D3 output.
#' @param elementId Use an explicit element ID for the widget (rather than an
#' automatically generated one). Useful if you have other JavaScript that needs to
#' explicitly discover and interact with a specific widget instance.
#' @param d3_version Major D3 version to use, the latest minor version is automatically
#' picked.
#' @param dependencies Additional HTML dependencies. These can take the form of paths to
#' JavaScript or CSS files, or alternatively can be fully specified dependencies created
#' with [htmltools::htmlDependency].
#' @param width Desired width for output widget.
#' @param height Desired height for output widget.
#' @param sizing Widget sizing policy (see [htmlwidgets::sizingPolicy]).
#' @param viewer "internal" to use the RStudio internal viewer pane for output; "external"
#' to display in an external RStudio window; "browser" to display in an external
#' browser.
#'
#' @export
#' @importFrom r2d3 r2d3 default_sizing
#' @importFrom geojsonio geojson_json geo2topo
#' @importFrom htmltools htmlDependency
#'
#' @examples
#' # todo
r2d3map <- function(data, script, css = "auto", dependencies = NULL, options = NULL,
d3_version = c("5", "4", "3"), container = "svg", elementId = NULL,
width = NULL, height = NULL, sizing = default_sizing(),
viewer = c("internal", "external", "browser")) {
# convert to geojson
suppressWarnings({
data <- geojson_json(input = data)
})
# convert to topojson
data <- geo2topo(x = data, object_name = "states")
# r2d3 call
r2d3(
data = data, script = script, css = css,
dependencies = list(
htmlDependency(
name = "topojson", version = "3.0.2",
src = system.file("js", package = "r2d3maps"),
script = "topojson.min.js"
),
dependencies
),
options = options, d3_version = d3_version, container = container,
elementId = elementId, width = width, height = height, sizing = sizing,
viewer = viewer
)
}


#' Use r2d3 template to create you D3 map
#'
#' @param path Path to a script R to create.
#'
#' @export
#'
#' @importFrom glue glue
#' @importFrom rstudioapi isAvailable navigateToFile
#'
#' @examples
#' \dontrun{
#'
#' # todo
#'
#' }
use_r2d3map <- function(path = "my_map.R") {
dir_script <- dirname(path)
path <- normalizePath(path, mustWork = FALSE)
name <- basename(path)
name <- gsub(pattern = "\\..+$", replacement = "", x = name)
country <- c("Switzerland", "Cameroon", "Bolivia", "Denmark", "Madagascar", "Nigeria", "Nepal", "Togo", "Indonesia")
country <- sample(country, 1)
script <- readLines(con = system.file("template/map/map.R", package = "r2d3maps"))
script <- paste(script, collapse = "\n")
script <- glue::glue(script, country = country, name = paste(dir_script, name, sep = "/"))
con <- file(path, encoding = "utf-8")
on.exit(close(con), add = TRUE)
cat(script, file = con, sep = "\n")
file.copy(
from = system.file("template/map/map.js", package = "r2d3maps"),
to = file.path(dirname(path), paste0(name, ".js"))
)
file.copy(
from = system.file("template/map/map.css", package = "r2d3maps"),
to = file.path(dirname(path), paste0(name, ".css"))
)
if (rstudioapi::isAvailable()) {
rstudioapi::navigateToFile(file = file.path(dirname(path), paste0(name, ".js")))
rstudioapi::navigateToFile(file = path)
}
invisible(TRUE)
}

20 changes: 20 additions & 0 deletions inst/template/map/map.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@


# D3 map ------------------------------------------------------------------


# Packages ----
library(r2d3maps)
library(rnaturalearth)


# Data ----
{country} <- ne_states(country = "{country}", returnclass = "sf")


# Map ----
r2d3map(
data = {country},
script = "{name}.js"
)

17 changes: 17 additions & 0 deletions inst/template/map/map.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

.feature {
fill: #ccc;
}

.mesh {
fill: none;
stroke: #fff;
stroke-width: .5px;
stroke-linejoin: round;
}

.outline {
fill: #ddd;
stroke: #000;
stroke-width: 1.5px;
}
38 changes: 38 additions & 0 deletions inst/template/map/map.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@

// D3 maps


r2d3.onRender(function(json, svg, width, height, options) {

var projection = d3.geoMercator();

var path = d3.geoPath()
.projection(projection);

svg.attr("width", width)
.attr("height", height);

var states = topojson.feature(json, json.objects.states);

projection
.scale(1)
.translate([0, 0]);

var b = path.bounds(states),
s = 0.95 / Math.max((b[1][0] - b[0][0]) / width, (b[1][1] - b[0][1]) / height),
t = [(width - s * (b[1][0] + b[0][0])) / 2, (height - s * (b[1][1] + b[0][1])) / 2];

projection
.scale(s)
.translate(t);

svg.append("path")
.datum(states)
.attr("class", "feature")
.attr("d", path);

svg.append("path")
.datum(topojson.mesh(json, json.objects.states, function(a, b) { return a !== b; }))
.attr("class", "mesh")
.attr("d", path);
});
51 changes: 51 additions & 0 deletions man/r2d3map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions man/use_r2d3map.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit e27a48d

Please sign in to comment.