3

I am trying to migrate from RStudio to emacs ess and i am trying now to find auto-completion functionality in emacs ess similar to Rstudio

For my question i'll use a simple RStudio example and i'll appreciate if you could tell me how to do the same in emacs ess (The purpose is of course to generalize):

in RStudio

when i type the text libr and then press TAB I get library()

inside the parentesis

when i type "tidyv" like that: library(tidyv) and then TAB i get library(tidyverse)

Could you please describe in a simple step by step manner how i can do the same in emacs ess?

Thanks

Rafael

Arnab
  • 4,216
  • 2
  • 28
  • 50
Rafael Zanzoori
  • 531
  • 1
  • 7
  • 23
  • 1
    This is a weak help because once my emacs does what I want I promptly forget how I made it work :). I have added: (require 'auto-complete-config) (require 'auto-complete) (ac-config-default) (setq ess-tab-complete-in-script t) To my '.emacs' ofcourse you need to install these first before it will work. – trosendal Mar 12 '18 at 10:20
  • 1
    i think there is a pull request pending for exactly this feature: https://github.com/emacs-ess/ESS/pull/502 for general autocompletion see here: https://stackoverflow.com/questions/16075012/emacs-ess-auto-complete?rq=1 – gdkrmr Mar 13 '18 at 19:07
  • Thanks trosendal. adding those statements to my .emacs file indeed provides auto completions for function names and arguments. However in the case of the library function no auto completions are provided for packages names. Any ideas? – Rafael Zanzoori Mar 14 '18 at 10:53
  • according to the recent docs, ess prefers company mode. However, I am unable to get that working at all. Would really appreciate any info if you figure it out. I am running Windows 10 64-bit, R 3.4.3 64-bit – Vince W. Mar 18 '18 at 13:39

2 Answers2

5

This should work for you with the default company-backend after a recent backend addition. You will just need to update your ESS from melpa. If company-R-library is not part of your company-backends in an ESS buffer, just add it in your ESS mode hook. Note that you should have set ess-use-company.

In response to comment, ess-use-company is a variable you should set to t in your config, not a function to call with M-x. I'll assume you have an init file, if not you can easily find information about that. Wherever you keep your config for ess in your init file, you could add (although this is the default, so unless you set ess-use-auto-complete, this is probably already set)

(setq ess-use-company t)

Every mode has a hook, which is a function it runs after it is setup in a buffer, allowing users to customize the mode. You can add such a hook using (add-hook 'ess-mode-hook 'my-ess-mode-hook), where my-ess-mode-hook is a function you write with your customizations, eg. (company-mode) unless you have that turned on globally (most likely).

company-backends is also a variable, from an ESS buffer you can type M-:company-backends to evaluate lisp code to see the value of that variable. Make sure you reinstall the latest version of ESS from melpa.

Edit: full init to test -- company-R-library may not have been added to default backends

(setq-default package-archives 
              '(("melpa"        . "http://melpa.milkbox.net/packages/")
                ("gnu"          . "http://elpa.gnu.org/packages/")))
(setq package-enable-at-startup nil)
(package-initialize)

