SymbolixAU / mapdeck
1
#' Mapdeck_tokens
2
#'
3
#' Retrieves the mapdeck token that has been set
4
#'
5
#' @export
6
mapdeck_tokens <- function() {
7

8 0
	if(!is.na( getOption("mapdeck")[["mapdeck"]][["mapbox"]] ) ) {
9 0
		return( getOption("mapdeck") )
10
	}
11

12 0
	if( !is.null( get_access_token() ) ){
13 0
		return( get_access_token() )
14
	}
15

16 0
	cat("no tokens found")
17 0
	return(invisible())
18

19
}
20

21
#' @export
22
print.mapdeck_api <- function(x, ...) {
23

24 0
	for (i in 1:length(x)) {
25

26 0
		cat("Mapdeck tokens\n")
27

28 0
		for (j in 1:length(x[[i]])){
29 0
			cat(" - ", names(x[[i]])[j], ": ")
30 0
			key <- x[[i]][[j]]
31 0
			cat(ifelse(is.na(key), "", key), "\n")
32
		}
33
	}
34
}
35

36
#' Set Token
37
#'
38
#' Sets an access token so it's available for all mapdeck calls. See details
39
#'
40
#' @param token Mapbox access token
41
#'
42
#' @details
43
#' Use \code{set_token} to make access tokens available for all the \code{mapdeck()}
44
#' calls in a session so you don't have to keep specifying the \code{token} argument
45
#' each time
46
#'
47
#' @export
48
set_token <- function(token) {
49

50 0
	options <- getOption("mapdeck")
51 0
	api <- "mapbox" ## future-proofing for other api keys
52

53 0
	options[['mapdeck']][[api]] <- token
54 0
	class(options) <- "mapdeck_api"
55 0
	options(mapdeck = options)
56 0
	invisible(NULL)
57
}
58

59

60
#' Clear tokens
61
#'
62
#' Clears the access tokens
63
#'
64
#' @export
65
clear_tokens <- function() {
66

67 0
	options <- list(
68 0
		mapdeck = list(
69 0
			mapbox = NA_character_
70
		)
71
	)
72 0
	attr(options, "class") <- "mapdeck_api"
73 0
	options(mapdeck = options)
74

75
}
76

77
get_access_token <- function(api = "mapbox") {
78

79 1
	api <- getOption("mapdeck")[['mapdeck']][[api]]
80 1
	if( is.null( api ) || is.na( api ) ) {
81 1
		e <- Sys.getenv()
82 1
		e <- e[ grep( "mapbox|mapdeck", names( e ), ignore.case = TRUE ) ]
83

84 1
		api <- unique( as.character( e ) )
85 1
		if( length( api ) > 1 ) {
86 0
			warning("Multiple MAPBOX API tokens found in Sys.getenv(), using the first one")
87
		}
88
	}
89 1
	if(length(api) == 0) api <- NULL
90 1
	return(api[1L])
91
}
92

Read our documentation on viewing source code .

Loading