Emulate bash prompt behavior in R console by replacing R's default prompt of > with the base::getwd() string. RStudio does display the current working directory in the Console window title bar, but it is not where I normally look. The global environment can be cleared without affecting the prompt behavior.

Details

A \n is appended to the prompt. In RStudio, the \n is eaten. In R (tested on OS X), it is honored.

Installation

devtools::install_github("dgabbe/wdprompt@Current")

To start manually:

wdprompt::start_wd()

To start automatically, add these lines to .First in Rprofile.site:

if (interactive()) {
  #
  # wdprompt::stop_wd() if you want to turn off the prompt.
  #
  if ( length(find.package("wdprompt", quiet = TRUE)) != 0 ) {
    wdprompt::start_wd()
  }
}

Removal

To stop the prompt and revert back to the prompt before wdprompt was started:

wdprompt::stop_wd()

Options

wdprompt.enabled

TRUE to use the prompt. FALSE to stop the prompt and revert back to a static prompt. See start_wd for the string that is used.

wdprompt.fullPath

TRUE to display the full path name. FALSE to to show a truncated prompt. See wd_prompt for the details.

wdprompt.promptLen

number that determines the length of truncated prompt.

R Triva

The taskCallback is invoked at the end of each top-level task. Roughly translated, that means when R successfully evaluates an expression typed in the console. start_wd adds the taskCallback, passing in the current prompt and then executes wd_prompt to set the prompt to be this new one.

Random Thoughts

My motivation for having the console prompt act the same as my bash prompt was creating a uniform command line environment. Experience has taught me that many problems with a build or deploy can be traced back to simply issuing the wrong command in the wrong directory.

Unfortunately R does not provide this behavior by default and simply setting the prompt option in one of the R init files is brittle. A better solution is creating a taskCallback. There's enough code that I pulled it out of Rprofile.site. The taskCallbackManager creates a copy of your code so one of the only ways to control it is with options. During development, I discovered it's very easy to instantiate multiple instances of a callback so having the code check an option for when to stop made it simple to terminate all instances.

References

See also