Create a local database in memory or on disk using duckdb(). This is the ideal method to experiment on a small scale. DuckDB is a relational database similar to SQLite, with full support for date and datetime data.

connect_local_database(file, timezone = Sys.timezone())

Arguments

file

A file path to an existing or new database file with a ".duckdb" extension.

timezone

A string for the time zone in which to return data to R from the database. By default, it is set to Sys.timezone().

Value

A database connection object of class duckdb_connection.

Details

This function creates a database on disk at the desired path. The database and its content will persist after it is disconnected.

Warning

This method does not provide any encryption or password protection. You should only use this method with mock data unless you operate within a secure data enclave.

See also

The duckdb website provides excellent guidance on how to connect to databases: https://duckdb.org/docs/api/r

Examples

    # Create database and load data
    con <- connect_local_database("ramses-db.duckdb")
#> DuckDB database created in 
#> ramses-db.duckdb
#> Please do not use real patient data.
    
    dplyr::copy_to(dest = con, df = reference_aware, name = "reference_aware", 
                   overwrite = FALSE, temporary = FALSE)      
    
    # Close connection to database
    DBI::dbDisconnect(con, shutdown=TRUE)
    
    # Connect to the database again and show data
    con <- connect_local_database("ramses-db.duckdb")
#> Connected to local DuckDB database ramses-db.duckdb
#> Please do not use real patient data.
    dplyr::tbl(con, "reference_aware")
#> # Source:   table<reference_aware> [?? x 8]
#> # Database: DuckDB v0.9.2 [unknown@Linux 5.15.0-1054-azure:R 4.3.2/ramses-db.duckdb]
#>    ATC_code ATC_route ATC_name    aware_category version  year VTM_code VTM_name
#>    <chr>    <chr>     <chr>       <chr>          <chr>   <dbl> <chr>    <chr>   
#>  1 J01GB06  P         amikacin    Watch          England  2019 48836000 Amikacin
#>  2 J01CA04  P         amoxicillin Access         England  2019 27658006 Amoxici…
#>  3 J01CA04  O         amoxicillin Access         England  2019 27658006 Amoxici…
#>  4 J01CR02  O         amoxicilli… Watch          England  2019 89519005 Co-amox…
#>  5 J01CR02  P         amoxicilli… Watch          England  2019 89519005 Co-amox…
#>  6 J01CA01  O         ampicillin  Access         England  2019 31087008 Ampicil…
#>  7 J01CA01  P         ampicillin  Access         England  2019 31087008 Ampicil…
#>  8 J01CA51  P         ampicillin… Access         England  2019 NA       NA      
#>  9 J01FA10  P         azithromyc… Watch          England  2019 96034006 Azithro…
#> 10 J01FA10  O         azithromyc… Watch          England  2019 96034006 Azithro…
#> # ℹ more rows
    DBI::dbDisconnect(con, shutdown=TRUE)
    file.remove("ramses-db.duckdb")
#> [1] TRUE