-
Notifications
You must be signed in to change notification settings - Fork 1
/
package.r
57 lines (52 loc) · 1.74 KB
/
package.r
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#' cimir: Interface to CIMIS
#'
#' This package provides an R interface to the
#' [California Irrigation Management Information System](https://cimis.water.ca.gov)
#' (CIMIS) [Web API](https://et.water.ca.gov). In order to use
#' this package, you will need to
#' [create a CIMIS account](https://cimis.water.ca.gov/Auth/Register.aspx)
#' and request a web services AppKey.
#'
#' @section Package options:
#'
#' cimir uses the following [options()] to configure behavior:
#'
#' \itemize{
#' \item `cimir.appkey`: The CIMIS AppKey to use for queries.
#' \item `cimir.timeout`: The maximum time to wait for a response
#' from the CIMIS Web API.
#' }
#' Alternatively, the CIMIS App Key can be saved to an environment
#' variable `CIMIS_APPKEY`.
#'
#' @docType package
#' @name cimir
"_PACKAGE"
.onLoad = function(libname, pkgname) {
# set up environment for authentication storage
authenv = new.env(parent = getNamespace(pkgname))
assign("authenv", authenv, envir = getNamespace(pkgname))
}
.onAttach = function(libname, pkgname) {
# check for existing AppKey
if (!is.null(options()[["cimir.appkey"]]) &&
nchar(options()[["cimir.appkey"]]) > 0L) {
set_key(options()[["cimir.appkey"]])
packageStartupMessage("Using existing App Key from ",
'option "cimir.appkey".')
} else if (nchar(Sys.getenv("CIMIS_APPKEY")) > 0L) {
set_key(Sys.getenv("CIMIS_APPKEY"))
packageStartupMessage("Using existing App Key from ",
'"CIMIS_APPKEY" environment variable.')
} else {
remove_key()
packageStartupMessage('Set your CIMIS App Key with "set_key()".')
}
if (is.null(options()[["cimir.timeout"]])) {
options(cimir.timeout = 30)
}
}
.onDetach = function(libname) {
#delete authentication information
remove_key()
}