---
title: Databricks for R developers
description: Learn how to work with Apache Spark from R using SparkR, sparklyr, and RStudio in Databricks.
canonical_url: https://docs.databricks.com/aws/en/sparkr/
---

# Databricks for R developers

This page provides an overview for developing notebooks and jobs in Databricks using the R language.

## Tutorials

The following tutorials provide example code and notebooks to learn about common workflows. See [Import a notebook](https://docs.databricks.com/aws/en/notebooks/notebook-export-import.md#import-notebook) for instructions on importing notebook examples into your workspace.

* [Tutorial: Analyze data with glm](https://docs.databricks.com/aws/en/sparkr/glm-tutorial.md)
* [Tutorial: Create and manage Delta Lake tables](https://docs.databricks.com/aws/en/delta/tutorial.md)
* [Tutorial: Load and transform data using Apache Spark DataFrames](https://docs.databricks.com/aws/en/getting-started/dataframes.md)

## Comparing SparkR and sparklyr

Databricks supports two APIs that provide an R interface to Apache Spark: [SparkR](https://spark.apache.org/docs/latest/sparkr.html) and [sparklyr](https://spark.rstudio.com/).

For a comparison of SparkR and sparklyr, see [Comparing SparkR and sparklyr](https://docs.databricks.com/aws/en/sparkr/sparkr-vs-sparklyr.md).

### SparkR

> [!IMPORTANT]
>
> SparkR in Databricks is [deprecated](https://docs.databricks.com/aws/en/release-notes/runtime/databricks-runtime-ver.md#platform-feature-lifecycle) in Databricks Runtime 16.0 and above. Databricks recommends migrating to [sparklyr](https://docs.databricks.com/aws/en/sparkr/sparklyr.md). See [Migrate from SparkR to sparklyr](https://docs.databricks.com/aws/en/sparkr/sparkr-migration.md).

SparkR is an R interface to Apache Spark that provides a distributed data frame implementation. SparkR supports operations like selection, filtering, and aggregation (similar to R data frames) but on large datasets. The following articles provide an introduction to [SparkR](https://spark.apache.org/docs/latest/sparkr.html).

* [SparkR overview](https://docs.databricks.com/aws/en/sparkr/overview.md)
* [Tutorial: Analyze data with glm](https://docs.databricks.com/aws/en/sparkr/glm-tutorial.md)

### sparklyr

sparklyr is an R interface to Apache Spark that provides functionality similar to [dplyr](https://spark.rstudio.com/guides/dplyr.html), `broom`, and [DBI](https://spark.rstudio.com/get-started/prepare-data.html#using-sql). The following article provides an introduction to [sparklyr](https://rdocumentation.org/packages/sparklyr).

* [sparklyr](https://docs.databricks.com/aws/en/sparkr/sparklyr.md)

### Work with DataFrames and tables in R

The following article describes how to use R, SparkR, sparklyr, and dplyr to work with R data.frames, Spark DataFrames, and Spark tables in Databricks.

* [Work with DataFrames and tables in R](https://docs.databricks.com/aws/en/sparkr/dataframes-tables.md)

## <a id="manage-code-with-notebooks-and-databricks-git-folders" />Manage code with notebooks and Databricks Git folders

Databricks [notebooks](https://docs.databricks.com/aws/en/notebooks/index.md) support R. These notebooks provide functionality similar to that of Jupyter, but with additions such as built-in visualizations using big data, Apache Spark integrations for debugging and performance monitoring, and MLflow integrations for tracking machine learning experiments. Get started by [importing a notebook](https://docs.databricks.com/aws/en/notebooks/notebook-export-import.md#import-notebook). Once you have access to a cluster, you can [attach a notebook](https://docs.databricks.com/aws/en/notebooks/notebook-compute.md#attach) to the cluster and [run the notebook](https://docs.databricks.com/aws/en/notebooks/run-notebook.md).

Databricks [Git folders](https://docs.databricks.com/aws/en/repos/index.md) allows users to synchronize notebooks and other files with Git repositories. Databricks Git folders help with code versioning and collaboration, and it can simplify importing a full repository of code into Databricks, viewing past notebook versions, and integrating with IDE development. Get started by [cloning a remote Git repository](https://docs.databricks.com/aws/en/repos/git-operations-with-repos.md). You can then open or create notebooks with the repository clone, [attach the notebook](https://docs.databricks.com/aws/en/notebooks/notebook-compute.md#attach) to a cluster, and [run the notebook](https://docs.databricks.com/aws/en/notebooks/run-notebook.md).

## Clusters

Databricks [compute](https://docs.databricks.com/aws/en/compute/index.md) provide compute management for both single nodes and large clusters. You can customize cluster hardware and libraries according to your needs. Once you have access to a cluster, you can [attach a notebook](https://docs.databricks.com/aws/en/notebooks/notebook-compute.md#attach) to the cluster or run a job on the cluster.

* For small workloads which only require single nodes, data scientists can use [single node compute](https://docs.databricks.com/aws/en/compute/configure.md#single-node) for cost savings.
* For detailed tips, see [Classic compute configuration best practices](https://docs.databricks.com/aws/en/compute/cluster-config-best-practices.md).
* Administrators can set up [cluster policies](https://docs.databricks.com/aws/en/admin/clusters/policies.md) to simplify and guide cluster creation.

### Single node R and distributed R

Databricks clusters consist of an Apache Spark *driver* node and zero or more Spark *worker* (also known as *executor*) nodes. The driver node maintains attached notebook state, maintains the `SparkContext`, interprets notebook and library commands, and runs the Spark master that coordinates with Spark executors. Worker nodes run the Spark executors, one Spark executor per worker node.

A *single node* cluster has one driver node and no worker nodes, with Spark running in local mode to support access to tables managed by Databricks. Single node clusters support RStudio, notebooks, and libraries, and are useful for R projects that don't depend on Spark for big data or parallel processing. See [Single-node compute](https://docs.databricks.com/aws/en/compute/configure.md#single-node).

For data sizes that R struggles to process (many gigabytes or petabytes), you should use multiple-node or *distributed* clusters instead. Distributed clusters have one driver node and one or more worker nodes. Distributed clusters support not only RStudio, notebooks, and libraries, but R packages such as SparkR and sparklyr, which are uniquely designed to use distributed clusters through the `SparkContext`. These packages provide familiar SQL and DataFrame APIs, which enable assigning and running various Spark tasks and commands in parallel across worker nodes. To learn more about sparklyr and SparkR, see [Comparing SparkR and sparklyr](https://docs.databricks.com/aws/en/sparkr/sparkr-vs-sparklyr.md).

Some SparkR and sparklyr functions that take particular advantage of distributing related work across worker nodes include the following:

* [sparklyr::spark\_apply](https://spark.rstudio.com/guides/distributed-r.html): Runs arbitrary R code at scale within a cluster. This is especially useful for using functionality that is available only in R, or R packages that are not available in Apache Spark nor other Spark packages.
* [SparkR::dapply](https://spark.apache.org/docs/latest/api/R/reference/dapply.html): Applies the specified function to each partition of a `SparkDataFrame`.
* [SparkR::dapplyCollect](https://spark.apache.org/docs/latest/api/R/reference/dapplyCollect.html): Applies the specified function to each partition of a `SparkDataFrame` and collects the results back to R as a `data.frame`.
* [SparkR::gapply](https://spark.apache.org/docs/latest/api/R/reference/gapply.html): Groups a `SparkDataFrame` by using the specified columns and applies the specified R function to each group.
* [SparkR::gapplyCollect](https://spark.apache.org/docs/latest/api/R/reference/gapplyCollect.html): Groups a `SparkDataFrame` by using the specified columns, applies the specified R function to each group, and collects the result back to R as a `data.frame`.
* [SparkR::spark.lapply](https://spark.apache.org/docs/latest/api/R/reference/spark.lapply.html): Runs the specified function over a list of elements, distributing the computations with Spark.

For examples, see the notebook [Distributed R: User Defined Functions in Spark](https://www.databricks.com/notebooks/gallery/DistributedRUserDefinedFunctions.html).

### Databricks Container Services

[Databricks Container Services](https://docs.databricks.com/aws/en/compute/custom-containers.md) lets you specify a Docker image when you create a cluster. Databricks provides the [databricksruntime/rbase](https://hub.docker.com/r/databricksruntime/rbase) base image on Docker Hub as an example to launch a Databricks Container Services cluster with R support. See also the [Dockerfile](https://github.com/databricks/containers/blob/master/ubuntu/R/Dockerfile) that is used to generate this base image.

## Libraries

Databricks clusters use the Databricks Runtime, which provides many popular libraries out-of-the-box, including Apache Spark, Delta Lake, and more. You can also install additional third-party or custom R packages into libraries to use with notebooks and jobs.

Start with the default libraries in the [Databricks Runtime release notes versions and compatibility](https://docs.databricks.com/aws/en/release-notes/runtime/index.md). Use [Databricks Runtime for Machine Learning](https://docs.databricks.com/aws/en/machine-learning/index.md) for machine learning workloads. For full lists of pre-installed libraries, see the “Installed R libraries” section for the target Databricks Runtime in [Databricks Runtime release notes versions and compatibility](https://docs.databricks.com/aws/en/release-notes/runtime/index.md).

You can customize your environment by using [Notebook-scoped R libraries](https://docs.databricks.com/aws/en/libraries/notebooks-r-libraries.md), which allow you to modify your notebook or job environment with libraries from CRAN or other repositories. To do this, you can use the familiar [install.packages](https://www.rdocumentation.org/packages/utils/versions/3.6.2/topics/install.packages) function from `utils`. The following example installs the [Arrow R package](https://arrow.apache.org/docs/r/) from the default CRAN repository:

```r
install.packages("arrow")
```

If you need an older version than what is included in the Databricks Runtime, you can use a notebook to run [install\_version](https://www.rdocumentation.org/packages/devtools/versions/1.13.6/topics/install_version) function from `devtools`. The following example installs [dplyr](https://dplyr.tidyverse.org/) version 0.7.4 from CRAN:

```r
require(devtools)

install_version(
  package = "dplyr",
  version = "0.7.4",
  repos   = "http://cran.r-project.org"
)
```

Packages installed this way are available across a cluster. They are scoped to the user who installs them. This enables you to install multiple versions of the same package on the same compute without creating package conflicts.

You can install other libraries as [Compute-scoped libraries](https://docs.databricks.com/aws/en/libraries/cluster-libraries.md) as needed, for example from CRAN. To do this, in the cluster user interface, click **Libraries > Install new > CRAN** and specify the library's name. This approach is especially important for when you want to call user-defined functions with SparkR or sparklyr.

For more details, see [Install libraries](https://docs.databricks.com/aws/en/libraries/index.md).

To install a [custom package](https://r-pkgs.org/) into a library:

1. Build your custom package from the command line or by using [RStudio](https://support.rstudio.com/hc/en-us/articles/200486488-Developing-Packages-with-the-RStudio-IDE).
2. Copy the custom package file from your development machine over to your Databricks workspace. For options, see [Install libraries](https://docs.databricks.com/aws/en/libraries/index.md).
3. Install the custom package into a library by running `install.packages`.

   For example, from a notebook in your workspace:

   ```r
   install.packages(
     pkgs  = "/path/to/tar/file/<custom-package>.tar.gz",
     type  = "source",
     repos = NULL
   )
   ```

   Or:

   ```bash
   %sh
   R CMD INSTALL /path/to/tar/file/<custom-package>.tar.gz
   ```

After you install a custom package into a library, add the library to the search path and then load the library with a single command.

For example:

```r
# Add the library to the search path one time.
.libPaths(c("/path/to/tar/file/", .libPaths()))

# Load the library. You do not need to add the library to the search path again.
library(<custom-package>)
```

To install a custom package as a library on *each* node in a cluster, you must use [What are init scripts?](https://docs.databricks.com/aws/en/init-scripts/index.md).

## Visualizations

Databricks R notebooks support various types of visualizations using the `display` function.

* [Visualizations in R](https://docs.databricks.com/aws/en/visualizations/legacy-visualizations.md#visualizations-in-r)

## Jobs

You can automate R workloads as scheduled or triggered notebook job in Databricks.

* For details on creating a job via the UI, see [Configure and edit Lakeflow Jobs](https://docs.databricks.com/aws/en/jobs/configure-job.md).
* The [Jobs API](https://docs.databricks.com/api/workspace/jobs) allows you to create, edit, and delete jobs.
* The [Databricks CLI](https://docs.databricks.com/aws/en/dev-tools/cli/index.md) provides a convenient command line interface for calling the Jobs API.

## Machine learning

Databricks supports a wide variety of machine learning (ML) workloads, including traditional ML on tabular data, deep learning for computer vision and natural language processing, recommendation systems, graph analytics, and more. For general information about machine learning on Databricks, see [Databricks Runtime for Machine Learning](https://docs.databricks.com/aws/en/machine-learning/index.md).

For ML algorithms, you can use pre-installed libraries in [Databricks Runtime for Machine Learning](https://docs.databricks.com/aws/en/machine-learning/index.md). You can also [install custom libraries](https://docs.databricks.com/aws/en/libraries/index.md).

For machine learning operations (MLOps), Databricks provides a managed service for the open source library MLflow. With [MLflow Tracking](https://docs.databricks.com/aws/en/mlflow/tracking.md) you can record model development and save models in reusable formats. You can use the [MLflow Model Registry](https://docs.databricks.com/aws/en/machine-learning/manage-model-lifecycle/index.md) to manage and automate the promotion of models towards production. [Jobs](https://docs.databricks.com/aws/en/jobs/index.md) and [Model Serving](https://docs.databricks.com/aws/en/machine-learning/model-serving/index.md) allow hosting models as batch and streaming jobs as REST endpoints. For more information and examples, see the [MLflow on Databricks](https://docs.databricks.com/aws/en/mlflow/index.md) or the [MLflow R API docs](https://mlflow.org/docs/latest/R-api.html).

## R developer tools

In addition to Databricks notebooks, you can also use the following R developer tools:

* [RStudio on Databricks](https://docs.databricks.com/aws/en/sparkr/rstudio.md)
* [Shiny on Databricks](https://docs.databricks.com/aws/en/sparkr/shiny.md)
* [`renv` on Databricks](https://docs.databricks.com/aws/en/sparkr/renv.md)

- Use SparkR and RStudio Desktop with [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md).
- Use sparklyr and RStudio Desktop with [Databricks Connect](https://docs.databricks.com/aws/en/dev-tools/databricks-connect/index.md).

## R session customization

In Databricks Runtime 12.2 LTS and above, R sessions can be customized by using site-wide profile (`.Rprofile`) files. R notebooks will source the file as R code during startup. To modify the file, find the value of `R_HOME` and modify `$R_HOME/etc/Rprofile.site`.

Databricks has added configuration in the file to ensure proper functionality for [hosted RStudio](https://docs.databricks.com/aws/en/sparkr/hosted-rstudio-server.md). Removing any of it may cause RStudio to not work as expected.

In Databricks Runtime 11.3 LTS and below, this behavior can be enabled by setting the environment variable `DATABRICKS_ENABLE_RPROFILE=true`.

## Additional resources

* [Knowledge Base](https://kb.databricks.com/r-aws.html)
