3

I use ess to run R and have encountered an annoying change that now when I start an R process with ess it defaults to starting in what is apparently the root of the project not the directory of the file in the buffer I'm in. For example if I'm editing a file in an R package that is in the ~/projects/my_package/tests/ directory and press C-c r I am prompted with:

"R starting project directory? ~/projects/my/package/"

But a previous behaviour which I think is more intuitive was for the default to start in the directory that the file i saved (~/projects/my_package/tests/).

I read this: https://ess.r-project.org/Manual/ess.html#Customizing-startup and interpret that ess determines the working directory in some way that I am not happy with. Is there a way to change this behaviour?

1 Answer 1

3

The documentation you linked is unfortunately a little bit outdated. Currently, it says:

  1. If the variable ess-directory-function stores the name of a function, the value returned by this function is used. The default for this variable is nil.
  2. Otherwise, if the variable ess-directory stores the name of a directory (ending in a slash), this value is used. The default for this variable is nil.
  3. Otherwise, the working directory of the current buffer is used.

On the other hand, ESS currently defines inferior-ess--get-startup-directory as:

(defun inferior-ess--get-startup-directory ()
  "Return a startup directory."
  (let ((dir (or (when (boundp 'ess-startup-directory)
                   (if (symbolp ess-startup-directory)
                       (symbol-value ess-startup-directory)
                     ess-startup-directory))
                 (and ess-startup-directory-function
                      (funcall ess-startup-directory-function))
                 (when-let ((proj (project-current)))
                   (ess--project-root proj))
                 default-directory)))
    (directory-file-name dir)))

(ess-directory is an alias for ess-startup-directory and ess-directory-function an alias for ess-startup-directory-function.)

In other words:

  • Steps 1. and 2. are swapped; and
  • Prior to step 3., project-current is consulted if it can be used instead of default-directory.

If you want to prevent project-current from being consulted, you can define your own ess-startup-directory-function that always returns default-directory:

(defun my-ess-startup-directory-function ()
  "Force ESS to use `default-directory' as its startup directory."
  default-directory)

(setq ess-startup-directory-function 'my-ess-startup-directory-function)
3
  • This answer solved my problem too, but now it doesn't seem to work anymore. Probably the ESS code has changed again?
    – pglpm
    Commented Dec 14, 2022 at 9:56
  • It took me a while to implement in my config but today it is working for me do as d125q suggests in the last code block. Now when I ask emacs to start R, the working directory defaults to the parent directory of the file in the buffer or the directory I'm in if the buffer is a dired buffer. Perfect!
    – trosendal
    Commented Mar 3, 2023 at 13:43
  • 1
    @pglpm See the help for ess-startup-directory. The documentation listed above is incomplete - ESS now checks to see if you are in a "project" before deciding which directory to use, that might be causing your problem
    – Tyler
    Commented Jul 31, 2023 at 15:02

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.