Pin R Object to Posit Connect

In R, pins is an open source package that allows you to publish and share R objects (data, models, etc.) across projects and with your colleagues. The process of pinning an R object is similar to using a push-pin to pin a piece of paper to a cork board:

In this example, we’ll pin an R dataset to Posit Connect.

Install and load the pins package

# Install pins
install.packages("pins")

# Load pins
library(pins)

Set up Posit Connect as your board

Pins supports a variety of boards including Posit Connect, Amazon S3, Azure Storage, and Microsoft 365.

To use Posit Connect as a board, you first need to authenticate. If you’re using the RStudio IDE, you can do this via Tools –> Global Options –> Publishing –> Connect. Otherwise, you can manually set the Connect server address and supply an API key. Once Posit Connect has been authenticated, we can connect to it via the board_connect() function and save the connection as my_board. The output from this function in your environment should reflect your Connect server.

my_board <- pins::board_connect()

# Connecting to Posit Connect [version number] at [Connect URL]

Pin R object to Posit Connect

For this example, our object with be the mtcars dataset:

# Assign mtcars datest to the "r_object" variable
r_object <- mtcars

And now we can pin the r_object to our Posit Connect server. We’ll name our pin “mtcars_dataset” and publish it as a csv file.

# Pin to Posit Connect
pins::pin_write(x = r_object,
                board = my_board,
                name = "mtcars_dataset",
                type = "csv")

# Writing to pin 'ryan/mtcars_dataset'

And here is the pin on the Posit Connect:

Read Pin from Posit Connect

Now that our R object has been pinned, we can easily read the pin into another R project, or share the pin with a colleague. In the image above, Posit Connect shows you how to read the pin using R:

# Read the pin
pins::pin_read(board = my_board,
               name = "ryan/mtcars_dataset")