forked from gonzalezivan90/biotablero_api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbiotablero_fun.R
72 lines (58 loc) · 2.8 KB
/
biotablero_fun.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
## Web parsing for biotablero queries
## Requires 'curl' and 'jsonlite'
## --------------------------------------------------- ##
## Parameters
## --------------------------------------------------- ##
##
## server = 'local' (default) or 'web'. Indicates the API
## location. If 'web' is selected, then must provide the *webURL argument
## port = ':8000' (default). System port opened for API connection.
## webURL = NULL (default). The API web host.
## dataPath = NULL (default). Folder containing data. Default value internally is '/data'
## printURL = FALSE (default). Should this function print the corresponding URL?
## endpoint = 'biotablero' (default). Endpoint to call. 'biotablero' is the main function.
## metric = NULL (default). Metric to calculate
## ... Other parameters for customize the query
## ---------------------------------------------------
## Usage
## ---------------------------------------------------
#
# biotablero(server = 'web', webURL = aws, port = ':8000',
# endpoint = 'biotablero',
# printURL = TRUE, metric = 'test')
#
#
# ForChange_aws <- biotablero(server = 'web', webURL = aws, port = ':8000', printURL = TRUE,
# endpoint = 'biotablero', metric = 'forest',
# sour = 'hansen', ebvstat = 'area',
# ebvyear='2000:2021',
# pol = simplePol)
biotablero <- function(server = ' 127.0.0.1', port = ':8000', webURL = NULL, dataPath = NULL, printURL = FALSE,
endpoint = 'biotablero',
metric = NULL, lay = NULL, polID = NULL, pol = NULL, outformat = NULL,
ebvstat = NULL, sour = NULL, cellSize = NULL, rasterLayer = FALSE, ... ) {
## Assign the URL
.webURL <- ifelse(is.null(webURL), 'http://biotablero.humboldt.org.co/api', webURL)
host <- ifelse(server == 'web', .webURL, 'http://localhost')
## Create a data.frame with params
dfParams <- data.frame(val = c(metric = metric, lay = lay, polID = polID, outformat = outformat,
ebvstat = ebvstat, sour = sour, dataPath = dataPath, rasterLayer = rasterLayer,
cellSize = cellSize, c(...), pol = pol))
## Build the URL
urlParams <- paste0(apply(cbind(rownames(dfParams), dfParams), 1, function(x) paste0(x, collapse = '=')), collapse = '&')
(myURL <- paste0(host, port, "/", endpoint, "?", urlParams))
## Print the URL
if(printURL) {
print(myURL)
}
## Connect the URL
con <- curl::curl(url = myURL)
## Read the URL
query <- tryCatch(jsonlite::fromJSON(readLines(con, warn = FALSE)), error = function(e) e)
close(con)
## Return R object values
return(query)
}
## URLonections
aws <- 'ec2-54-173-12-47.compute-1.amazonaws.com'
aws_port <- ':8000'