-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathREADME.Rmd
153 lines (109 loc) · 3.44 KB
/
README.Rmd
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
---
output: github_document
---
<!-- README.md is generated from README.Rmd. Please edit that file -->
```{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
<!-- badges: start -->
[![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)
<!-- badges: end -->
**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!