Provides a connection to a DuckDB database of the Star Wars data. Alternatively, you can use starwars_db() to manually connect to the database using DBI::dbConnect() and duckdb::duckdb().

starwars_connect(dbdir = ":memory:", ...)

starwars_disconnect(con)

starwars_db()

Arguments

dbdir

Location for database files. Should be a path to an existing directory in the file system. With the default, all data is kept in RAM

...

Additional parameters passed to DBI::dbConnect()

con

A connection to the Star Wars database

Value

A connection to the Star Wars database, or the path to the database.

Functions

  • starwars_connect: Connect to the DuckDB database

  • starwars_disconnect: Disconnect from the DuckDB database

  • starwars_db: Returns the path to the starwarsdb database

Examples

# Manually connect using {duckdb} and {DBI}
con <- DBI::dbConnect(
  duckdb::duckdb(),
  dbdir = starwars_db(),
  read_only = TRUE
)

if (requireNamespace("dplyr", quietly = TRUE)) {
  dplyr::tbl(con, "films")
}
#> # Source:   table<films> [6 x 6]
#> # Database: DuckDB 0.6.0 [unknown@Linux 5.15.0-1024-azure:R 4.2.2//tmp/RtmpV5tXL6/starwarsdb1e012b1f139c/starwars.duckdb]
#>   title                   episode_id opening_crawl    direc…¹ produ…² release_…³
#>   <chr>                        <int> <chr>            <chr>   <chr>   <date>    
#> 1 A New Hope                       4 "It is a period… George… Gary K… 1977-05-25
#> 2 The Empire Strikes Back          5 "It is a dark t… Irvin … Gary K… 1980-05-17
#> 3 Return of the Jedi               6 "Luke Skywalker… Richar… Howard… 1983-05-25
#> 4 The Phantom Menace               1 "Turmoil has en… George… Rick M… 1999-05-19
#> 5 Attack of the Clones             2 "There is unres… George… Rick M… 2002-05-16
#> 6 Revenge of the Sith              3 "War! The Repub… George… Rick M… 2005-05-19
#> # … with abbreviated variable names ¹​director, ²​producer, ³​release_date

# Disconnect from that database (shutdown is specific to duckdb)
DBI::dbDisconnect(con, shutdown = TRUE)

# Or connect without worrying about connection details
con <- starwars_connect()

if (requireNamespace("dplyr", quietly = TRUE)) {
  dplyr::tbl(con, "films")
}
#> # Source:   table<films> [6 x 6]
#> # Database: DuckDB 0.6.0 [unknown@Linux 5.15.0-1024-azure:R 4.2.2/:memory:]
#>   title                   episode_id opening_crawl    direc…¹ produ…² release_…³
#>   <chr>                        <int> <chr>            <chr>   <chr>   <date>    
#> 1 A New Hope                       4 "It is a period… George… Gary K… 1977-05-25
#> 2 The Empire Strikes Back          5 "It is a dark t… Irvin … Gary K… 1980-05-17
#> 3 Return of the Jedi               6 "Luke Skywalker… Richar… Howard… 1983-05-25
#> 4 The Phantom Menace               1 "Turmoil has en… George… Rick M… 1999-05-19
#> 5 Attack of the Clones             2 "There is unres… George… Rick M… 2002-05-16
#> 6 Revenge of the Sith              3 "War! The Repub… George… Rick M… 2005-05-19
#> # … with abbreviated variable names ¹​director, ²​producer, ³​release_date

# Similarly, disconnect quickly without worrying about duckdb arguments
starwars_disconnect(con)