Is it possible to get code completion for R in Emacs ESS similar to what is available in Rstudio?
You do get the completion thanks to the rcompgen package by Deepayan (now "promoted" into base R as part of the utils package). So when I type
lm(
and hit TAB a new buffer opens which gets me the left-hand side of your window above: the available options to the function at hand. I don't think you can show the help directly though.
There is / was also a way to get context-sensitive help in the mini-buffer when typing but I have forgottten how/where that gets turned on.
emacs ess : how to auto complete library function
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.
Emacs autocomplete-mode extension for ESS and R
There is an ac-source for R here. I recall struggling with it and finally wrote my own which at that time was much faster, but buggy since I didn't manage to make prefix regexp work properly.
EDIT: the newest ESS (only svn currently) has out-of-the-box integration with auto-complete. I have added the instructions to the wiki.
Asking ESS/R users for suggestions for elisp codes in .emacs file
For me, this thing is fantastically useful:
;; ESS: Emacs Speaks Statistics
(load "~/elisp/vendor/ess/lisp/ess-site.el")
;; Use shift-enter to split window & launch R (if not running), execute highlighted
;; region (if R running & area highlighted), or execute current line
;; (and move to next line, skipping comments). Nice.
;; See http://www.emacswiki.org/emacs/EmacsSpeaksStatistics,
;; FelipeCsaszar. Adapted to spilit vertically instead of
;; horizontally.
(setq ess-ask-for-ess-directory nil)
(setq ess-local-process-name "R")
(setq ansi-color-for-comint-mode 'filter)
(setq comint-prompt-read-only t)
(setq comint-scroll-to-bottom-on-input t)
(setq comint-scroll-to-bottom-on-output t)
(setq comint-move-point-for-output t)
(defun my-ess-start-R ()
(interactive)
(if (not (member "*R*" (mapcar (function buffer-name) (buffer-list))))
(progn
(delete-other-windows)
(setq w1 (selected-window))
(setq w1name (buffer-name))
(setq w2 (split-window w1 nil t))
(R)
(set-window-buffer w2 "*R*")
(set-window-buffer w1 w1name))))
(defun my-ess-eval ()
(interactive)
(my-ess-start-R)
(if (and transient-mark-mode mark-active)
(call-interactively 'ess-eval-region)
(call-interactively 'ess-eval-line-and-step)))
(add-hook 'ess-mode-hook
'(lambda()
(local-set-key [(shift return)] 'my-ess-eval)))
(add-hook 'inferior-ess-mode-hook
'(lambda()
(local-set-key [C-up] 'comint-previous-input)
(local-set-key [C-down] 'comint-next-input)))
(require 'ess-site)
Taken from: http://www.kieranhealy.org/blog/archives/2009/10/12/make-shift-enter-do-a-lot-in-ess/
source code for autocompletion in R run in terminal
The completion code is actually in the base instalation in the utils package. You can view everything associated with it (for the devel version) on R's SVN server:
http://svn.r-project.org/R/trunk/src/library/utils/R/completion.R
This code should be read in conjunction with ?completion
.
How to suggest hints to Rstudio for auto completion for my code?
For objects that inherit from the tbl
class, RStudio does indeed call tbl_vars()
to populate completions. (This is an RStudio-specific autocompletion system feature.)
In your example, the object you're creating does not inherit from tbl
, so this autocompletion pathway doesn't kick in.
However, this form of 'ad-hoc' S3 dispatch (where you define S3 methods directly as code like this) is not detected by RStudio, so you won't be able to verify this with test code like this. You'll have to explicitly define and register the S3 method in an R package.
Alternatively, you can try explicitly registering the S3 method with something like:
registerS3method("tbl_vars", "test_auto_complete", tbl_vars.test_auto_complete)
for inline testing.
Customizing the ESS environment for R
I am not using autoyas as I find auto-complete integration a better approach.
Insertion of previously defined symbols is a general emacs functionality called 'dabbrev-expand' and is bound to M-/. I have this in my .emacs to make it complete on full symbols:
(setq dabbrev-abbrev-char-regexp "\\sw\\|\\s_\\|s.")
(setq dabbrev-case-fold-search t)
Another thing which I use extensively is imenu-based-jump-to-symbol-definition. It offers similar functionality to emacs tags, but just for open buffers in the same mode as the current buffer. It also uses IDO for queries:
Put imenu-anywhere.el
into your emacs load path and add this:
(require 'imenu-anywhere)
(global-set-key [?\M-o] 'imenu-anywhere)
Now, if I do M-o foo RET emacs jumps to the function/class/method/generic definition of 'foo' as long as 'foo' is defined in one of the open buffers. This of course works whenever a mode defines imenu-tags. ESS defines those, so you should not need to add more.
There is also somewhere a collection of R-yas templates. I didn't get around to starting using them but my guess is that it's a pretty efficient template insertion mechanism.
[edit] Activate tracebug:
(setq ess-use-tracebug t)
Related Topics
HTML with Multicolumn Table in Markdown Using Knitr
Street Address to Geolocation Lat/Long
Rmarkdown: Pandoc: PDFlatex Not Found
Difference Between Paste() and Paste0()
How to Hide Code in Rmarkdown, with Option to See It
How to Do Selective Labeling with Ggplot Geom_Point()
Plotting the Average Values for Each Level in Ggplot2
How to Separate Title Page and Table of Content Page from Knitr Rmarkdown PDF
Ggally::Ggpairs Plot Without Gridlines When Plotting Correlation Coefficient
Download Attachment from an Outlook Email Using R
How to Extract Everything Until First Occurrence of Pattern
How to Get My Blogdown Blog on R-Bloggers
Dplyr Join Warning: Joining Factors with Different Levels
Why Is Seq(X) So Much Slower Than 1:Length(X)
Dplyr::First() to Choose First Non Na Value
Installing a Package Offline from Github
Confidence Intervals for Predictions from Logistic Regression