-
Notifications
You must be signed in to change notification settings - Fork 0
/
esios-indicators.R
131 lines (126 loc) · 5.15 KB
/
esios-indicators.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
#' List ESIOS indicators
#'
#' Find which indicators are available.
#' @param text Text to search indicators
#' @param taxonomy_terms Terms of indicators
#' @param taxonomy_ids Ids of the indicators
#'
#' @return A data.frame with four columns: name, description, short_name and id.
#' @export
#' @seealso [esios_indicators()]
#' @examples
#' \donttest{
#' ei <- esios_search_indicators()
#' mercados <- esios_search_indicators(text = "Mercados y precios")
#' }
esios_search_indicators <- function(text = NULL, taxonomy_terms = NULL, taxonomy_ids = NULL) {
check_taxonomy_ids(taxonomy_ids)
check_taxonomy_terms(taxonomy_terms)
ind <- prep_esios("indicators",
text = text,
taxonomy_terms = taxonomy_terms,
taxonomy_ids = taxonomy_ids) |>
req_perform()
out <- resp_body_json(ind)
indic <- do.call(rbind, lapply(out$indicators, list2DF))
# Extract hour from the text
s <- strsplit(indic$description, split = "Publicaci(ó|\u00F3)n:")
s[lengths(s) == 0L] <- ""
v <- mapply(getElement, s, lengths(s))
r <- extract_hour(v)
indic$renew <- r
indic
}
#' Retrieve an indicator
#'
#' Retrieve the information of any indicator based on the id.
#' @param indicator ID code for an indicator
#' @param locale Get translations for sources (es, en). Default language: es
#' @param datetime A certain date to filter values by (iso8601 format)
#' @param start_date Beginning of the date range to filter indicator values
#' (iso8601 format)
#' @param end_date End of the date range to filter indicator values
#' (iso8601 format)
#' @param time_agg How to aggregate indicator values when grouping them by time.
#' Accepted values: 'sum', 'average'. Default value: 'sum'.
#' @param time_trunc Tells the API how to trunc data time series. Accepted
#' values: 'five_minutes', 'ten_minutes', 'fifteen_minutes', 'hour', 'day', 'month', 'year'.
#' @param geo_agg How to aggregate indicator values when grouping them by geo_id.
#' Accepted values: 'sum', 'average'. Default value: 'sum'.
#' @param geo_ids Tells the API the geo ids to filter the date.
#' @param geo_trunc Tells the API how to group data at geolocalization level
#' when the geo_agg is informed. Accepted values: 'country', 'electric_system',
#' 'autonomous_community', 'province', 'electric_subsystem', 'town' and
#' 'drainage_basin'.
#' @export
#' @seealso [esios_search_indicators()]
#' @references <https://api.esios.ree.es/>
#' @examples
#' \donttest{
#' ei10001 <- esios_indicators(10001)
#' head(ei10001)
#' dr <- esios_indicators(1293)
#' }
esios_indicators <- function(indicator, locale = NULL, datetime = NULL,
start_date = NULL, end_date = NULL,
time_agg = NULL, time_trunc = NULL, geo_agg = NULL,
geo_ids = NULL, geo_trunc = NULL) {
stopifnot((is.numeric(indicator) || is.character(indicator)) &&
length(indicator) == 1 && !is.na(indicator))
path <- paste0("indicators/", indicator)
resp <- prep_esios(path,
locale = locale,
datetime = datetime,
start_date = start_date,
end_date = end_date,
time_agg = time_agg,
time_trunc = time_trunc,
geo_agg = geo_agg,
`geo_ids[]` = geo_ids,
geo_trunc = geo_trunc) |>
req_perform()
resp_body_json(resp)
}
#' Prediction of electricity prices using indicator 1001
#'
#' This is a shortcut for `esios_indicators("1001")` with some parsing.
#'
#' @inheritParams esios_indicators
#'
#' @return A data.frame with value ( €/MWh), datetime, datetime_utc, tz_time,
#' geo_id and geo_name.
#' @seealso [esios_indicators()]
#' @export
#' @examples
#' \donttest{
#' e <- esios_pvpc()
#' }
esios_pvpc <- function(locale = NULL, datetime = NULL,
start_date = NULL, end_date = NULL,
time_agg = NULL, time_trunc = NULL, geo_agg = NULL,
geo_ids = NULL, geo_trunc = NULL) {
out <- esios_indicators(indicator = "1001",
locale = locale,
datetime = datetime,
start_date = start_date,
end_date = end_date,
time_agg = time_agg,
time_trunc = time_trunc,
geo_agg = geo_agg,
geo_ids = geo_ids,
geo_trunc = geo_trunc)
val <- do.call(rbind, lapply(out$indicator$values, list2DF))
if ("datetime" %in% colnames(val)) {
# Assumes that it is in the right locale and timezone
val$datetime <- as.POSIXct(strptime(val$datetime,
format = "%Y-%m-%dT%H:%M:%OS"))}
if ("datetime_utc" %in% colnames(val)) {
val$datetime_utc <- as.POSIXct(strptime(val$datetime_utc,
tz = "UTC", format = "%Y-%m-%dT%H:%M:%SZ"))
}
if ("tz_time" %in% colnames(val)) {
val$tz_time <- as.POSIXct(strptime(val$tz_time,
tz = "Europe/Madrid", format = "%Y-%m-%dT%H:%M:%OSZ"))
}
val
}