Ecosyste.ms: Awesome

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

Awesome Lists | Featured Topics | Projects

https://github.com/gadenbuie/starwarsdb

Relational Data from the Star Wars API for Learning and Teaching
https://github.com/gadenbuie/starwarsdb

dplyr duckdb-database learning relational-database sql star-wars-data teaching

Last synced: about 2 months ago
JSON representation

Relational Data from the Star Wars API for Learning and Teaching

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%"
)
```

[swapi]: https://swapi.dev
[rwars]: https://github.com/Ironholds/rwars
[dbplyr]: https://dbplyr.tidyverse.org/
[dm]: https://krlmlr.github.io/dm/
[duckdb]: https://duckdb.org

# starwarsdb

[![CRAN status](https://www.r-pkg.org/badges/version/starwarsdb)](https://CRAN.R-project.org/package=starwarsdb)
[![R-CMD-check](https://github.com/gadenbuie/starwarsdb/actions/workflows/R-CMD-check.yaml/badge.svg)](https://github.com/gadenbuie/starwarsdb/actions/workflows/R-CMD-check.yaml)

**starwarsdb** provides data from the [Star Wars API][swapi] as a set of relational tables, or as an in-package [DuckDB] database.

![](man/figures/README-starwars-data-model-1.svg)

**Formats:**
[Metadata](#star-wars-data),
[Local Tables](#local-tables),
[Remote Tables](#remote-tables),
[Data Model (dm)](#dm-tables),
[API (rwars)](#api)

## Installation

You can install **starwarsdb** from CRAN:

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

Or you can install the development version of **starwarsdb** from GitHub with [remotes](https://remotes.r-lib.org):

``` r
# install.packages("remotes")

remotes::install_github("gadenbuie/starwarsdb")

# For remotes <= 2.1.0
remotes::install_github("gadenbuie/starwarsdb@main")
```

## Star Wars Data

```{r echo=FALSE}
library(tibble)
```

All of the tables are available when you load **starwarsdb**

```{r message=FALSE, warning=FALSE}
library(dplyr)
library(starwarsdb)
```

or via

```{r eval=FALSE}
data(package = "starwarsdb")
```

The `schema` table includes information about the tables that were sourced from [SWAPI].

```{r}
schema
```

```{r}
schema %>%
filter(endpoint == "films") %>%
pull(properties)
```

## Local Tables

Ask questions, like _who flew an X-Wing?_

```{r}
x_wing_pilots <- pilots %>% filter(vehicle == "X-wing")
x_wing_pilots

people %>% semi_join(x_wing_pilots, by = c(name = "pilot"))
```

## Remote Tables

**starwarsdb** also includes the entire data set as a DuckDB database,
appropriate for teaching and practicing remote database access with [dbplyr].

```{r}
con <- starwars_connect()

people_rmt <- tbl(con, "people")
pilots_rmt <- tbl(con, "pilots")
pilots_rmt

x_wing_pilots <- pilots_rmt %>% filter(vehicle == "X-wing")
x_wing_pilots

people_rmt %>% semi_join(x_wing_pilots, by = c(name = "pilot"))
```

## DM Tables

Finally, **starwarsdb** provides a function that returns a pre-configured [dm] object.
The `dm` package wraps local data frames into a complete relational data model.

```{r dm-tables}
library(dm, warn.conflicts = FALSE)

sw_dm <- starwars_dm()
sw_dm

sw_dm %>%
dm_select_tbl(pilots, people) %>%
dm_filter(pilots = vehicle == "X-wing") %>%
dm_zoom_to("people") %>%
semi_join(pilots)
```

```{r starwars-data-model, eval = FALSE, echo = TRUE}
dm_draw(sw_dm)
```

![](man/figures/README-starwars-data-model-1.svg)

```{r starwars-data-model-write, echo = FALSE, eval = TRUE}
sw_dm %>%
dm_draw(graph_attrs = 'bgcolor="transparent"', node_attrs = 'fontname="Helvetica"') %>%
DiagrammeRsvg::export_svg() %>%
writeLines("man/figures/README-starwars-data-model-1.svg")
```

## API

For API access from R, check out the [rwars] package by Oliver Keyes.
Big thanks to `rwars` for making it easy to access the Star Wars API!