/
getdata.R
153 lines (138 loc) · 4.37 KB
/
getdata.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#' @import httr
#' @import jsonlite
#'
NULL
#' Get a list of GUID's
#'
#' Get a list of all the available GUID's with datasets or views from the
#' base URL
#'
#' @param base_url The base URL of the Junar service
#' @param api_key The user's API key for the Junar service
#' @keywords GUID
#' @export
get_index <- function(base_url, api_key){
if (missing(base_url)) {
warning(
"Please add a valid base URL"
)
} else if (missing(api_key)) {
warning(
"Please add a valid API key for the base URL you are trying to access"
)
} else
try({
r <- GET(paste(base_url, "?auth_key=", api_key, sep=""), accept_json())
content_index <- fromJSON(content(r, "text"))
return(content_index)
})
}
#' Show GUID list
#'
#' Show a list of all available GUID's identifying the available data sets. It
#' only shows GUID's so that the list will fit in the console window.
#'
#' @param base_url The base URL of the Junar service
#' @param api_key The user's API key for the Junar service
#' @keywords GUID
#' @export
list_guid <- function(base_url, api_key){
if (missing(base_url)) {
warning("Please add a valid base URL")
} else if (missing(api_key)) {
warning(
"Please add a valid API key for the base URL you are trying to access"
)
} else
try({
content_index <- get_index(base_url, api_key)
return(content_index$guid)
})
}
#' Show GUID titles
#'
#' Show a list of all available GUID titles. It only shows the titles so that
#' the list will fit in the console window.
#'
#' @param base_url The base URL of the Junar service
#' @param api_key The user's API key for the Junar service
#' @keywords GUID
#' @export
list_titles <- function(base_url, api_key){
if (missing(base_url)) {
warning(
"Please add a valid base URL"
)
} else if (missing(api_key)) {
warning(
"Please add a valid API key for the base URL you are trying to access"
)
} else
try({
content_index <- get_index(base_url, api_key)
return(content_index$title)
})
}
#' Get data for a given GUID
#'
#' Get the data for any given GUID and return it as a data frame. Note that we
#' use the "ajson" JSON format from the API. The "JSON" format has a more
#' complex structure.
#'
#' We do use the JSON response to get the \code{fLength} value, which indicates the
#' length of the dataset. This way we can include a fixed way to get around the
#' default limit of 1000 rows of the Junar API.
#'
#' Note that this removes all meta-data from the JSON response given by the API.
#'
#' @param base_url The base URL of the Junar service
#' @param api_key The user's API key for the Junar service
#' @param guid The GUID of the data set of interest
#' @param stream The type of data stream of the request. By default we use the
#' paginated stream (stream = "paged"). To get the full unpaginated stream
#' set stream = "unlimited". Please use the latter option with care
#' @keywords GUID
#' @export
get_data <- function(base_url, api_key, guid, stream = "paged") {
if (missing(base_url)) {
warning("Please add a valid base URL")
} else if (missing(api_key)) {
warning(
"Please add a valid API key for the base URL you are trying to access"
)
} else if (missing(guid)) {
warning(
"Please add a valid GUID for the dataset you are trying to access"
)
} else if (stream == "paged") {
try({
r_json <- GET(paste(base_url, guid,
"/data.json/", "?auth_key=",
api_key, sep = ""), accept_json())
jsondata <- fromJSON(content(r_json, "text"))
data_length <- jsondata$result$fLength
r_ajson <- GET(paste(base_url, guid,
"/data.ajson/","?auth_key=",
api_key, "&limit=", data_length, sep = ""), accept_json())
dataset <- fromJSON(content(r_ajson, "text"))
dataset <- dataset$result
df <- as.data.frame(dataset)
colnames(df) <- dataset[1,]
df <- df [-1,]
return(df)
})
} else if (stream == "unlimited") {
try({
warning(
"Please take into account that using stream = 'unlimited' puts stress
on the API infrastructure. Please use this option with care.
Toma en cuenta que este tipo de llamados impactan la infraestructura
del API. Usalo por favor con prudencia y consideraciu00F3n"
)
r_pjson <- fromJSON(paste(base_url, guid,
"/data.pjson/","?auth_key=", api_key, sep = ""))
df <- r_pjson$result
return(df)
})
}
}