connect_local_database: Connect to (or create) a local DuckDB database

View source: R/database.R

connect_local_databaseR Documentation

Connect to (or create) a local DuckDB database

Description

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.

Usage

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().

Details

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

Value

A database connection object of class duckdb_connection.

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")
    
    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")
    dplyr::tbl(con, "reference_aware")
    DBI::dbDisconnect(con, shutdown=TRUE)
    file.remove("ramses-db.duckdb")

ramses-antibiotics/ramses-package documentation built on Feb. 13, 2024, 1:01 p.m.