> For the complete documentation index, see [llms.txt](https://support.pears.io/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://support.pears.io/analyze/data-mart/setting-up-r.md).

# Setting up R

The PEARS data mart can be used in combination with R and [RStudio](https://posit.co/products/open-source/rstudio/) as follows.

## Prerequisites

{% stepper %}
{% step %}

#### Install R and RStudio

Visit <https://posit.co/download/rstudio-desktop/> to install both R and RStudio, if you haven't already.
{% endstep %}

{% step %}

#### Confirm firewall access

Check with the PEARS Client Success team to ensure the IP address of the computer or server running R has been added to the data mart firewall.
{% endstep %}

{% step %}

#### Confirm network access

Check with your IT staff to ensure incoming and outbound traffic on the data mart port is allowed to the computer or server running R.
{% endstep %}

{% step %}

#### Obtain credentials

Obtain the host, port, database, user, and password from the PEARS Client Success team.
{% endstep %}
{% endstepper %}

## Setup

### Certificates

Before a trusted connection can be established with the data mart, Amazon's RDS certificates must be downloaded to a local folder on the device.

{% stepper %}
{% step %}

#### Download the certificate bundle

Download a [certificate bundle from Amazon](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/UsingWithRDS.SSL-certificate-rotation.html). You will either need the specific bundle for the us-east-1 region or the AWS global bundle.

* Global (any AWS region): <https://truststore.pki.rds.amazonaws.com/global/global-bundle.pem>
* US East (N. Virginia): <https://truststore.pki.rds.amazonaws.com/us-east-1/us-east-1-bundle.pem>
  {% endstep %}

{% step %}

#### Place the file locally

Place the file downloaded into a location on your local drive that can be accessed by R.

* One suggestion is under the root of the drive, such as `C:\Certificates\`.
* If you have limited access to your local drive, consider a location under your user folder, such as `C:\Users\<username>\Certificates\`.
  {% endstep %}
  {% endstepper %}

### Package Installation

R can connect directly to the data mart using the `RPostgres` library along with the standard `DBI` library.

```r
install.packages("RPostgres")
install.packages("DBI")
```

It is recommended that you **not** provide your password in clear text as part of your script. One way to handle your password more securely is to utilize the keyring with an initial one-time step. The script below will prompt for the password and save it securely for future use.

{% hint style="info" %}
**TIP:** The PEARS Client Success team will provide your username and password along with the other connection information.
{% endhint %}

```r
install.packages("keyring")
library(keyring)

# One-time setup to store password
key_set("PEARS_datamart")  # You'll be prompted to enter the password securely
```

## Usage

Following is a sample R script that will connect to the data mart and list all available tables. Replace the values for `host`, `port`, `dbname`, and `user` with those provided by the PEARS Client Success team. Additionally, `sslrootcert` should reference the certificate file downloaded as part of the setup described above.

```r
library(DBI)
library(keyring)

db_password <- key_get("PEARS_datamart")

con <- dbConnect(
  RPostgres::Postgres(),
  host = "host name",
  port = 12345,
  dbname = "data mart name",
  sslmode = "verify-full",
  sslrootcert = "C:/Users/<username>/Certificates/global-bundle.pem",
  user = "username",
  password = db_password
)

dbListTables(con)
dbDisconnect(con)
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://support.pears.io/analyze/data-mart/setting-up-r.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
