reddit / emacs
Using lsp-mode with R
velbunadur · 2022-05-27 10:51
Can anyone advise me how to set up the R language server with lsp-mode? I already have lsp-mode working well with Python and Julia but I can't find out how to get it running with R. I've installed the language server package in R but I'm not sure what I have to do to have it start automatically when I open an R code file like it does with the other languages. Thanks.
1419538 · 2022-05-27 12:54
You need both R package and language server in emacs to be installed
velbunadur · 2022-05-27 13:00
I have ESS installed. What else do I need?
1419538 · 2022-05-27 13:06
Lsp server for R like py-right for python. Check [this](https://github.com/REditorSupport/languageserver)
velbunadur · 2022-05-27 13:11
I've done that but can't get it to load. I've added the code below to my init file but it's still not loading
(with-eval-after-load 'lsp-mode
(lsp-register-client (make-lsp-client :new-connection
(lsp-stdio-connection '("R" "--slave" "-e" "languageserver::run()"))
:major-modes '(ess-r-mode inferior-ess-r-mode)
:server-id 'lsp-R)))
1419538 · 2022-05-27 13:17
I don’t have any config relating to lsp in my config file bc I’m using doom emacs. Did you try M-x install-server or lsp-install (I’m on mobile and can’t remember exactly). Once you install languageserver it should initialize automatically
velbunadur · 2022-05-27 13:21
No I haven't. I'll look into that. Thanks!
velbunadur · 2022-05-27 13:32
Found the problem. I hadn't included `(ess-r-mode . lsp)` when loading the lsp-mode package. Many thanks for your help.
1419538 · 2022-05-27 14:00
No problem! Hope you find ESS better to work with than Rstudio!
velbunadur · 2022-05-27 14:33
I've used R for nearly 20 years and have used Rstudio about twice :-)
white-tanuki · 2022-05-27 16:08
From the discussion it seems ESS and LSP can be used together.
I always thought one has to choose one of them.
Can you use both ESS and LSP together?
1419538 · 2022-05-27 16:37
Yeah definitely!
white-tanuki · 2022-05-27 16:55
cool, I must look into this. A friend of mine uses R. I've only set him up with ESS so far.
1419538 · 2022-05-27 18:30
Check this video out as well: it gives some good pointers about improving your ESS experience: especially if you are used to working from Rstudio [https://www.youtube.com/watch?v=uxyhmhRVOfw&t=3s](https://www.youtube.com/watch?v=uxyhmhRVOfw&t=3s)
FreeAd7233 · 2022-05-27 18:46
ESS is part o r-project.org and some of the maintainers are even R-core devs, so it is expected that ESS will have the same life cycle with the R software… and for a commercial product, I think the answer is opaque
mahhjs · 2022-05-27 19:45
Can you show the full config you have that got this working?
velbunadur · 2022-05-27 20:56
Sure, this is what worked for me. Apologies for the poor formatting. You have to follow the instructions [here](https://emacs-lsp.github.io/lsp-mode/page/lsp-r/) too.
(use-package lsp-mode
:init (setq lsp-keymap-prefix "s-l")
:hook ((python-mode . lsp)
(ess-r-mode . lsp)
(lsp-mode . lsp-enable-which-key-integration))
:commands lsp)
(with-eval-after-load 'lsp-mode (lsp-register-client (make-lsp-client
:new-connection (lsp-stdio-connection '("R" "--slave" "-e" "languageserver::run()"))
:major-modes '(ess-r-mode inferior-ess-r-mode) :server-id 'lsp-R)))
white-tanuki · 2022-05-27 21:20
thanks, that looks really useful.:)
u3435 · 2022-08-05 17:57
I got it working also, running `install.packages(“languageserver”)` in R, but I have to use as follows (note the change in the `:major-modes` line, using `ess-mode` rather than `ess-r-mode`):
(setq ess-r-backend 'lsp)
(use-package lsp-mode
:init (setq lsp-keymap-prefix "s-l")
:hook ((python-mode . lsp)
(ess-r-mode . lsp)
(lsp-mode . lsp-enable-which-key-integration))
:commands lsp)
(with-eval-after-load 'lsp-mode
(lsp-register-client
(make-lsp-client
:new-connection (lsp-stdio-connection '("R" "--slave" "-e" "languageserver::run()"))
:major-modes '(ess-mode inferior-ess-r-mode)
:server-id 'lsp-R)))
stevebarratt · 2022-12-23 20:57
I'm struggling to get this set up like I thought it would...
In your setup does it provide completion of object variables from the dollar operator?
Like... for `dataframeA$Column1` if you press tab at `dataframe$` does it give completions based on the objects properties?
In my setup (with doom emacs and with or without your added config) I don't get the proper completions like I do with straight `ESS` without lsp.