1

When editing an .R-file I see that ess-r-mode is on ("ESS[R]" in the status line). If an R session is opened in another buffer, I see that inferior-ess-r-mode is on there ("iESS [R:1]").

In the R-session buffer I see that there's TAB completion: it completes a string with possible function/variable names, and within a function's parentheses it gives list of arguments. That's very convenient. In the .R-file buffer, on the other hand, this doesn't happen.

Is it possible to have this kind of completion in the buffer editing the .R-file, that is, in ess-r-mode?

I see that ess-use-company and ess-use-eldoc are both t, at their standard values.

2 Answers 2

1

As you've discovered, ess-r-mode doesn't provide completion until it's associated with an R process. This is because "In R versions newer than 2.14.1, ESS uses R's built-in completion engine" ESS Manual. Which means there's nothing to complete without an R process connected.

I don't think there's a way to automatically attach a script buffer to an R process - ESS would have to know if you wanted multiple scripts to share the same process, or to each get their own process, or some combination.

My work-around: in practice, I usually want to run code from my script as soon as I open it (such as loading packages or data), which means completion is available as soon as I do. If I want completion before I've written any code at all, I will usually just send a ls() to get R started, and then work from there.

Update:

Another work around is to automatically call ess-switch-process (i.e., C-c C-s) when you open a file in r-mode, the following code in your init should do it:

(add-hook 'ess-r-mode-hook 'my-start-r-process)

(defun my-start-r-process ()
  (ess-switch-process))

or even just the single line:

(add-hook 'ess-r-mode-hook 'ess-switch-process)
3
  • Thank you for the explanation and work-around! I think I can just write a macro to call C-c C-s directly after the R session is started; or something to that effect.
    – pglpm
    Nov 6, 2023 at 19:08
  • 1
    @pglpm I misunderstood what you meant. You can use a hook to call C-c C-s, I'll update the answer
    – Tyler
    Nov 6, 2023 at 19:37
  • Fantastic, thank you!
    – pglpm
    Nov 6, 2023 at 20:15
1

I just tried, ess-r-mode do have completion. Maybe check the value of company-backends. I have
(company-R-library company-R-args company-R-objects :separate) in it, did not need to set it myself though.

1
  • Thank you Sean. I discovered that completion gets activated if I "attach" the file-buffer with an R session, with C-c C-s. I'm trying to understand whether the attachment can be done automatically. But maybe with company I don't need to do this.
    – pglpm
    Nov 4, 2023 at 21:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.