---
title: RStudio on Databricks
description: Learn how to work with R using RStudio on Databricks. RStudio is a popular integrated development environment (IDE) for R.
canonical_url: https://docs.databricks.com/aws/en/sparkr/rstudio
---

# RStudio on Databricks

You can use [RStudio](https://www.rstudio.com/), a popular integrated development environment (IDE) for R, to connect to Databricks compute resources within Databricks workspaces from your local development machine.

## <a id="rstudio-desktop" />Set up RStudio Desktop

To set up RStudio Desktop on your local development machine:

1. [Download and install R 3.3.0 or higher](https://cran.rstudio.com/).
2. [Download and install RStudio Desktop](https://posit.co/download/rstudio-desktop/).
3. Start RStudio Desktop.

(Optional) To create an RStudio project:

1. Start RStudio Desktop.
2. Click **File > New Project**.
3. Select **New Directory > New Project**.
4. Choose a new directory for the project, and then click **Create Project**.

To create an R script:

1. With the project open, click **File > New File > R Script**.
2. Click **File > Save As**.
3. Name the file, and then click **Save**.

## Connect to Databricks

To use [RStudio Desktop](https://posit.co/download/rstudio-desktop/) to connect to a remote Databricks cluster or SQL warehouse from your local development machine, use an ODBC connection and call ODBC package functions for R.

> [!NOTE]
>
> You cannot use packages such as [SparkR](https://docs.databricks.com/aws/en/sparkr/overview.md) or [sparklyr](https://docs.databricks.com/aws/en/sparkr/sparklyr.md) in this RStudio Desktop scenario, unless you also use [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md).

To connect to the remote Databricks cluster or SQL warehouse through ODBC for R:

1. Get the **Server hostname**, **Port**, and **HTTP path** values for your remote [cluster](https://docs.databricks.com/aws/en/integrations/compute-details.md) or [SQL warehouse](https://docs.databricks.com/aws/en/integrations/compute-details.md) For a cluster, these values are on the **JDBC/ODBC** tab of **Advanced options**. For a SQL warehouse, these values are on the **Connection details** tab.

2. Get a Databricks [personal access token](https://docs.databricks.com/aws/en/dev-tools/auth/pat.md).

   > [!NOTE]
   >
   > As a security best practice when you authenticate with automated tools, systems, scripts, and apps, Databricks recommends that you use [OAuth tokens](https://docs.databricks.com/aws/en/dev-tools/auth/oauth-m2m.md).
   >
   > If you use personal access token authentication, Databricks recommends using personal access tokens belonging to [service principals](https://docs.databricks.com/aws/en/admin/users-groups/service-principals.md) instead of workspace users. To create tokens for service principals, see [Manage tokens for a service principal](https://docs.databricks.com/aws/en/admin/users-groups/manage-service-principals.md#tokens).

3. [Install and configure the Databricks ODBC driver](https://docs.databricks.com/aws/en/integrations/odbc/download.md) for your operating system.

4. Set up an ODBC Data Source Name (DSN) to your remote cluster or SQL warehouse for [Windows](https://docs.databricks.com/aws/en/integrations/odbc/dsn.md#windows), [macOS](https://docs.databricks.com/aws/en/integrations/odbc/dsn.md#macos), or [Linux](https://docs.databricks.com/aws/en/integrations/odbc/dsn.md#linux), based on your local machine's operating system.

5. From the RStudio console (**View > Move Focus to Console**), install the [odbc](https://solutions.rstudio.com/db/r-packages/odbc/) and [DBI](https://dbi.r-dbi.org/) packages from [CRAN](http://cran.us.r-project.org):

   ```r
   require(devtools)

   install_version(
     package = "odbc",
     repos   = "http://cran.us.r-project.org"
   )

   install_version(
     package = "DBI",
     repos   = "http://cran.us.r-project.org"
   )
   ```

6. In your R script (**View > Move Focus to Source**), load the installed `odbc` and `DBI` packages:

   ```r
   library(odbc)
   library(DBI)
   ```

7. Call the ODBC version of the [dbConnect](https://rdrr.io/cran/odbc/man/dbConnect-OdbcDriver-method.html) function in the `DBI` package, specifying the `odbc` driver in the `odbc` package and the ODBC DSN that you created, for example, an ODBC DSN of `Databricks`.

   ```r
   conn = dbConnect(
     drv = odbc(),
     dsn = "Databricks"
   )
   ```

8. Call an operation through the ODBC DSN, for example a `SELECT` statement through the [dbGetQuery](https://www.rdocumentation.org/packages/DBI/versions/0.5-1/topics/dbGetQuery) function in the `DBI` package, specifying the name of the connection variable and the `SELECT` statement itself, for example from a table named `diamonds` in a schema (database) named `default`:

   ```r
   print(dbGetQuery(conn, "SELECT * FROM default.diamonds LIMIT 2"))
   ```

The complete R script is as follows:

```r
library(odbc)
library(DBI)

conn = dbConnect(
  drv = odbc(),
  dsn = "Databricks"
)

print(dbGetQuery(conn, "SELECT * FROM default.diamonds LIMIT 2"))
```

To run the script, in source view, click **Source**. The results for the preceding R script are as follows:

```
  _c0 carat     cut color clarity depth table price    x    y    z
1   1  0.23   Ideal     E     SI2  61.5    55   326 3.95 3.98 2.43
2   2  0.21 Premium     E     SI1  59.8    61   326 3.89 3.84 2.31
```
