Data sources and connection methods

Depending on the data source you are accessing and the programming language you are using, you will have different connection methods available. When writing Python and R code, the most common connection methods are standardized database drivers, platform SDKs, language-specific libraries, and HTTP clients for web APIs.

Choosing a connection method

In general, it is best to use SDKs, or language-specific libraries if available, because they are typically most performant, feature-rich, and easiest to use. If these options are not available, ODBC database drivers are the next best option. Java Database Connectivity (JDBC) drivers are less desirable because they have a Java dependency, which can add complexity to manage.

SDKs

Some platforms provide SDKs to interact with their services directly from R or Python code. Because you call them from your code, they work the same in development and deployment. Examples:

Platforms often provide a CLI as well, such as the AWS CLI. CLIs are useful for interactive exploration, scripting, and local authentication setup. For data access from application code, especially deployed content, use the platform’s SDK or a language-specific library instead.

Language-specific libraries

R and Python provide language-specific, higher-level libraries that build upon the lower-level standards, SDKs, or CLIs to simplify data access for the target system. Examples:

  • Postgres database access with psycopg2 in Python or RPostgres in R
  • Access AWS resources with boto3 in Python or paws in R

HTTP clients

When a data source is exposed as a web API, you connect to it over HTTP. If the API has a dedicated R or Python package, use it, since it handles authentication and requests for you. If there is not a dedicated R or Python package, use a general-purpose HTTP client. Examples:

  • httr2 in R or requests in Python
  • Dedicated packages that wrap a specific API, which often simplify authentication and pagination, such as googlesheets4 in R or gspread in Python for accessing data in Google Sheets

ODBC/JDBC database drivers

To connect to databases, you can use standardized, system-level ODBC/JDBC database drivers. Examples:

We recommend using ODBC drivers over JDBC when possible. JDBC drivers introduce a Java dependency, which can add complexity to manage.

ImportantAdministrator configuration required

An administrator must install and configure database drivers on the server before you can use them in your code. See the Data sources admin guide for more information.

Common data sources and connection methods

This table outlines common data sources and the recommended connection methods to use with R and Python code.

The source-specific sections of this guide provide more detailed access patterns and examples for available sources.

Data Source Typical Use Case Preferred Connection Methods
Databases and data warehouses (e.g., PostgreSQL, Redshift, BigQuery) Storing and querying clean, structured data for applications or analytics Language-Specific Libraries: High-level packages such as RPostgres, bigrquery, RMariaDB in R, and psycopg2 or mysql-connector-python in Python.
ODBC Drivers: A standardized, system-level approach. Posit Professional Drivers are an excellent option.
Data lakehouse (e.g., Databricks, Snowflake) A unified platform combining data warehouse and data lake features for business intelligence (BI) and AI/ML workloads Language-Specific Libraries: In Python, use packages like snowflake-connector-python or databricks-connect. In R, use the sparklyr package to access Databricks clusters.
ODBC Drivers: In R, the odbc package provides specific helpers for connecting to Databricks and Snowflake via odbc::databricks() and odbc::snowflake().
Cloud blob storage (e.g., AWS S3, Azure Blob Storage, Google Cloud Storage) Storing unstructured data, large files, and project artifacts Language-Specific Libraries: For AWS, use boto3 in Python, or the paws package in R. For Azure, use azure-storage-blob in Python or AzureStor in R. For Google, use google-cloud-storage in Python, or googleCloudStorageR in R.
API (e.g., Yahoo Finance API, internal microservices) Accessing data from external or internal services programmatically HTTP Libraries: Use general-purpose HTTP clients like requests in Python or httr2 in R.
Language-Specific Libraries: Many popular APIs have dedicated R or Python packages that simplify authentication and requests.
Pins Caching intermediate results, models, or small datasets to speed up development and share within a team without a formal database. Accessed via the pins library in both Python and R. Posit Connect is an ideal location for pinned objects, but pins can also be stored on other various backends.
Local or network files Small, static data (countries.csv), configuration (config.json) deployed with the code, or very large files on a shared server
(This method is typically used as a last resort)
Data is accessed directly via file paths using built-in language functions.
Caution: Data deployed with code should be updated less frequently than the code itself to avoid unnecessary application redeploys. Files on a network share often require SSH access to the server for setup, and might require code changes between development and deployment.