SymbolixAU / mapdeck
1
## from htmltools::htmlDependency()
2
createHtmlDependency <- function(name, version, src, script = NULL, stylesheet = NULL, all_files = FALSE) {
3 1
	structure(
4 1
		list(
5 1
			name = name
6 1
			, version = version
7 1
			, src = list( file = src )
8 1
			, meta = NULL
9 1
			, script = script
10 1
			, stylesheet = stylesheet
11 1
			, head = NULL
12 1
			, attachment = NULL
13 1
			, package = NULL
14 1
			, all_files = all_files
15
		)
16 1
		, class = "html_dependency"
17
	)
18
}
19

20

21
addDependency <- function(map, dependencyFunction) {
22

23 1
	existingDeps <- sapply(map$dependencies, function(x) x[['name']])
24 1
	addingDependency <- sapply(dependencyFunction, function(x) x[['name']])
25

26 1
	if(!addingDependency %in% existingDeps)
27 1
		map$dependencies <- c(map$dependencies, dependencyFunction)
28

29 1
	return(map)
30
}
31

32
#' Add Dependencies
33
#'
34
#' Adds the required mapdeck javascript dependencies to a map when not using a mapdeck map.
35
#'
36
#' @param map the map object to which dependencies will be added
37
#'
38
#' @examples
39
#' \donttest{
40
#'
41
#' ## use with a google map from googleway
42
#' library(googleway)
43
#'
44
#' set_key("GOOGLE_MAP_KEY")
45
#'
46
#' google_map() %>%
47
#' 	add_dependencies() %>%
48
#' 	add_scatterplot(
49
#' 		data = capitals
50
#' 		, lon = "lon"
51
#' 		, lat = "lat"
52
#' 		, fill_colour = "country"
53
#' 		, radius = 10000
54
#' 	)
55
#'
56
#' }
57
#'
58
#'
59
#'
60
#' @export
61
add_dependencies <- function( map ) {
62 1
	map$dependencies <- unique( c( map$dependencies, mapdeck_dependencies() ) )
63 1
	return( map )
64
}
65

66
#' Mapdeck Dependencies
67
#'
68
#' Adds the required mapdeck javascript dependencies to a Shiny UI when you want to use
69
#' mapdeck layers, but not with a mapdeck map.
70
#'
71
#'
72
#'
73
#' @export
74
mapdeck_dependencies <- function() {
75 1
	c(
76 1
		mapdeck_dep_functions()
77 1
		, deckgl_min_js()
78 1
		, mapdeck_dep_legend()
79 1
		, mapdeck_dep_title()
80 1
		, mapdeck_dep_location()
81 1
		, mapdeck_dep_colours()
82 1
		, mapdeck_dep_coordinates()
83
		)
84
}
85

86
htmlwidgets_js <- function() {
87 1
	list(
88 1
		createHtmlDependency(
89 1
			name = "htmlwidgets",
90 1
			version = as.character( utils::packageVersion("htmlwidgets") ),
91 1
			src = system.file("www", package = "htmlwidgets"),
92 1
			script = c("htmlwidgets.js"),
93 1
			all_files = FALSE
94
		)
95
	)
96
}
97

98
mapdeck_js <- function() {
99 1
	list(
100 1
		createHtmlDependency(
101 1
			name = "mpadeck-binding",
102 1
			version = as.character( utils::packageVersion("mapdeck") ),
103 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
104 1
			script = c("mapdeck.js"),
105 1
			all_files = FALSE
106
		)
107
	)
108
}
109

110
mapdeck_dep_functions <- function() {
111 1
	list(
112 1
		createHtmlDependency(
113 1
			name = "mpadeck_functions",
114 1
			version = "0.0.1",
115 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
116 1
			script = c("mapdeck_functions.js"),
117 1
			all_files = FALSE
118
		)
119
	)
120
}
121

122
mapdeck_dep_coordinates <- function() {
123 1
	list(
124 1
		createHtmlDependency(
125 1
			name = "mapdeck_coordinates",
126 1
			version = "0.0.1",
127 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
128 1
			script = c("mapdeck_coordinates.js"),
129 1
			all_files = FALSE
130
		)
131
	)
132
}
133

134
mapdeck_dep_colours <- function() {
135 1
	list(
136 1
		createHtmlDependency(
137 1
			name = "mapdeck_colours",
138 1
			version = "0.0.1",
139 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
140 1
			script = c("mapdeck_colours.js"),
141 1
			all_files = FALSE
142
		)
143
	)
144
}
145

146
mapdeck_dep_location <- function() {
147 1
	list(
148 1
		createHtmlDependency(
149 1
			name = "mapdeck_location",
150 1
			version = "0.0.1",
151 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
152 1
			script = c("mapdeck_location.js"),
153 1
			all_files = FALSE
154
		)
155
	)
156
}
157

158
# https://unpkg.com/deck.gl@latest/dist.min.js
159
deckgl_min_js <- function() {
160 1
	list(
161 1
		createHtmlDependency(
162 1
			name = "deckgl",
163 1
			version = "8.1.6",
164 1
			src = system.file("htmlwidgets/lib/", package = "mapdeck"),
165 1
			script = c("deckgl.min.js"),
166 1
			all_files = FALSE
167
		)
168
	)
169
}
170

171
## https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.js
172
## https://api.mapbox.com/mapbox-gl-js/v1.10.0/mapbox-gl.css
173
mapboxgl <- function() {
174 1
	list(
175 1
		createHtmlDependency(
176 1
			name = "mapboxgl",
177 1
			version = "1.10.0",
178 1
			src = system.file("htmlwidgets/lib/", package = "mapdeck"),
179 1
			script = c("mapbox-gl.js"),
180 1
			stylesheet = c("mapbox-gl.css"),
181 1
			all_files = FALSE
182
		)
183
	)
184
}
185

186
# css specific to mapdeck map
187
mapdeck_css <- function() {
188 1
	list(
189 1
		createHtmlDependency(
190 1
			name = "mapdeck",
191 1
			version = "0.0.1",
192 1
			src = system.file("htmlwidgets/", package = "mapdeck"),
193 1
			stylesheet = c("mapdeck.css")
194
		)
195
	)
196
}
197

198
mapdeck_dep_legend <- function() {
199 1
	list(
200 1
		createHtmlDependency(
201 1
			name = "legend",
202 1
			version = "0.0.1",
203 1
			src = system.file("htmlwidgets/lib/map/", package = "mapdeck"),
204 1
			script = c("legend.js"),
205 1
			all_files = FALSE
206
		)
207
	)
208
}
209

210
mapdeck_dep_title <- function() {
211 1
	list(
212 1
		createHtmlDependency(
213 1
			name = "title",
214 1
			version = "0.0.1",
215 1
			src = system.file("htmlwidgets/lib/map/", package = "mapdeck"),
216 1
			script = c("title.js"),
217 1
			all_files = FALSE
218
		)
219
	)
220
}
221

Read our documentation on viewing source code .

Loading