An open API service indexing awesome lists of open source software.

https://github.com/josesamos/rolap

Relational OLAP in R
https://github.com/josesamos/rolap

Last synced: 26 days ago
JSON representation

Relational OLAP in R

Awesome Lists containing this project

README

        

---
output: github_document
---

```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```

# rolap rolap website

[![CRAN status](https://www.r-pkg.org/badges/version/rolap)](https://CRAN.R-project.org/package=rolap)
[![R-CMD-check](https://github.com/josesamos/rolap/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/josesamos/rolap/actions/workflows/R-CMD-check.yaml)
[![Codecov test coverage](https://codecov.io/gh/josesamos/rolap/branch/master/graph/badge.svg)](https://app.codecov.io/gh/josesamos/rolap?branch=master)
[![Downloads](http://cranlogs.r-pkg.org/badges/grand-total/rolap?color=brightgreen)](https://www.r-pkg.org:443/pkg/rolap)

The aim of the *multidimensional data model* is organize data for supporting data analysis. Data in multidimensional systems is obtained from operational systems and is transformed to adapt it to the new structure.

Transformations can be carried out using professional ETL (*Extract, Transform and Load*) tools. Recently, tools aimed at end users have emerged, which are also aimed at performing transformation operations. All these tools are very useful to carry out the transformation process, they provide a development environment to define the transformation operations in a general way.

Frequently, the operations to be performed aim to transform a set of tables with data that comes from operational systems into a ROLAP (*Relational On-Line Analytical Processing*) star database, made up of fact and dimension tables, which implements a multidimensional system. With the tools mentioned above, this transformation can be carried out, but it requires a lot of work. We are not aware of any tools with operations designed to specifically support this transformation process.

The goal of `rolap` is to define transformations that allow us to easily obtain ROLAP star databases, composed by fact and dimension tables, from operational tables, to be able to export them in various formats to be used by OLAP query tools and also be able to exploit them from R.

The `rolap` package builds on experience with the [`starschemar`](https://CRAN.R-project.org/package=starschemar) package on which it is based. It incorporates the main functionalities for which `starschemar` was initially intended. In particular, the data model and the way of treating role-playing and role dimensions have been changed, so that it is easier to add future extensions. It has been designed in such a way that migration from `starschemar` is practically immediate.

## Installation

You can install the released version of `rolap` from [CRAN](https://CRAN.R-project.org) with:

``` r
install.packages("rolap")
```

And the development version from [GitHub](https://github.com/) with:

``` r
devtools::install_github("josesamos/rolap")
```

## Example

```{r, echo = FALSE}
library(rolap)
```

To illustrate how the package works we will use a small part of the [Deaths in 122 U.S. cities - 1962-2016. 122 Cities Mortality Reporting System](https://catalog.data.gov/dataset/deaths-in-122-u-s-cities-1962-2016-122-cities-mortality-reporting-system) data set in the form of a flat table, available in the package in the `ft_num` variable, shown below.

```{r, results = "asis", echo = FALSE}
pander::pandoc.table(ft_num, split.table = Inf)
```

### Star database definition

The transformation to obtain a star database from the table using `rolap` package is as follows:

```{r}
library(rolap)

where <- dimension_schema(name = "Where",
attributes = c("REGION",
"State",
"City"))

s <- star_schema() |>
define_facts(name = "MRS Cause",
measures = c("Pneumonia and Influenza Deaths",
"All Deaths")) |>
define_dimension(name = "When",
attributes = c("Year")) |>
define_dimension(where)

db <- star_database(s, ft_num) |>
snake_case()
```

The dimension and fact schemas can be defined as variables (`where`) to be reused or directly in the star schema definition. To make it easier to work in a database environment we transform the table field names to snake case.

Geographic attributes can be associated with vector layers of geographic information.

```{r}
db <- db |>
define_geoattribute(
dimension = "where",
attribute = "state",
from_layer = us_layer_state,
by = "STUSPS"
)
```

### Result

To better appreciate the result, let's export it as a `tibble` list. The tables of dimensions and facts of the obtained star database are shown below.

```{r, results = "asis"}
ls <- db |>
as_tibble_list()

for (i in 1:length(ls)) {
pander::pandoc.table(ls[[i]], split.table = Inf)
}
```

The result can be exported in various formats such as *csv* and *xslx* files or to a relational database, as shown below.

```{r}
con <- DBI::dbConnect(RSQLite::SQLite())

db |>
as_rdb(con)

DBI::dbListTables(con)

DBI::dbDisconnect(con)
```

In addition to exporting it, we can formulate multidimensional queries from R. Below is an example.

```{r}
sq <- star_query(db) |>
select_dimension(name = "where",
attributes = "state") |>
select_dimension(name = "when",
attributes = "year") |>
select_fact(name = "mrs_cause",
measures = "all_deaths") |>
filter_dimension(name = "when", year >= "1963") |>
filter_dimension(name = "where", city == "Bridgeport" | city == "Boston")

db_2 <- db |>
run_query(sq)
```

The result can be displayed using the [`pivottabler`](https://CRAN.R-project.org/package=pivottabler) package.

```{r, eval=FALSE}
ft <- db_2 |>
as_single_tibble_list()
ft_cause <- ft[["mrs_cause"]]

pt <- pivottabler::qpvt(
ft_cause,
c("=", "state"),
c("year"),
c("Number of Deaths" = "sum(all_deaths)")
)

pt$renderPivot()
```

```{r, echo = FALSE, fig.align = 'center', out.width = "40%"}
knitr::include_graphics("man/figures/README-pivottabler.png")
```

We can obtain a geographic information layer that includes it, to use it in R as an object of class `sf`.

```{r}
gl <- db_2 |>
as_geolayer()

l1 <- gl |>
get_layer()
class(l1)

title <- gl |>
get_variable_description("var_1")

plot(sf::st_geometry(l1[, c("var_1")]), axes = TRUE, main = title)
text(
sf::st_coordinates(sf::st_centroid(sf::st_geometry(l1))),
labels = paste0(l1$state, ": ", l1$var_1),
pos = 3,
cex = 1.5
)
```

We can also include all geographic instances originally present in the layer.

```{r}
l2 <- gl |>
get_layer(keep_all_variables_na = TRUE)

plot(sf::st_shift_longitude(l2[, "var_1"]), axes = TRUE, main = title)
```

Or export it in *GeoPackage* format.

```{r}
f <- gl |>
as_GeoPackage(dir = tempdir())

sf::st_layers(f)
```

We can work with several star databases to form a *constellation*. It supports the definition of *role-playing* and *role* dimensions, as well as *incremental refresh* operations and automatic deployment on any RDBMS (*Relational Database Management System*). Examples and detailed information of these functionalities can be found in the documentation and vignettes of the package.