r.md

  1---
  2title: R
  3description: "Configure R language support in Zed, including language servers, formatting, and debugging."
  4---
  5
  6# R
  7
  8R support is available via multiple R Zed extensions:
  9
 10- [ocsmit/zed-r](https://github.com/ocsmit/zed-r)
 11
 12  - Tree-sitter: [r-lib/tree-sitter-r](https://github.com/r-lib/tree-sitter-r)
 13  - Language-Server: [REditorSupport/languageserver](https://github.com/REditorSupport/languageserver)
 14
 15- [posit-dev/air](https://github.com/posit-dev/air/tree/main/editors/zed)
 16  - Formatter: [posit-dev/air](https://posit-dev.github.io/air/)
 17
 18## Installation
 19
 201. [Download and Install R](https://cloud.r-project.org/).
 212. Install the R packages `languageserver` and `lintr`:
 22
 23```R
 24install.packages("languageserver")
 25install.packages("lintr")
 26```
 27
 283. Install the [R](https://github.com/ocsmit/zed-r) extension through Zed's extensions manager for basic R language support (syntax highlighting, tree-sitter support) and for [REditorSupport/languageserver](https://github.com/REditorSupport/languageserver) support.
 29
 304. Install the [Air](https://posit-dev.github.io/air/) extension through Zed's extensions manager for R code formatting via Air.
 31
 32## Linting
 33
 34`REditorSupport/languageserver` bundles support for [r-lib/lintr](https://github.com/r-lib/lintr) as a linter. This can be configured via the use of a `.lintr` inside your project (or in your home directory for global defaults).
 35
 36```r
 37linters: linters_with_defaults(
 38    line_length_linter(120),
 39    commented_code_linter = NULL
 40  )
 41exclusions: list(
 42    "inst/doc/creating_linters.R" = 1,
 43    "inst/example/bad.R",
 44    "tests/testthat/exclusions-test"
 45  )
 46```
 47
 48Or exclude it from linting anything,
 49
 50```r
 51exclusions: list(".")
 52```
 53
 54See [Using lintr](https://lintr.r-lib.org/articles/lintr.html) for a complete list of options,
 55
 56## Formatting
 57
 58### Air
 59
 60[Air](https://posit-dev.github.io/air/) provides code formatting for R, including support for format-on-save. The [Air documentation for Zed](https://posit-dev.github.io/air/editor-zed.html) contains the most up-to-date advice for running Air in Zed.
 61
 62Ensure that you have installed both the [ocsmit/zed-r](https://github.com/ocsmit/zed-r) extension (for general R language awareness in Zed) and the [Air](https://posit-dev.github.io/air/) extension.
 63
 64Configure language servers in Settings ({#kb zed::OpenSettings}) under Languages > R, or add to your settings file:
 65
 66```json [settings]
 67{
 68  "languages": {
 69    "R": {
 70      "language_servers": ["air"]
 71    }
 72  }
 73}
 74```
 75
 76If you use the `"r_language_server"` from `REditorSupport/languageserver`, but would still like to use Air for formatting, configure in Settings ({#kb zed::OpenSettings}) under Languages > R, or add to your settings file:
 77
 78```json [settings]
 79{
 80  "languages": {
 81    "R": {
 82      "language_servers": ["air", "r_language_server"],
 83      "use_on_type_format": false
 84    }
 85  }
 86}
 87```
 88
 89Note that `"air"` must come first in this list, otherwise [r-lib/styler](https://github.com/r-lib/styler) will be invoked via `"r_language_server"`.
 90
 91`"r_language_server"` provides on-type-formatting that differs from Air's formatting rules. To avoid this entirely and let Air be fully in charge of formatting your R files, also set `"use_on_type_format": false` as shown above.
 92
 93#### Configuring Air
 94
 95Air is minimally configurable via an `air.toml` file placed in the root folder of your project:
 96
 97```toml
 98[format]
 99line-width = 80
100indent-width = 2
101```
102
103For more details, refer to the Air documentation about [configuration](https://posit-dev.github.io/air/configuration.html).
104
105### Styler
106
107`REditorSupport/languageserver` bundles support for [r-lib/styler](https://github.com/r-lib/styler) as a formatter. See [Customizing Styler](https://cran.r-project.org/web/packages/styler/vignettes/customizing_styler.html) for more information on how to customize its behavior.
108
109<!--
110TBD: Get this working
111
112### REditorSupport/languageserver Configuration
113
114You can configure the [R languageserver settings](https://github.com/REditorSupport/languageserver#settings) via Zed Project Settings `.zed/settings.json` or Zed User Settings `~/.config/zed/settings.json`:
115
116For example to disable Lintr linting and suppress code snippet suggestions (both enabled by default):
117
118```json [settings]
119{
120  "lsp": {
121    "r_language_server": {
122      "settings": {
123        "r": {
124          "lsp": {
125            "diagnostics": false,
126            "snippet_support": false
127          }
128        }
129      }
130    }
131  }
132}
133```
134
135-->
136
137<!--
138TBD: R REPL Docs
139
140## REPL
141
142### Ark Installation
143
144To use the Zed REPL with R you need to install [Ark](https://github.com/posit-dev/ark), an R Kernel for Jupyter applications.
145You can down the latest version from the [Ark GitHub Releases](https://github.com/posit-dev/ark/releases) and then extract the `ark` binary to a directory in your `PATH`.
146
147For example to install the latest non-debug build:
148
149```sh
150# macOS
151cd /tmp
152curl -L -o ark-latest-darwin.zip \
153    $(curl -s "https://api.github.com/repos/posit-dev/ark/releases/latest" | \
154    jq -r '.assets[] | select(.name | contains("darwin-universal") and (contains("debug") | not)) | .browser_download_url')
155unzip ark-latest-darwin.zip ark
156sudo mv /tmp/ark /usr/local/bin/
157```
158
159```sh
160# Linux X86_64
161cd /tmp
162curl -L -o ark-latest-linux.zip \
163    $(curl -s "https://api.github.com/repos/posit-dev/ark/releases/latest" \
164        | jq -r '.assets[] | select(.name | contains("linux-x64") and (contains("debug") | not)) | .browser_download_url'
165    )
166unzip ark-latest-linux.zip ark
167sudo mv /tmp/ark /usr/local/bin/
168```
169
170-->