1# R
2
3R support is available via multiple R Zed extensions:
4
5- [ocsmit/zed-r](https://github.com/ocsmit/zed-r)
6 - Tree-sitter: [r-lib/tree-sitter-r](https://github.com/r-lib/tree-sitter-r)
7 - Language-Server: [REditorSupport/languageserver](https://github.com/REditorSupport/languageserver)
8
9- [posit-dev/air](https://github.com/posit-dev/air/tree/main/editors/zed)
10 - Formatter: [posit-dev/air](https://posit-dev.github.io/air/)
11
12## Installation
13
141. [Download and Install R](https://cloud.r-project.org/).
152. Install the R packages `languageserver` and `lintr`:
16
17```R
18install.packages("languageserver")
19install.packages("lintr")
20```
21
223. 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.
23
244. Install the [Air](https://posit-dev.github.io/air/) extension through Zed's extensions manager for R code formatting via Air.
25
26## Linting
27
28`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).
29
30```r
31linters: linters_with_defaults(
32 line_length_linter(120),
33 commented_code_linter = NULL
34 )
35exclusions: list(
36 "inst/doc/creating_linters.R" = 1,
37 "inst/example/bad.R",
38 "tests/testthat/exclusions-test"
39 )
40```
41
42Or exclude it from linting anything,
43
44```r
45exclusions: list(".")
46```
47
48See [Using lintr](https://lintr.r-lib.org/articles/lintr.html) for a complete list of options,
49
50## Formatting
51
52### Air
53
54[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.
55
56Ensure 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.
57
58Enable Air in your `settings.json`:
59
60```json [settings]
61{
62 "languages": {
63 "R": {
64 "language_servers": ["air"]
65 }
66 }
67}
68```
69
70If you use the `"r_language_server"` from `REditorSupport/languageserver`, but would still like to use Air for formatting, use the following configuration:
71
72```json [settings]
73{
74 "languages": {
75 "R": {
76 "language_servers": ["air", "r_language_server"],
77 "use_on_type_format": false
78 }
79 }
80}
81```
82
83Note 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"`.
84
85`"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.
86
87#### Configuring Air
88
89Air is minimally configurable via an `air.toml` file placed in the root directory of your project:
90
91```toml
92[format]
93line-width = 80
94indent-width = 2
95```
96
97For more details, refer to the Air documentation about [configuration](https://posit-dev.github.io/air/configuration.html).
98
99### Styler
100
101`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.
102
103<!--
104TBD: Get this working
105
106### REditorSupport/languageserver Configuration
107
108You 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`:
109
110For example to disable Lintr linting and suppress code snippet suggestions (both enabled by default):
111
112```json [settings]
113{
114 "lsp": {
115 "r_language_server": {
116 "settings": {
117 "r": {
118 "lsp": {
119 "diagnostics": false,
120 "snippet_support": false
121 }
122 }
123 }
124 }
125 }
126}
127```
128
129-->
130
131<!--
132TBD: R REPL Docs
133
134## REPL
135
136### Ark Installation
137
138To use the Zed REPL with R you need to install [Ark](https://github.com/posit-dev/ark), an R Kernel for Jupyter applications.
139You 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`.
140
141For example to install the latest non-debug build:
142
143```sh
144# macOS
145cd /tmp
146curl -L -o ark-latest-darwin.zip \
147 $(curl -s "https://api.github.com/repos/posit-dev/ark/releases/latest" | \
148 jq -r '.assets[] | select(.name | contains("darwin-universal") and (contains("debug") | not)) | .browser_download_url')
149unzip ark-latest-darwin.zip ark
150sudo mv /tmp/ark /usr/local/bin/
151```
152
153```sh
154# Linux X86_64
155cd /tmp
156curl -L -o ark-latest-linux.zip \
157 $(curl -s "https://api.github.com/repos/posit-dev/ark/releases/latest" \
158 | jq -r '.assets[] | select(.name | contains("linux-x64") and (contains("debug") | not)) | .browser_download_url'
159 )
160unzip ark-latest-linux.zip ark
161sudo mv /tmp/ark /usr/local/bin/
162```
163
164-->