The main goal of connections is to integrate DBI-compliant packages with the RStudio IDE’s Connection Pane. Packages such as RPostgres, RSQLite, RMariaDB and bigrquery connect R to those databases, but do not provide a direct integration with the Connections Pane. connections reads the configuration of the connection and creates the integration with RStudio.

A second goal is to provide integration with the pins package. The connections package allows you to pin database connections and dplyr table objects.

Installation

Install the development version from GitHub with:

Functions

The two main functions added by connections are:

library(connections)
library(RSQLite)

con <- connection_open(SQLite(), "local.sqlite")


The connection can now be closed by using the appropriate button in the Connections pane, or by using connection_close()


The connection code is parsed when connecting to the database, and it is visible once the connection is closed.

Uploading and referencing tables with dplyr

connections integrates with dplyr by supporting the following two functions:

  • tbl() - To create a pointer to a table or view within the database.
  • copy_to() - To copy data from the R session to the database.

The version of copy_to() inside connections automatically updates the Connections pane, so the new table automatically shows up.

To use an existing table inside the database use tbl().


The tbl() function opens the rest of the already available dplyr database integration.

pins

The connections package integrates with pins. It enables the ability to save and retrieve connections and queries.

library(pins)
board_register_local(cache = "~/pins")

Pin a connection

Use the same pin() command to save a database connection. Under the hood, connections saves the necessary information to recreate the connection code, not the actual connection R object.

pin(con, "my_conn", board = "local")


Use pin_get() to re-open the connection. In effect, pin_get() will replay the exact same code used to initially connect to the database. This means that connection_open() is already called for you, so the Connections pane should automatically start up. Assign the output of pin_get() to a variable, such as con. The variable will work just like any connection variable.

con <- pin_get("my_conn", board = "local")

The Connections Pane does not open by default when pulled via a pin. To open it use connection_view()

The con variable is now a regular database connection variable.

Pin a dplyr database query

When dplyr works with database data, the resulting query is not executed until the data is explicitly collected into R, or when printing the top results to the R Console. The pin records two things:

  • The dplyr R object that contains all of the transformations. It does not save the actual results.
  • The necessary information to recreate the database connection. This is to make sure that the data is being retrieved from the original database connection.
pin(db_mtcars, "avg_mpg", board = "local")


pin_get() will connect to the database, and return the dplyr object. Without assigning it to a variable, the pin will immediately print the results of the database. Those results are being processed at the time pin_get() runs.

Full pins example

The way pins integrates with databases, via the connections package, allows to open the connection from a pin, and pipe all of the subsequent code into a new pin. Afterwards, that pin can be used to collect or to continue using the dplyr object.


Back-end examples

There are a couple of examples of how the Connections pane will look when opening the connection via connections.

DBI connections

It is possible to integrate DBI connections not opened via connection_open(). To do that, use connection_view() and pass it the variable containing the existing database connection.

library(DBI)

con <- dbConnect(RSQLite::SQLite(), ":memory:")

connection_view(con)


Changes to the database will not automatically load in the Connections pane. The connection_update() function will refresh the pane with the latest.

dbWriteTable(con, "mtcars", mtcars)

connection_update(con)