-
Notifications
You must be signed in to change notification settings - Fork 0
/
acs_5yr_rolap.R
164 lines (146 loc) · 4.55 KB
/
acs_5yr_rolap.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
153
154
155
156
157
158
159
160
161
162
163
164
#' As `rolap::flat_table` object
#'
#' Obtain an `rolap::flat_table` object to be able to modify the data or integrate
#' it with other data.
#'
#' We can indicate the attributes of the geographic layer to include in the export.
#' Otherwise, the default attributes are included (not area, perimeter or location
#' attributes).
#'
#' @param act An `acs_5yr_topic` object.
#' @param attributes A string vector.
#'
#' @return A `flat_table` object.
#'
#' @family data exploitation and export functions
#'
#' @examples
#'
#' ft <- anrc_2021_x01 |>
#' as_flat_table()
#'
#' @export
as_flat_table <- function(act, attributes)
UseMethod("as_flat_table")
#' @rdname as_flat_table
#' @export
as_flat_table.acs_5yr_topic <- function(act, attributes = NULL) {
geo <- sf::st_drop_geometry(act$geo)
names <- names(geo)
if (is.null(attributes)) {
i <- grep('ALAND|AWATER|INTPTLAT|INTPTLON|FUNCSTAT|Shape', names, ignore.case = TRUE)
attributes <- names[-i]
} else {
attributes <- validate_names(names, attributes, 'attribute')
}
geo <- tibble::as_tibble(geo[, attributes])
data <- act$data
data <- transform_metadata_rest(data)
names <- names(data)
i <- grep('GEOID', names, ignore.case = TRUE)
names[i] <- 'GEOID_Data'
names(data) <- names
i <- grep('estimate|margin_of_error', names)
names <- c(names[-i], 'estimate', 'margin_of_error')
data <- data[, names]
data$estimate <- as.numeric(data$estimate)
data$margin_of_error <- as.numeric(data$margin_of_error)
data <- dplyr::inner_join(geo, data, by = "GEOID_Data")
ft <-
rolap::flat_table(name = act$area,
instances = data,
unknown_value = "Not available")
ft <- ft |>
rolap::transform_to_attribute("report_var", width = 2)
ft
}
#' As `rolap::star_database` object
#'
#' Obtain an `rolap::star_database` object to be able to export it to a RDBMS and
#' make queries with other tools.
#'
#' We can indicate the attributes of the geographic layer to include in the export.
#' Otherwise, the default attributes are included (not area, perimeter or location
#' attributes).
#'
#' @param act An `acs_5yr_topic` object.
#' @param attributes A string vector.
#'
#' @return A `star_database` object.
#'
#' @family data exploitation and export functions
#'
#' @examples
#'
#' st <- anrc_2021_x01 |>
#' as_star_database()
#'
#' @export
as_star_database <- function(act, attributes)
UseMethod("as_star_database")
#' @rdname as_star_database
#' @export
as_star_database.acs_5yr_topic <- function(act, attributes = NULL) {
ft <- as_flat_table(act, attributes)
ft <- ft |>
rolap::snake_case()
names <- names(ft$table)
l <- length(names)
i <- grep('year', names, fixed = TRUE)
geo_names <- names[1:(i - 1)]
var_names <- names[(i + 1):(l - 2)]
i <- grep('report_var', var_names, fixed = TRUE)
var_names <- var_names[-i]
i <- grep('subreport', var_names, fixed = TRUE)
var_names <- c(var_names[1:i], 'report_var', var_names[(i + 1):length(var_names)])
measure_names <- names[(l - 1):l]
when <- rolap::dimension_schema(name = "dim_when",
attributes = 'year')
where <- rolap::dimension_schema(name = "dim_where",
attributes = geo_names)
what <- rolap::dimension_schema(name = "dim_what",
attributes = var_names)
facts <- rolap::fact_schema(name = ft$name,
measures = measure_names)
schema <- rolap::star_schema() |>
rolap::define_facts(facts) |>
rolap::define_dimension(when) |>
rolap::define_dimension(where) |>
rolap::define_dimension(what)
db <- ft |>
rolap::as_star_database(schema)
db
}
#' As `geomultistar::geomultistar` object
#'
#' Obtain an `geomultistar::geomultistar` object to be able to enrich multidimensional
#' queries with geographic data.
#'
#' We can indicate the attributes of the geographic layer to include in the export.
#' Otherwise, the default attributes are included (not area, perimeter or location
#' attributes).
#'
#' @param act An `acs_5yr_topic` object.
#' @param attributes A string vector.
#'
#' @return A `geomultistar` object.
#'
#' @family data exploitation and export functions
#'
#' @examples
#'
#' gms <- anrc_2021_x01 |>
#' as_geomultistar()
#'
#' @export
as_geomultistar <- function(act, attributes)
UseMethod("as_geomultistar")
#' @rdname as_geomultistar
#' @export
as_geomultistar.acs_5yr_topic <- function(act, attributes = NULL) {
ms <- as_star_database(act, attributes) |>
rolap::as_multistar()
gms <-
geomultistar::geomultistar(ms, geodimension = "dim_where")
gms
}