If your target table is in a database, the db_tbl() function is a handy way of accessing it. This function simplifies the process of getting a tbl_dbi
object, which usually involves a combination of building a connection to a database and using the dplyr::tbl() function with the connection and the table name (or a reference to a table in a schema). You can use db_tbl() as the basis for obtaining a database table for the tbl parameter in create_agent() or create_informant(). Another great option is supplying a table-prep formula involving db_tbl() to tbl_store() so that you have access to database tables though single names via a table store.
The username and password are supplied through environment variable names. If desired, values for the username and password can be supplied directly by enclosing such values in I().
table: The name of the table, or, a reference to a table in a schema (two-element vector with the names of schema and table). Alternatively, this can be supplied as a data table to copy into an in-memory database connection. This only works if: (1) the db is chosen as either "sqlite"
or "duckdb", (2) the dbname was is set to ":memory:", and (3) the object supplied to table is a data frame or a tibble object.
dbtype: Either an appropriate driver function (e.g., RPostgres::Postgres()) or a shortname for the database type. Valid names are: "postgresql", "postgres", or "pgsql" (PostgreSQL, using the RPostgres::Postgres() driver function); "mysql" (MySQL, using RMySQL::MySQL()); bigquery or bq (BigQuery, using bigrquery::bigquery()); "duckdb" (DuckDB, using duckdb::duckdb()); and "sqlite" (SQLite, using RSQLite::SQLite()).
dbname: The database name.
host, port: The database host and optional port number.
user, password: The environment variables used to access the username and password for the database. Enclose in I() when using literal username or password values.
bq_project, bq_dataset, bq_billing: If accessing a table from a BigQuery data source, there's the requirement to provide the table's associated project (bq_project) and dataset (bq_dataset) names. By default, the project to be billed will be the same as the one provided for bq_project but the bq_billing argument can be changed to reflect a different BigQuery project.
Returns
A tbl_dbi object.
Examples
Obtaining in-memory database tables
You can use an in-memory database table and by supplying it with an in-memory table. This works with the DuckDB database and the key thing is to use dbname = ":memory" in the db_tbl() call.
## # Source: table<small_table> [?? x 8]
## # Database: sqlite 3.37.0 [:memory:]
## date_time date a b c d e f
## <dbl> <dbl> <int> <chr> <dbl> <dbl> <int> <chr>
## 1 1451905200 16804 2 1-bcd-345 3 3423. 1 high
## 2 1451867520 16804 3 5-egh-163 8 10000. 1 low
## 3 1452000720 16805 6 8-kdg-938 3 2343. 1 high
## 4 1452100980 16806 2 5-jdo-903 NA 3892. 0 mid
## 5 1452342960 16809 8 3-ldm-038 7 284. 1 low
## 6 1452492900 16811 4 2-dhe-923 4 3291. 1 mid
## 7 1452883560 16815 7 1-knw-093 3 843. 1 high
## 8 1453030020 16817 4 5-boe-639 2 1036. 0 low
## 9 1453264200 16820 3 5-bce-642 9 838. 0 high
## 10 1453264200 16820 3 5-bce-642 9 838. 0 high
## # … with more rows
It's also possible to obtain a table from a remote file and shove it into an
in-memory database. For this, we can use the all-powerful file_tbl() +
db_tbl() combo.
For remote databases, we have to specify quite a few things but it's a one-step process nonetheless. Here's an example that accesses the rna table (in the RNA Central public database) using db_tbl(). Here, for the user
and password entries we are using the literal username and password values (publicly available when visiting the RNA Central website) by enclosing the values in I().
You'd normally want to use the names of environment variables (envvars) to
more securely access the appropriate username and password values when
connecting to a DB. Here are all the necessary inputs:
Environment variables can be created by editing the user .Renviron file and
the usethis::edit_r_environ() function makes this pretty easy to do.
DB table access and prep via the table store
Using table-prep formulas in a centralized table store can make it easier to work with DB tables in pointblank . Here's how to generate a table store with two named entries for table preparations involving the tbl_store() and db_tbl() functions.
The second table in the table store is a mutated
version of the first. It's just as easily obtainable via tbl_get():
tbl_get(tbl = "small_high_duck", store = store)
## # Source: lazy query [?? x 8]
## # Database: duckdb_connection
## date_time date a b c d e
## <dttm> <date> <int> <chr> <dbl> <dbl> <lgl>
## 1 2016-01-04 11:00:00 2016-01-04 2 1-bcd-345 3 3423. TRUE
## 2 2016-01-05 13:32:00 2016-01-05 6 8-kdg-938 3 2343. TRUE
## 3 2016-01-15 18:46:00 2016-01-15 7 1-knw-093 3 843. TRUE
## 4 2016-01-20 04:30:00 2016-01-20 3 5-bce-642 9 838. FALSE
## 5 2016-01-20 04:30:00 2016-01-20 3 5-bce-642 9 838. FALSE
## 6 2016-01-30 11:23:00 2016-01-30 1 3-dka-303 NA 2230. TRUE
## # … with more rows, and 1 more variable: f <chr>
The table-prep formulas in the store object could also be used in functions
with a tbl argument (like create_agent() and create_informant()). This
is accomplished most easily with the tbl_source() function.