| duckdb | R Documentation |
duckdb() creates or reuses a database instance.
duckdb_shutdown() shuts down a database instance.
Return an adbcdrivermanager::adbc_driver() for use with Arrow Database Connectivity via the adbcdrivermanager package.
dbConnect() connects to a database instance.
dbDisconnect() closes a DuckDB database connection.
The associated DuckDB database instance is shut down automatically,
it is no longer necessary to set shutdown = TRUE or to call duckdb_shutdown().
duckdb(
dbdir = DBDIR_MEMORY,
read_only = FALSE,
bigint = "numeric",
config = list(),
...,
home = NULL,
shared_home = NULL,
allow_extensions = NULL,
environment_scan = FALSE
)
duckdb_shutdown(drv)
duckdb_adbc()
## S4 method for signature 'duckdb_driver'
dbConnect(
drv,
dbdir = DBDIR_MEMORY,
...,
debug = getOption("duckdb.debug", FALSE),
read_only = FALSE,
timezone_out = "UTC",
tz_out_convert = c("with", "force"),
config = list(),
bigint = "numeric",
array = "none",
geometry = "blob",
map = "data.frame"
)
## S4 method for signature 'duckdb_connection'
dbDisconnect(conn, ..., shutdown = TRUE)
dbdir |
Location for database files.
Should be a path to an existing directory in the file system.
With the default (or |
read_only |
Set to |
bigint |
How 64-bit integers should be returned.
There are two options: |
config |
Named list with DuckDB configuration flags, see https://duckdb.org/docs/configuration/overview#configuration-reference for the possible options. These flags are only applied when the database object is instantiated. Subsequent connections cannot apply them, and fail rather than ignoring them. |
... |
These dots are for future extensions and must be empty. |
home |
Root directory for DuckDB's downloaded extensions and stored secrets.
|
shared_home |
Opt in or out of the shared
Cannot be combined with |
allow_extensions |
The argument takes precedence over the |
environment_scan |
Set to |
drv |
Object returned by |
debug |
Print additional debug information, such as queries. |
timezone_out |
The time zone in which plain |
tz_out_convert |
How to convert timestamp columns to the timezone specified in |
array |
How arrays should be returned.
There are two options: |
geometry |
How geometry columns should be returned.
There are two options: |
map |
How |
conn |
A |
shutdown |
Unused. The database instance is shut down automatically. |
The behavior of with = "force" at DST transitions depends on
how R handles translation from the underlying time representation to a human-readable format.
If the timestamp is invalid in the target timezone, the resulting value may be NA or an adjusted time.
duckdb() returns an object of class duckdb_driver.
dbDisconnect() and duckdb_shutdown() are called for their side effect.
An object of class "adbc_driver"
dbConnect() returns an object of class duckdb_connection.
duckdb() returns a driver object that owns a DuckDB database instance.
dbConnect() opens connections to that instance,
and many connections can share one instance.
For a file-based dbdir, the instance is cached, keyed by the (normalized) path:
calling duckdb() again with the same dbdir returns the same driver and instance while it is still alive.
This is deliberate.
DuckDB allows only a single read-write handle to a database file at a time,
so opening a second instance of the same file fails with a lock error in another process,
and, on Linux and macOS, is not prevented at all within the same one.
Reusing one instance instead lets any number of dbConnect(duckdb(dbdir = "my.db")) calls share it.
An in-memory database (:memory:, the default) has no file to lock and is never cached:
every duckdb() call creates a fresh, isolated instance.
The key is the path as the engine resolves it, not as normalizePath() does.
DuckDB canonicalizes the longest part of the path that exists and appends the rest,
so a database that does not exist yet gets the key it will keep once created,
and two spellings of one database (a relative path, a symlink, a different separator)
share an instance instead of each opening their own.
A path that resolves no further is used as it stands rather than refused.
Symbolic links are not supported on Windows,
where creating one takes administrator rights or Developer Mode:
there, a symlink to a database that does not exist yet fails to open.
Because the instance is created once per database file,
config, read_only, home, and shared_home take effect only at creation.
A call that reuses an existing instance cannot apply them, and fails rather than dropping them.
Passing dbdir to dbConnect() fails too when the driver owns a database file of its own,
because the connection would go to dbdir while the driver kept its own database open.
To apply different values to a file-based database –
for example to reopen it read-only, or to send extensions and secrets elsewhere –
first release the instance with duckdb_shutdown(), which also drops it from the cache,
then create it again.
dbDisconnect() closes one connection, and its shutdown argument is unused.
Connections keep the instance alive,
so it is released once the last connection to it closes,
unless a result not yet cleared with dbClearResult() or an Arrow stream not yet released still uses it.
The cache does not find an instance that only such a result or stream keeps open,
so duckdb() with the same dbdir then opens a second instance of the file in the same session.
A driver that was never connected to releases its instance
when the driver is garbage-collected or the session ends.
dbIsValid() reports whether a driver still holds an instance.
DuckDB's prebuilt extensions for Linux are compiled with the GNU C++ standard library (libstdc++).
Loading one into a duckdb package that was itself built with a different C++ standard library –
most commonly libc++ (clang's -stdlib=libc++) –
is an ABI mismatch that crashes R (https://github.com/duckdb/duckdb-r/issues/1107).
Almost all Linux builds (CRAN binaries and most source installs) use libstdc++ and are unaffected;
macOS and Windows are unaffected.
Each duckdb() call decides whether the driver it returns may load extensions,
via the allow_extensions argument, the duckdb.allow_extensions option,
the DUCKDB_R_ALLOW_EXTENSIONS environment variable, or automatic detection.
On the automatic path a build that was not compiled with libstdc++ on Linux disables extensions:
INSTALL / LOAD raise a clear error instead of crashing,
automatic extension install/load is turned off,
and a throttled advisory message is shown when duckdb() is called.
Pass allow_extensions = FALSE to disable extensions and silence that message,
or allow_extensions = TRUE to attempt loading anyway (which may still crash R).
The decision is carried on the returned driver as the experimental allow_extensions slot (see duckdb_driver).
library(adbcdrivermanager)
with_adbc(db <- adbc_database_init(duckdb_adbc()), {
as.data.frame(read_adbc(db, "SELECT 1 as one;"))
})
drv <- duckdb()
con <- dbConnect(drv)
dbGetQuery(con, "SELECT 'Hello, world!'")
dbDisconnect(con)
duckdb_shutdown(drv)
# Shorter:
con <- dbConnect(duckdb())
dbGetQuery(con, "SELECT 'Hello, world!'")
dbDisconnect(con, shutdown = TRUE)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.