r.md

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