Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/davidfoord1/dbpkgr
Turn a database connection into a temporary R package.
https://github.com/davidfoord1/dbpkgr
Last synced: about 2 months ago
JSON representation
Turn a database connection into a temporary R package.
- Host: GitHub
- URL: https://github.com/davidfoord1/dbpkgr
- Owner: davidfoord1
- Created: 2024-06-08T07:32:25.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-06-21T20:40:42.000Z (7 months ago)
- Last Synced: 2024-06-22T10:17:29.417Z (7 months ago)
- Language: R
- Homepage:
- Size: 66.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.Rmd
Awesome Lists containing this project
README
---
output: github_document
---```{r, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# dbpkgr
dbpkgr seeks to turn a database connection into a temporary R package to provide a set of convenience functions. Each created package supplies the following functions for the given connection:
- `_structure()` retrieves data.frame of catolog, schema and table names from information_schema.tables.
- `_list_schemas()` retrieves a character vector of schema names available.
- `_list_tables()` retrieves a character vector of table names available.
- `_query()` takes a SQL string and retrieves the result as a data.frame.
- `_execute()` takes an SQL string and executes it against the connection.As well as a function for each and every table in the database, so you can take
advantage of autocomplete for table names.The package is most functional where an `INFORMATION_SCHEMA` is provided by the
connection database management system (DBMS), which it can used to get the
`_structure()` and build queries like `FROM table_schema.table_name`.## Installation
You can install the development version of dbpkgr from [GitHub](https://github.com/) with:
``` r
# install.packages("remotes")
remotes::install_github("davidfoord1/dbpkgr")
```## Example
```{r example}
library(dbpkgr)
```A demo connection with a database in memory:
```{r cars}
connection <- DBI::dbConnect(RSQLite::SQLite(), ":memory:")
DBI::dbWriteTable(connection, "mtcars", mtcars)
DBI::dbWriteTable(connection, "iris", iris)
```Set up the package with a call to `dbp_package`, supplying the connection and
package name. Once the package is created you don't need to handle the
connection object anymore.```{r package}
dbp_package(connection, "mydb")
```The name will be used as a prefix for auto-complete friendly functions, with which you can get information from the connection:
```{r list_tables}
mydb_list_tables()
```Tables get their own functions:
```{r}
mydb_mtcars()
```They are lazy loaded - as a connection object that works with a dplyr/dbplyr chain:
```{r dplyr}
# median petal length/width ratio by species
mydb_iris() |>
mutate(petal_ratio = Petal.Length / Petal.Width) |>
summarise(petal_ratio = median(petal_ratio, na.rm = TRUE), .by = Species) |>
arrange(desc(petal_ratio))
```You can retrieve raw SQL query results:
```{r query}
mydb_query("SELECT cyl, COUNT(cyl) AS count FROM mtcars GROUP BY cyl")
```And execute SQL statements that don't return a table:
```{r execute}
mydb_execute("CREATE TABLE table_1 (ID int PRIMARY KEY)")
mydb_list_tables()
mydb_execute("DROP TABLE table_1")
mydb_list_tables()
```Basic documentation is automatically generated for the temporary package e.g. you could use
```{r eval = FALSE}
help(mydb_structure)
```