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: 8 months ago
JSON representation
Relational Data from the Star Wars API for Learning and Teaching
- Host: GitHub
- URL: https://github.com/gadenbuie/starwarsdb
- Owner: gadenbuie
- License: other
- Created: 2020-10-07T19:38:05.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2022-12-08T16:08:54.000Z (almost 3 years ago)
- Last Synced: 2024-07-04T02:34:41.825Z (over 1 year ago)
- Topics: dplyr, duckdb-database, learning, relational-database, sql, star-wars-data, teaching
- Language: R
- Homepage: https://pkg.garrickadenbuie.com/starwarsdb
- Size: 584 KB
- Stars: 37
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
Awesome Lists containing this project
- awesome-rainmana - gadenbuie/starwarsdb - Relational Data from the Star Wars API for Learning and Teaching (R)
- jimsghstars - gadenbuie/starwarsdb - Relational Data from the Star Wars API for Learning and Teaching (R)
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
[](https://CRAN.R-project.org/package=starwarsdb)
[](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.

**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)
```

```{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!