;;; company
(require 'company)
(setq tab-always-indent 'complete)

(setq company-idle-delay 0.5
      company-show-numbers t
      company-minimum-prefix-length 2
      company-tooltip-flip-when-above t)

(global-set-key (kbd "C-M-/") #'company-complete)
(global-company-mode)

;;; ESS
(defun my-ess-hook ()
  ;; ensure company-R-library is in ESS backends
  (make-local-variable 'company-backends)
  (cl-delete-if (lambda (x) (and (eq (car-safe x) 'company-R-args))) company-backends)
  (push (list 'company-R-args 'company-R-objects 'company-R-library :separate)
        company-backends))

(add-hook 'ess-mode-hook 'my-ess-hook)

(with-eval-after-load 'ess
  (setq ess-use-company t))

Start emacs with

emacs -Q -l /path/to/this/init.el

Open an R file, start the inferior R process, then type library(ti M-C-/ and you should get package completions.

Rorschach
  • 31,301
  • 5
  • 78
  • 129
  • Hi. Thanks for taking the time to answer. I installed the latest ess and company package from melpa. when i type ALT-X ess-use-c i can't find uss-use-company in my autocompletions . the same is for company-R-library. Since i'm a very new emacs user it is quite possible i didn't understand the part of your answer dealing with adding it to my ess hook. I'll appreciate it if you could provide how to do it in a step by step manner. Thanks – Rafael Zanzoori Apr 10 '18 at 08:01
  • Hi @jenesaisquoi . i have added (setq ess-use-company t) to my emacs and restarted emacs. and added company mode by pressing M-x company-mode and still when i type library( library(package= and then TAB no auto completions for libraries are provided. my ess version is 17.11 any ideas? – Rafael Zanzoori Apr 10 '18 at 10:01
  • did you start an ess inferior R process? completions come from the running R process – Rorschach Apr 10 '18 at 11:22
  • Hi @jenesaisquoi . Yes. After opening a buffer called test.r i pressed the R icon on the toolbar and then sent an ls() command and got a response. and then when i type in the buffer library( or library(package= and then TAB and no auto completions for libraries are provided. What is company-R-library that you mentioned in your original response and how can i verify it's existence? – Rafael Zanzoori Apr 10 '18 at 12:34
  • I wrote how already, evaluate the value of company-backend so in your ess buffer. It won’t complete there, but after “ti” it should complete for tidyr, etc. it might be worth going back and modifying the backend to complete all packages without a prefix, but it seems overkill to me – Rorschach Apr 10 '18 at 12:38
  • What is tab bound to? Probably not company-complete, which I bind to M-/ for example. You should call m-x company-complete to test instead of tab – Rorschach Apr 10 '18 at 12:44
  • @RafaelZanzoori the backend may not have been added to the default backends, you can use the above init file to test it out – Rorschach Apr 10 '18 at 13:09
  • Hi @jenesaisquoi. I saved init.el with the contents above in c:\Emacs and type emacs -Q -l c:/Emacs/init.el but when emacs loads i get a message command-line-1: Cannot open load file: No such file or directory, c:/Emacs/init.el so i have replaced the contents of my .emacs file with the contents of init.el and restarted emacs but now i get the following messages: Company backend ’company-R-library’ could not be initialized: Symbol’s function definition is void: company-R-library. I appreciate your patience – Rafael Zanzoori Apr 11 '18 at 08:42
  • you should figure out how to use `emacs -Q -l` on windows as that way you start with a clean emacs and run the code. I don't use windows so I can;t help you there. That error sounds like you haven;t loaded a recent ESS – Rorschach Apr 11 '18 at 11:38
  • Hi @jenesaisquoi. It took me some time but i finally installed the latest version of emacs and ess and company package and it works. i think it didn't work the last time i tried because something in my .emacs file interfered with it. it works with an empty .emacs file. Thank you very much for your help. – Rafael Zanzoori May 13 '18 at 09:59
  • @jenesaisquoi Hi, I've just upgraded to Debian10 and now Emacs version is 26.1, ess-version is 18.10.2 and company-mode is not in melpa-stable anymore. Looks like company-mode have been incorporated to Emacs. Any code/library completion working. Could you please possible give me some help? See .emacs in paste.debian.net/1096873 – fred Aug 21 '19 at 20:04
  • @fred I don't understand your question. Consider posting a new question, preferably with just the relevant part of your init – Rorschach Aug 29 '19 at 12:48
1

Okay, so I have it working for me, quite well with the following setup and steps:

  • Windows 10 x64
  • R 3.4.3 x64
  • Emacs 25 x64 installed normally
  • MELPA repo enabled in init.el
  • package-install [RET] company
  • package-install [RET] ess
  • open a new R file in some directory
  • M-x company-mode to enable company-mode in the current buffer
  • `C-c C-z' to start an inferior R process (will not work without this)

At this point, autocomplete automatically pops up completion suggestions on R objects. Autocomplete has to be triggered for inside parentheses. Say I have my cursor inside the function parentheses for shiny:runApp(). To get completion suggestions for the function arguments, I need to run the command company-complete.

You an do this with M-x company-complete or M-x company-complete-common and you should see the following:

enter image description here

I decided to bind this to Shift-Tab with the following line in my init.el:

(global-set-key (kbd "<backtab>") 'company-complete-common)

And to have company mode enabled globally with:

(add-hook 'after-init-hook 'global-company-mode)
Vince W.
  • 3,561
  • 3
  • 31
  • 59
  • Hi Vince. Using that configuration can you auto-complete the following? library(tidy + TAB . The expected result should be library(tidyverse). for some reason i can't find a way to autocomplete packages names and data.frames field names like this : iris %>% select(Spe + TAB should yield iris %>% select(Species). In RStudio it works without problems. Thanks – Rafael Zanzoori Mar 19 '18 at 08:39
  • No that doesn't work :-(. I just tried it. It autocompletes the function argument options for the library functions, and it will autocomplete functions and objects within the package name-space but it does not autocomplete the names of the package. You might want to open an issue in the ESS github repo – Vince W. Mar 19 '18 at 11:42