\input texinfo   @c -*-texinfo-*-
@comment %**start of header
@setfilename ess.info
@settitle ESS -- Emacs Speaks Statistics
@comment %**end of header

@synindex pg fn

@include ess-defs.texi
@titlepage
@title ESS -- Emacs Speaks Statistics
ESS version
@ESSVER

@c --- KEEP this synchronized with ./authors.texi :
@c                                 ~~~~~~~~~~~~~~
@author The ESS Developers (A.J. Rossini, R.M. Heiberger, K. Hornik,
@author M. Maechler, R.A. Sparapani, S.J. Eglen,
@author S.P. Luque, H. Redestig, V. Spinu, L. Henry, and J.A. Branham)
@c @author (Formerly by: Doug Bates, Ed Kademan, Frank Ritter
@c @author                and David Smith)
@c @ifhtml
@c <P><HR></P>
@c @end ifhtml
@c
@author Current Documentation by The ESS Developers
@author Copyright @copyright{} 2002--2022 The ESS Developers
@author Copyright @copyright{} 1996--2001 A.J. Rossini
@author Original Documentation by David M. Smith
@author Copyright @copyright{} 1992--1995 David M. Smith
@c (@email{D.M.Smith@@lancaster.ac.uk})
@c @author Department of Mathematics and Statistics
@c @author Lancaster University, UK
@c @page
@c @vskip 0pt plus 1filll
@c @sp 2
@author Permission is granted to make and distribute verbatim copies of this
@author manual provided the copyright notice and this permission notice are
@author preserved on all copies.
@author Permission is granted to copy and distribute modified versions of this
@author manual under the conditions for verbatim copying, provided that the
@author entire resulting derived work is distributed under the terms of a
@author permission notice identical to this one.

@end titlepage

@contents

@ifnottex
@majorheading ESS --- Emacs Speaks Statistics

@dircategory Emacs
@direntry
* ESS: (ess). Emacs Speaks Statistics (R/S/S+, SAS, BUGS/JAGS and Stata).
@end direntry
@node Top
@top ESS: Emacs Speaks Statistics

ESS version
@ESSVER

@display
by  A.J. Rossini,
    R.M. Heiberger,
    K. Hornik,
    M. Maechler,
    R.A. Sparapani,
    S.J. Eglen,
    S.P. Luque,
    H. Redestig,
    V. Spinu,
    L. Henry,
and J.A. Branham.
@end display

@c (Formerly: Doug
@c Bates, Ed Kademan, Frank Ritter and David Smith).
@quotation
Emacs Speaks Statistics (ESS) provides an intelligent, consistent
interface between the user and the software.  ESS interfaces with R,
SAS, S-PLUS, BUGS/JAGS and other statistical analysis packages on
GNU/Linux, other Unix-like systems such as macOS, and Microsoft Windows.
ESS is itself a package within the Emacs text editor and uses Emacs
features to streamline the creation and use of statistical software.
ESS knows the syntax and grammar of statistical analysis packages and
provides consistent display and editing features based on that
knowledge.  ESS assists in interactive and batch execution of statements
written in these statistical analysis languages.
@end quotation

@include license.texi

@end ifnottex

@menu
* Introduction::                Overview of features provided by this package
* Installation::                Installing ESS on your system
* Interactive ESS::             Interacting with statistical programs
* Entering commands::           Interacting with the ESS process
* Evaluating code::             Sending code to the ESS process
* Transcript Mode::             Manipulating saved transcript files
* Editing objects::             How to create/edit objects and functions
* Help::                        Reading help files
* Completion::                  Various completion mechanisms
* Developing with ESS::         Tools for package development and debugging
* Extras::                      Other ESS features for the S family
* ESS for R::                   Overview of ESS features for the S family
* ESS for SAS::
* ESS for BUGS::
* ESS for JAGS::
* Mailing lists/bug reports::   How to get assistance with ESS
* Customization::               Customizing ESS
* Indices::
@end menu

@node Introduction
@chapter Introduction to ESS
@cindex introduction

ESS provides a generic interface, through Emacs, to statistical
packages.  It currently supports R (and the rest of the S family), SAS,
BUGS/JAGS, Stata, and Julia with the level of support roughly in that
order.

Throughout this manual, @emph{Emacs} refers to @emph{GNU Emacs} by the
Free Software Foundation.  Although previous versions of ESS supported
other Emacsen, current versions only support GNU Emacs.

There are two main ways of interacting with ESS: through ``regular''
modes or ``inferior'' modes.  Regular modes act like normal Emacs major
modes.  ESS major modes are displayed in the mode-line in the format
@code{ESS[dialect]}, where @code{dialect} can take values such as
@code{R}, @code{SAS}, or @code{S}.

ESS also provides easy access to an ``inferior process,'' which is an Emacs buffer associated with a running process.  This can be an R session, for example.  These inferior processes are referred
to as inferior ESS (@code{iESS}), and are shown in the modeline by
@code{iESS [dialect]}.

Currently, the documentation contains many references to @cite{`R'}
where actually any supported (statistics) language is meant, i.e., `R'
could also mean `S' or `SAS.'

@cindex interactive use of R
@cindex using R interactively
@cindex interactive use of S
@cindex using S interactively
@cindex using ESS interactively
For exclusively interactive users of R, ESS provides a number of
features to make life easier.  There is an easy to use command history
mechanism, including a quick prefix-search history.  To reduce typing,
command-line completion is provided for all R objects and ``hot keys''
are provided for common R function calls.  Help files are easily
accessible, and a paging mechanism is provided to view them.  Finally,
an incidental (but very useful) side-effect of ESS is that a transcript
of your session is kept for later saving or editing.
@cindex transcripts of R sessions
@cindex transcripts of S sessions

No special knowledge of Emacs is necessary when using R interactively
under ESS.

@cindex programming in R
@cindex programming in S
For those that use R in the typical edit--test--revise cycle when
writing R functions, ESS provides for editing of R functions
in Emacs buffers.  Unlike the typical use of R where the editor
is restarted every time an object is edited, ESS uses the current Emacs
session for editing.  In practical terms, this means that you can edit
more than one function at once, and that the ESS process is still
available for use while editing.  Error checking is performed on
functions loaded back into R, and a mechanism to jump directly to the
error is provided.  ESS also provides for maintaining text versions of
your R functions in specified source directories.

@menu
* Features::                    Why should I use ESS?
* New features::
* Credits::                     Authors of and contributors to ESS
* Manual::                      How to read this manual
@end menu

@node Features
@section Why should I use ESS?

Statistical packages are powerful software systems for manipulating and
analyzing data, but their user interfaces often leave something
to be desired:  they offer weak editor functionality and they differ
among themselves so markedly that you have to re-learn how to do those
things for each package.  ESS is a package which is designed to make
editing and interacting with statistical packages more uniform,
user-friendly and give you the power of Emacs as well.

Additionally, both Emacs and ESS (and R) are free software designed to
give users full control over their computer.  For more on what this
means, visit @uref{https://www.gnu.org/philosophy/free-sw.html}.


@menu
* Current Features::
@end menu

@node Current Features
@subsection Features Overview
@include currfeat.texi

For source code buffers, ESS offers several features:

@itemize @bullet
@item
@b{Support for multiple indentation styles} R code @xref{Indenting}.

@item
@b{Facilities for loading and error-checking source files}, including a
keystroke to jump straight to the position of an error in a source file.
@xref{Error Checking}.

@item
@b{Source code revision maintenance}, which allows you to keep historic
versions of R source files.
@xref{Source Files}.

@item
@b{Facilities for evaluating R code} such as portions of source
files, or line-by-line evaluation of files (useful for debugging).
@xref{Evaluating code}.
@end itemize

ESS also provides features that make it easier to interact with inferior
ESS (iESS) process (a connection between your buffer and the statistical
package which is waiting for you to input commands).  These include:

@itemize @bullet
@item
@b{Command-line editing} for fixing mistakes in commands before they are
entered.  @xref{Command-line editing}.

@item
@b{Searchable command history} for recalling previously-submitted
commands.  @xref{Command History}.

@item
@b{Command-line completion} of both object and file names for quick
entry.  @xref{Completion}.

@item
@b{Hot-keys} for quick entry of commonly-used commands in `R' such as
@code{objects()}, and @code{search()}.  @xref{Hot keys}.

@item
@b{Transcript recording} for a complete record of all the actions in an
R session.  @xref{Transcript}.

@item
@b{Interface to the help system}, with a specialized mode for viewing R
help files.  @xref{Help}.

@item
@b{Object editing}.  ESS allows you to edit more than one function
simultaneously in dedicated Emacs buffers.  The ESS process may
continue to be used while functions are being edited.
@xref{Edit buffer}.
@end itemize

Finally, ESS provides features for re-submitting commands from saved
transcript files, including:

@itemize @bullet

@item
@b{Evaluation of previously entered commands}, stripping away
unnecessary prompts.
@xref{Transcript resubmit}.

@end itemize

@node New features
@section New features in ESS

@include newfeat.texi
@comment @include onewfeat.texi

@node Credits
@section Authors of and contributors to ESS

@include credits.texi

@node Manual
@section How to read this manual

If you need to install ESS, read @ref{Installation} for details on what
needs to be done before proceeding to the next chapter.  This section
describes some of the basics of using Emacs.  If you are already
familiar with basic Emacs functionality, skip this section.  You may
also want to use the Emacs tutorial, accessible via @kbd{C-h t}.

In this manual we use the standard notation used by Emacs for describing the
keystrokes used to invoke certain commands.  @kbd{C-<chr>} means hold
the CONTROL key while typing the character <chr>. @kbd{M-<chr>} means
hold the META key (usually ALT) down while typing <chr>. If there is no
META, EDIT or ALT key, instead press and release the ESC key and then
type <chr>.

All ESS commands can be invoked by typing @kbd{M-x command}. Most of the
useful commands are bound to keystrokes for ease of use. Also, the most
popular commands are also available through the Emacs menubar, and a
small subset are provided on the toolbar. Where possible, keybindings
are similar to other modes in Emacs to strive for a consistent user
interface within Emacs, regardless of the details of which programming
language is being edited, or process being run.

Some commands, such as @kbd{M-x R} can accept an optional `prefix'
argument.  To specify the prefix argument, you would type @kbd{C-u}
before giving the command.  For example, if you type @kbd{C-u M-x R}, you will
be asked for command line options that you wish to invoke the R process
with.

Emacs is a `self-documenting' text editor.  This applies to ESS in two
ways.  First, some documentation about each ESS command can be obtained
by typing @kbd{C-h f}.  For example, if you type @kbd{C-h f
ess-eval-region}, documentation for that command will appear in a
separate *Help* buffer.  Second, @kbd{C-h m} pops up a complete list of
keybindings available in each ESS mode and brief description of that
mode.

Emacs is a versatile editor written in both C and a dialect of Lisp
known as Elisp. ESS is written in Elisp and benefits from the flexible
nature of lisp. In particular, many aspects of ESS behaviour can be
changed by suitable customization of Lisp variables. This manual
mentions some of the most frequent variables. A full list of them
however is available by using the Custom facility within Emacs. Type
@kbd{M-x customize-group RET ess RET} to get started.
@ref{Customization} provides details of common user variables you can
change to customize ESS to your taste, but it is recommended that you
defer this section until you are more familiar with ESS.

@node Installation
@chapter Installing ESS on your system
@cindex installation
@include installation.texi

@node Interactive ESS
@chapter Interacting with statistical programs

As well as using ESS to edit your source files for statistical programs,
you can use ESS to run these statistical programs.  In this chapter, we
mostly will refer by example to running R from within Emacs.  The Emacs
convention is to name such processes running under its control as
`inferior processes'.  Some users find this terminology confusing; you
may prefer to think of these as `interactive processes.'  Either way, we
use the term `iESS' to refer to the Emacs mode used to interact with
statistical programs.


@menu
* Starting up::
* Multiple ESS processes::
* ESS processes on Remote Computers::
* Customizing startup::
* Controlling buffer display::
@end menu

@node Starting up
@section Starting an ESS process
@cindex starting ESS
@cindex running S
@cindex running R

To start an inferior R session on GNU/Linux, macOS, or Windows using the
Cygwin bash shell, simply type @kbd{M-x R RET}. To start an R session on
Windows when you use the MSDOS/powershell shell, simply type @kbd{M-x
S+6-msdos RET}. R will then (by default) ask the question

@pindex S
@pindex R

@cindex ESS process directory
@cindex starting directory
@cindex working directory
@cindex directories

@example
R starting data directory?
@end example

@noindent
Enter the name of the directory you wish to have as the working
directory (that is, the directory you wish to have @code{getwd()} return
if using R).

You will then be popped into a buffer
@cindex ESS process buffer
@cindex process buffer
named @samp{*R*} which will be used for interacting with the ESS
process, and you can start entering commands.


@node Multiple ESS processes
@section Running more than one ESS process
@cindex multiple ESS processes
@cindex changing ESS processes
@cindex multiple inferior processes
@cindex multiple interactive processes

ESS allows you to run more than one iESS process simultaneously in the
same session.  Each process has a name and a number; the initial process
@cindex process names
(process 1) is simply named @samp{R}.  If you call @kbd{M-x R} again
without killing the first R process, ESS will start a second R process
with the name @samp{R:2}.  To have the first buffer named @samp{R:1},
customize the option @code{ess-plain-first-buffername}.  With a prefix
argument, @kbd{C-u M-x R} allows for the specification of command line
options.

@defvr {User Option} ess-plain-first-buffername
If non-@code{nil}, name the first iESS process [R]. Otherwise, name it [R:1].
@end defvr

@findex ess-request-a-process
You can switch to any active ESS process with the command
@samp{M-x ess-request-a-process}.  Just enter the name of the process
you require; completion is provided over the names of all running
processes.  This is a good command to consider binding to a global key.

@node ESS processes on Remote Computers
@section ESS processes on Remote Computers
@subsection ESS and TRAMP
@findex ess-remote
@cindex remote Computers
@cindex ESS-elsewhere
@cindex S+elsewhere
@cindex tramp support

ESS works with processes on remote computers as easily as with processes
on the local machine.  The recommended way to access a statistical
program on remote computer is to start it with @xref{Top,,, tramp, TRAMP
User Manual}. 

Start an ssh session using TRAMP with @samp{C-x C-f /ssh:user@@host:
RET}.  Tramp should open a dired buffer in your remote home directory.
Now call your favorite ESS process (@code{R}, @code{Julia}, @code{stata}
etc) as you would usually do on local machine: @code{M-x R}.

Alternatively you can start your process normally (@code{M-x R}).  When
asked for starting directory, simply type @samp{/ssh:user@@host: RET}.
The R process will be started on the remote machine.

To simplify the process even further create a "config" file in your
@code{.ssh/} folder and add an account. For example if you use amazon
EC2, it might look like following:

@verbatim
   Host amazon
      Hostname ec2-54-215-203-181.us-west-1.compute.amazonaws.com
      User ubuntu
      IdentityFile ~/.ssh/my_amazon_key.pem
      ForwardX11 yes
@end verbatim

With this configuration @code{/ssh:amazon:} is enough to start a
connection. The ForwardX11 is needed if you want to see the R graphic device
showing on the current machine

@subsection ESS-remote
TRAMP is the recommended way of starting a remote session.  The other
way to start a remote ESS connection is through @code{ess-remote}.

@enumerate
@item Start a new shell, telnet or ssh buffer and connect to the remote computer
(e.g. use, @samp{M-x shell}, @samp{M-x telnet} or @samp{M-x ssh}; ssh.el is available at
@uref{https://www.splode.com/~friedman/software/emacs-lisp/src/ssh.el}).

@item Start the ESS process on the remote machine, for example with one of
the commands @samp{R}, @samp{Splus}, or @samp{sas -stdio}.

@item Start @samp{M-x ess-remote}.
You will be prompted for a program name with completion.  Choose
one. Your process is now known to ESS.  All the usual ESS commands
(@samp{C-c C-n} and its relatives) now work with the R language
processes.  For SAS you need to use a different command @samp{C-c i}
(that is a regular @samp{i}, not a @samp{C-i}) to send lines from your
@file{myfile.sas} to the remote SAS process. @samp{C-c i} sends lines
over invisibly.
@c and lets SAS display them formatted correctly as in a SAS log file.
With ess-remote you get teletype behavior---the data input, the
log, and the listing all appear in the same buffer.  To make this work,
you need to end every PROC and DATA step with a "RUN;" statement.  The
"RUN;" statement is what tells SAS that it should process the preceding
input statements.

@item Graphics (interactive) on the remote machine.  If you run X11
(@xref{X11}, X Windows)
on both the local and remote machines then you should be able to
display the graphs locally by setting the @samp{DISPLAY} environment
variable appropriately.  Windows users can download @samp{xfree86}
from cygwin.

@item Graphics (static) on the remote machine.
If you don't run the X window system on the local machine, then you can write
graphics to a file on the remote machine, and display the file in
a graphics viewer on the local machine.  Most statistical software can
write one or more of postscript, GIF, or JPEG files.
Depending on the versions of Emacs and the operating system that you
are running, Emacs itself may display @samp{.gif} and @samp{.jpg}
files.  Otherwise, a graphics file viewer will be needed.
Ghostscript/ghostview may be downloaded to display @samp{.ps} and
@samp{.eps} files.  Viewers for GIF and JPEG are usually included with
operating systems.  @xref{ESS(SAS)--Function keys for batch processing},
for more information on using the F12 key for displaying graphics files
with SAS.
@end enumerate

Should you or a colleague inadvertently start a statistical process in
an ordinary @samp{*shell*} buffer, the @samp{ess-remote} command can
be used to convert it to an ESS buffer and allow you to use the ESS
commands with it.

@node Customizing startup
@section Changing the startup actions

If you do not wish ESS to prompt for a starting directory when starting
a new process, set the variable @code{ess-ask-for-ess-directory} to
@vindex ess-ask-for-ess-directory
@code{nil}.  In this case, the starting directory will be set using
one of the following methods:

@enumerate

@item If the variable @code{ess-directory-function} stores the name of a
function, the value returned by this function is used.  The default for
this variable is nil.

@item Otherwise, if the variable @code{ess-directory} stores the name of a
directory (ending in a slash), this value is used.  The default for this
variable is nil.

@item Otherwise, the working directory of the current buffer is used.

@end enumerate

@vindex ess-ask-for-ess-directory
@vindex ess-pre-run-hook
If @code{ess-ask-for-ess-directory} has a non-@code{nil} value (as it
does by default) then the value determined by the above rules provides
the default when prompting for the starting directory.  Incidentally,
@code{ess-directory} is an ideal variable to set in
@code{ess-pre-run-hook}.

@vindex ess-post-run-hook
You may also customize @code{ess-post-run-hook} and the
dialect-specific @code{ess-*-post-run-hook} variables. For example,
you may want to use @code{ess-load-file} to load a file when ESS
starts R:

@example
(add-hook 'ess-r-post-run-hook (lambda () (ess-load-file "foo.R")))
@end example

If you like to keep a record of your R actions, set the variable
@code{ess-ask-about-transfile} to @code{t}, and you will be asked for a
filename for the transcript before the ESS process starts.

@defvr {User Option} ess-ask-about-transfile
If non-@code{nil}, ask for a file name in which to save the session
transcript.
@end defvr

@cindex transcript file
Enter the name of a file in which to save the transcript at the prompt.
If the file doesn't exist it will be created (for R, you likely want it
to end in @samp{.Rout}). If the file already exists the transcript will
be appended to the file. (Note: if you don't set this variable but you
still want to save the transcript, you can still do it later ---
@pxref{Saving transcripts}.)

Once these questions are answered (if they are asked at all) the inferior
process itself is started.
@vindex inferior-ess-program
@vindex iESS program arguments
If you need to pass any arguments to this program, they may be specified
in the variable @code{inferior-@var{S_program_name}-args}.  For example, if
@code{inferior-ess-program} is @code{"R"} then the variable to set is
@code{inferior-R-args}.
@cindex arguments to S program
@cindex arguments to R program
It is not normally necessary to pass arguments to the iESS program; in
particular do not pass the @samp{-e} option to @code{Splus}, since ESS
provides its own command history mechanism.

@vindex ess-auto-width
@vindex ess-auto-width-visible

ESS can set the width option of inferior processes automatically when
the window configuration changes. To do so, set @code{ess-auto-width}
to a non-nil value. By default, the change will not be shown in the
inferior buffer. If you want it to be shown, set
@code{ess-auto-width-visible} to a non-nil value.


@node Controlling buffer display
@section Controlling buffer display

Users can control how buffers are displayed by customizing
@code{display-buffer-alist}, @xref{Window Choice,,, emacs}. This
section provides examples of how to achieve certain setups.

Users coming from RStudio may want the R process to appear at the
bottom left of the frame, help buffers to appear at the bottom right,
and ess-rdired buffers at the top right. To achieve that, you could
this. Note that the order matters; @code{*R Dired*} would match
@code{*R} if it came before in the alist.

@example
(setq display-buffer-alist
      `(("^\\*R Dired"
         (display-buffer-reuse-window display-buffer-in-side-window)
         (side . right)
         (slot . -1)
         (window-width . 0.33)
         (reusable-frames . nil))
        ("^\\*R"
         (display-buffer-reuse-window display-buffer-at-bottom)
         (window-width . 0.5)
         (reusable-frames . nil))
        ("^\\*Help"
         (display-buffer-reuse-window display-buffer-in-side-window)
         (side . right)
         (slot . 1)
         (window-width . 0.33)
         (reusable-frames . nil))))
@end example

Some users prefer working with multiple frames where one frame has the
source code and the other frame has the inferior process. To achieve
this, use this example.  For a detailed description of
@code{reusable-frames}, see @xref{Buffer Display Action Alists,,, elisp}.

@example
(setq display-buffer-alist
      '(("^\\*R"
         (display-buffer-reuse-window display-buffer-pop-up-frame)
         (reusable-frames . 0))))
@end example

Some users may want the process window to be dedicated to the process
so that other buffers are not shown on top of that window.
@xref{Dedicated Windows,,, elisp}. For example, this prevents help
buffers from being displayed in the window showing the inferior R
process.

@example
(setq display-buffer-alist
      '(("^\\*R"
         nil
         (dedicated . t))))
@end example

By default, ESS usually prefers splitting the frame into multiple
windows so that you can work with both the R script and the R REPL
visible at once. If you want to override this behavior so that the
script buffer and the process buffer share the same window, you can do
so like this:

@example
(setq display-buffer-alist
      '(("^\\*R" . ((display-buffer-same-window) (inhibit-same-window . nil)))
        ("\\.R$" . ((display-buffer-same-window) (inhibit-same-window . nil)))))
@end example

@node Entering commands
@chapter Interacting with the ESS process
@cindex entering commands
@cindex commands
@cindex sending input

The primary function of the ESS package is to provide an easy-to-use
front end to the R interpreter.  This is achieved by running the R
process from within an Emacs buffer, called hereafter @emph{inferior}
buffer, which has an active @code{inferior-ess-mode}. The features of
inferior R mode are similar to those provided by the standard Emacs
shell mode (@pxref{Shell Mode,,, emacs, The Gnu Emacs Reference Manual}).
Command-line completion of R objects and a number of `hot keys' for
commonly-used R commands are also provided for ease of typing.

@menu
* Command-line editing::        Entering commands and fixing mistakes
* Transcript::                  Manipulating the transcript
* Command History::             Command History
* History expansion::           References to historical commands
* Hot keys::                    Hot keys for common commands
* Statistical Process running in ESS?::
* Emacsclient::                 Using emacsclient
* Other::                       Other commands provided by inferior-ESS
@end menu

@node Command-line editing
@section Entering commands and fixing mistakes
@cindex command-line editing

Sending a command to the ESS process is as simple as typing it in
and pressing the @key{RETURN} key:

@deffn Command inferior-ess-send-input
@kindex RET
@kbd{RET} Send the command on the current line to the ESS process.
@end deffn

If you make a typing error before pressing @kbd{RET} all the usual Emacs
editing commands are available to correct it (@pxref{Basic, Basic, Basic
editing commands, emacs, The GNU Emacs Reference Manual}).  Once the
command has been corrected you can press @key{RETURN} (even if the
cursor is not at the end of the line) to send the corrected command to
the ESS process.

Emacs provides some other commands which are useful for fixing mistakes:

@table @kbd
@item C-c C-w
@findex backward-kill-word
@code{backward-kill-word} Deletes the previous word (such as an object
name) on the command line.

@item C-c C-u
@findex comint-kill-input
@code{comint-kill-input} Deletes everything from the prompt to point.
Use this to abandon a command you have not yet sent to the ESS process.

@item C-a
@findex comint-bol
@code{comint-bol} Move to the beginning of the line, and then skip
forwards past the prompt, if any.
@end table

@xref{Shell Mode,,, emacs, The Gnu Emacs Reference Manual}, for other
commands relevant to entering input.


@node Transcript
@section Manipulating the transcript

Most of the time, the cursor spends its time at the bottom of
the ESS process buffer, entering commands.  However all the input
and output from the current (and previous) ESS sessions is stored in
the process buffer (we call this the transcript) and often we want to
@cindex transcript
move back up through the buffer, to look at the output from previous
commands for example.

Within the process buffer, a paragraph
@cindex paragraphs in the process buffer
is defined as the prompt, the command after the prompt, and the output
from the command.  Thus @kbd{M-@{} and @kbd{M-@}} move you backwards and
forwards, respectively, through commands in the transcript.  A
particularly useful command is @kbd{M-h} (@code{mark-paragraph}) which
will allow you to mark a command and its entire output (for deletion,
perhaps).  For more information about paragraph commands,
@pxref{Paragraphs, Paragraphs, Paragraphs, emacs, The GNU Emacs
Reference Manual}.

If an ESS process finishes and you restart it in the same process
buffer, the output from the new ESS process appears after the output
from the first ESS process separated by a form-feed (@samp{^L})
character.  Thus pages in the ESS
@cindex pages in the process buffer
process buffer correspond to ESS sessions.  Thus, for example, you may
use @kbd{C-x [} and @kbd{C-x ]} to move backward and forwards through
ESS sessions in a single ESS process buffer.  For more information about
page commands, @pxref{Pages, Pages, Pages, emacs, The GNU Emacs
Reference Manual}.

@menu
* Last command::                Manipulating the output from the last command
* Process buffer motion::       Viewing older commands
* Transcript resubmit::         Re-submitting commands from the transcript
* Saving transcripts::          Keeping a record of your R session
@end menu

@node Last command
@subsection Manipulating the output from the last command

Viewing the output of the command you have just entered is a common
occurrence and ESS provides a number of facilities for doing this.
Whenever a command produces output, it is possible that the
window will scroll, leaving the next prompt near the middle of the
window.  The first part of the command output may have scrolled off the
top of the window, even though the entire output would fit in the window
if the prompt were near the bottom of the window.  If this happens, you
can use the following comint commands:

@code{comint-show-maximum-output} to move to the end
of the buffer, and place cursor on bottom line of window to make more of
the last output visible.  To make this happen automatically for all
inputs, set the variable @code{comint-scroll-to-bottom-on-input} to
@code{t} or @code{'this}.  If the first part of the output is still not
visible, use
@cindex reading long command outputs
@kbd{C-c C-r} (@code{comint-show-output}),
@findex comint-show-output
which moves cursor to the previous command line and places it at the
top of the window.

Finally, if you want to discard the last command output altogether, use
@cindex deleting output
@kbd{C-c C-o} (@code{comint-delete-output}),
@findex comint-delete-output
which deletes everything from the last command to the current prompt.
Use this command judiciously to keep your transcript to a more
manageable size.

@node Process buffer motion
@subsection Viewing older commands

If you want to view the output from more historic commands than the
previous command, commands are also provided to move backwards and
forwards through previously entered commands in the process buffer:

@table @kbd
@item C-c C-p
@findex comint-previous-prompt
@code{comint-previous-prompt} Moves point to the preceding prompt in the
process buffer.

@item C-c C-n
@findex comint-next-prompt
@code{comint-next-prompt} Moves point to the next prompt in the process
buffer.
@end table

@noindent
Note that these two commands are analogous to @kbd{C-p} and @kbd{C-n}
but apply to command lines rather than text lines.  And just like
@kbd{C-p} and @kbd{C-n}, passing a prefix argument to these commands
means to move to the @var{ARG}'th next (or previous) command.  (These
commands are also discussed in @ref{Shell History Copying,,Shell History
Copying,emacs, The GNU Emacs Reference Manual}.)

There are also two similar commands (not bound to any keys by default)
which move to preceding or succeeding commands, but which first prompt
for a regular expression (@pxref{Regexps,,Syntax of Regular
Expression,emacs, The GNU Emacs Reference Manual}), and then moves to
the next (previous) command matching the pattern.

@table @code
@item comint-backward-matching-input regexp arg
@findex comint-backward-matching-input
@itemx comint-forward-matching-input regexp arg
@findex comint-forward-matching-input
Search backward (forward) through the transcript buffer for the
@var{arg}'th previous (next) command matching @var{regexp}.  @var{arg}
is the prefix argument; @var{regexp} is prompted for in the minibuffer.
@end table

@node Transcript resubmit
@subsection Re-submitting commands from the transcript

When moving through the transcript, you may wish to re-execute some of
the commands you find there.  ESS provides commands to do this;
these commands may be used whenever the cursor is within a command line
in the transcript (if the cursor is within some command @emph{output},
an error is signaled).  Note all commands involve the @key{RETURN}
key.

@table @kbd
@item RET
@code{inferior-ess-send-input} @xref{Command-line editing}.

@item C-c RET
@findex comint-copy-old-input
@code{comint-copy-old-input} Copy the command under the cursor to the
current command line, but don't execute it.  Leaves the cursor on the
command line so that the copied command may be edited.
@end table

When the cursor is not after the current prompt, the @key{RETURN} key
has a slightly different behavior than usual.  Pressing @kbd{RET} on any
line containing a command that you entered (i.e. a line beginning with a
prompt) sends that command to the ESS process once again.  If you
wish to edit the command before executing it, use @kbd{C-c RET} instead;
it copies the command to the current prompt but does not execute it,
allowing you to edit it before (re)submitting it.

These commands work even if the current line is a continuation line
(i.e. the prompt is @samp{+} instead of @samp{>}) --- in this case all
the lines that form the multi-line command are concatenated together and
the resulting command is sent to the ESS process (currently this is
the only way to resubmit a multi-line command to the ESS process in
one go).  If the current line does
@cindex multi-line commands, resubmitting
not begin with a prompt, an error is signalled.  This feature, coupled
with the command-based motion commands described above, could be used as
a primitive history mechanism.  ESS provides a more sophisticated
mechanism, however, which is described in @ref{Command History}.

@node Saving transcripts
@subsection Keeping a record of your R session

To keep a record of your R session in a disk file, use the Emacs
command @kbd{C-x C-w} (@code{write-file}) to attach a file to the ESS
process buffer.  The name of the process buffer will (probably) change
to the name of the file, but this is not a problem.  You can still use
R as usual; just remember to save the file before you quit Emacs
with @kbd{C-x C-s}.  You can make ESS prompt you for a filename in which
to save the transcript every time you start R by setting the
variable
@vindex ess-ask-about-transfile
@code{ess-ask-about-transfile} to @code{t}; @xref{Customizing startup}.
@cindex transcript file names
For R files, naming transcript files @samp{*.Rout} puts them in a
special mode (ESS transcript mode --- @pxref{Transcript Mode}) for
editing transcript files which is automatically selected for files with
this suffix.

@cindex editing transcripts
R transcripts can get very large, so some judicious editing is
appropriate if you are saving it in a file.  Use @kbd{C-c C-o} whenever
a command produces excessively long output (printing large arrays, for
example).  Delete erroneous commands (and the resulting error messages
or other output) by moving to the command (or its output) and typing
@kbd{M-h C-w}.  Also, remember that @kbd{C-c C-x} (and other hot keys)
may be used for commands whose output you do not wish to appear in the
transcript.  These suggestions are appropriate even if you are not
saving your transcript to disk, since the larger the transcript, the
more memory your Emacs process will use on the host machine.

You can use @code{ess-transcript-clean-region} to strip output from a
transcript, leaving only source code suitable for inclusion in files
@code{source()}-able from R.  @pxref{Clean, Transcript Mode}


@node Command History
@section Command History
@cindex command history
@cindex editing commands
@cindex re-executing commands

ESS provides easy-to-use facilities for re-executing or editing previous
commands.  An input history of the last few commands is maintained (by
default the last 500 commands are stored, although this can be changed by
setting the variable @code{comint-input-ring-size} in
@vindex comint-input-ring-size
@code{inferior-ess-mode-hook}.) The simplest history commands simply
select the next and previous commands in the input history:

@table @kbd
@item M-p
@findex comint-previous-input
@code{comint-previous-input} Select the previous command in the input
history.

@item M-n
@findex comint-next-input
@code{comint-next-input} Select the next command in the input history.
@end table

@noindent
For example, pressing @kbd{M-p} once will re-enter the last command into
the process buffer after the prompt but does not send it to the ESS
process, thus allowing editing or correction of the command before the
ESS process sees it.  Once corrections have been made, press
@kbd{RET} to send the edited command to the ESS process.

If you want to select a particular command from the history by matching
it against a regular expression (@pxref{Regexps,,Syntax of Regular
Expression,emacs, The GNU Emacs Reference Manual}), to search for a
particular variable name for example, these commands are also available:

@table @kbd
@item M-r
@findex comint-history-isearch-backward-regexp
@code{comint-history-isearch-backward-regexp} Prompt for a regular
expression, and search backwards through the input history for a command
matching the expression.

@c @item M-s
@c @findex comint-next-matching-input
@c @code{comint-next-matching-input} Prompt for a regular expression, and
@c search backwards through the input history for a command matching the
@c expression.
@end table

@noindent
A common type of search is to find the last command that began with a
particular sequence of characters; the following two commands provide an
easy way to do this:

@table @kbd
@item C-c M-r
@findex comint-previous-matching-input-from-input
@code{comint-previous-matching-input-from-input} Select the previous
command in the history which matches the string typed so far.

@item C-c M-s
@findex comint-next-matching-input-from-input
@code{comint-next-matching-input-from-input} Select the next command in
the history which matches the string typed so far.
@end table

@noindent
Instead of prompting for a regular expression to match against, as they
instead select commands starting with those characters already entered.
For instance, if you wanted to re-execute the last @code{attach()}
command, you may only need to type @kbd{att} and then @kbd{C-c M-r} and
@kbd{RET}.

@xref{Shell Ring,,Shell History Ring,emacs, The GNU Emacs Reference
Manual}, for a more detailed discussion of the history mechanism,
and do experiment with the @code{In/Out} menu to explore the possibilities.

Many ESS users like to have even easier access to these, and recommend
adding something like
@example
  (eval-after-load "comint"
   '(progn
      (define-key comint-mode-map [up]
        'comint-previous-matching-input-from-input)
      (define-key comint-mode-map [down]
        'comint-next-matching-input-from-input)

      ;; also recommended for ESS use --
      (setq comint-move-point-for-output 'others)
      ;; somewhat extreme, almost disabling writing in *R*, *shell* buffers above prompt:
      (setq comint-scroll-to-bottom-on-input 'this)
      ))
@end example
to your Emacs configuration file, where the last two settings are
typically desirable for the situation where you work with a script (for
example, @file{code.R}) and send code chunks to the process buffer
(e.g. @code{*R*}).  Note however that these settings influence all
@code{comint}-using Emacs modes, not just the ESS ones, and for that
reason, these customization cannot be part of @code{ESS} itself.

@menu
* Saving History::
@end menu

@node Saving History
@subsection Saving the command history

The @code{ess-history-file} variable, which is @code{t} by default,
together with  @code{ess-history-directory}, governs if and where the
command history is saved and restored between sessions.
By default, @code{ess-history-directory} is @code{nil}, and the command
history will be stored (as a file) in the same directory as the iESS process.

ESS users may work exclusively with script files rather than in a iESS
session, and may not want to save any history files.  To do so:

@smallexample
  (setq ess-history-file nil)
@end smallexample

@noindent
or if you prefer only one global command history file:

@smallexample
  (setq ess-history-directory "~/.R/")
@end smallexample

@noindent
in your Emacs configuration file.

@node History expansion
@section References to historical commands

Instead of searching through the command history using the command
described in the previous section, you can alternatively refer to a
historical command directly using a notation very similar to that used
in @code{csh}.  History references are introduced by a @samp{!} or
@samp{^} character and have meanings as follows:

@table @samp
@item !!
The immediately previous command

@item !-@var{N}
The @var{N}th previous command

@item !text
The last command beginning with the string @samp{text}

@item !?text
The last command containing the string @samp{text}
@end table

In addition, you may follow the reference with a @dfn{word designator}
to select particular @dfn{words} of the input.  A word is defined as a
sequence of characters separated by whitespace.  (You can modify this
definition by setting the value of @code{comint-delimiter-argument-list}
to a list of characters that are allowed to separate words and
@vindex comint-delimiter-argument-list
themselves form words.)  Words are numbered beginning with zero.  The
word designator usually begins with a @samp{:} (colon) character;
however it may be omitted if the word reference begins with a @samp{^},
@samp{$}, @samp{*} or @samp{-}.  If the word is to be selected from the
previous command, the second @samp{!}  character can be omitted from the
event specification.  For instance, @samp{!!:1} and @samp{!:1} both
refer to the first word of the previous command, while @samp{!!$} and
@samp{!$} both refer to the last word in the previous command.  The
format of word designators is as follows:

@table @samp
@item 0
The zeroth word (i.e. the first one on the command line)

@item @var{n}
The @var{n}th word, where @var{n} is a number

@item ^
The first word (i.e. the second one on the command line)

@item $
The last word

@item @var{x}-@var{y}
A range of words; @samp{-@var{y}} abbreviates @samp{0-@var{y}}

@item *
All the words except the zeroth word, or nothing if the command had just
one word (the zeroth)

@item @var{x}*
Abbreviates @var{x}-$

@item @var{x}-
Like @samp{@var{x}*}, but omitting the last word
@end table

In addition, you may surround the entire reference except for the first
@samp{!} by braces to allow it to be followed by other (non-whitespace)
characters (which will be appended to the expanded reference).

Finally, ESS also provides quick substitution; a reference like
@samp{^old^new^} means ``the last command, but with the first occurrence
of the string @samp{old} replaced with the string @samp{new}'' (the last
@samp{^} is optional).  Similarly, @samp{^old^} means ``the last
command, with the first occurrence of the string @samp{old} deleted''
(again, the last @samp{^} is optional).

To convert a history reference as described above to an input suitable
for R, you need to @dfn{expand} the history reference, using the
@key{TAB} key.  For this to work, the cursor must be preceded by a space
(otherwise it would try to complete an object name) and not be within a
string (otherwise it would try to complete a filename).  So to expand
the history reference, type @kbd{SPC TAB}.  This will convert the
history reference into an R command from the history, which you can
then edit or press @key{RET} to execute.

For example, to execute the last command that referenced the variable
@code{data}, type @kbd{!?data SPC TAB RET}.

@node Hot keys
@section Hot keys for common commands

ESS provides a number of commands for executing the commonly used
functions.  These commands below are basically information-gaining
commands (such as @code{objects()} or @code{search()}) which tend to
clutter up your transcript and for this reason some of the hot keys
display their output in a temporary buffer instead of the process buffer
by default.  This behavior is controlled by the following option:

@defvr {User Option} ess-execute-in-process-buffer
If non-@code{nil}, means that these commands will produce their output
in the process buffer instead.
@end defvr

In any case, passing a prefix argument to the commands (with @kbd{C-u})
will reverse the meaning of @code{ess-execute-in-process-buffer} for
that command. In other words, the output will be displayed in the
process buffer if it usually goes to a temporary buffer, and vice-versa.
These are the hot keys that behave in this way:

@deffn Command ess-execute-objects @var{posn}
@kindex C-c C-x
@pindex objects()
@cindex objects
@kbd{C-c C-x} Sends the @code{objects()} command to the ESS process.  A
prefix argument specifies the position on the search list (use a
negative argument to toggle @code{ess-execute-in-process-buffer} as
well).  This is a quick way to see what objects are in your working
directory.  A prefix argument of 2 or more means get objects for that
position.  A negative prefix argument @var{posn} gets the objects for
that position, as well as toggling @code{ess-execute-in-process-buffer}.
@end deffn

@deffn Command ess-execute-search @var{invert}
@kindex C-c C-s
@pindex search()
@cindex search list
@kbd{C-c C-s} Sends the @code{inferior-ess-search-list-command} command
to the @code{ess-language} process; @code{search()} in R.  Prefix
@var{invert} toggles @code{ess-execute-in-process-buffer}.
@end deffn

@code{ess-execute} may seem pointless when you could just type the
command in anyway, but it proves useful for `spot' calculations which
would otherwise clutter your transcript, or for evaluating an expression
while partway through entering a command.  You can also use this command
to generate new hot keys using the Emacs keyboard macro facilities;
@pxref{Keyboard Macros, Keyboard Macros, Keyboard Macros, emacs, The GNU
Emacs Reference Manual}.
@cindex hot keys
@cindex keyboard short cuts

The following hot keys do not use @code{ess-execute-in-process-buffer}
to decide where to display the output --- they either always display in
the process buffer or in a separate buffer, as indicated:

@c @deffn Command ess-execute-attach @var{dir} @var{posn}
@c @pindex attach()
@c Prompts for a directory @var{dir} to attach to the ESS process with the
@c @code{attach()} command.  If a numeric prefix argument @var{posn} is
@c given, it is used as the position on the search list to attach the
@c directory; otherwise the R default of 2 is used.  The
@c @code{attach()} command actually executed appears in the process buffer.
@c @end deffn

@deffn Command ess-load-file @var{filename}
@kindex C-c M-l
@kbd{C-c M-l} Prompts for a file (@var{filename}) to load into the ESS
process using @code{source()}.  If there is an error during loading, you
can jump to the error in the file with the following function.
@end deffn

@deffn Command ess-parse-errors @var{arg} @var{reset}
Visits next @code{next-error} message and
corresponding source code.  If all the error messages parsed so far have
been processed already, the message buffer is checked for new ones.  A
prefix @var{arg} specifies how many error messages to move; negative
means move back to previous error messages.  Just @kbd{C-u} as a prefix
means reparse the error message buffer and start at the first error.
The @var{reset} argument specifies restarting from the beginning.

@xref{Error Checking}, for more details.
@end deffn

@deffn Command ess-display-help-on-object @var{object} @var{command}
@kindex C-c C-v
@kbd{C-c C-v} Pops up a help buffer for an R object or
function.  If @var{command} is supplied, it is used instead of
@code{inferior-ess-help-command}.  See @ref{Help} for more details.
@end deffn

@deffn Command ess-quit
@kindex C-c C-q
@pindex q()
@findex ess-cleanup
@cindex quitting from ESS
@cindex killing the ESS process
@cindex cleaning up
@cindex temporary buffers, killing
@cindex killing temporary buffers
@kbd{C-c C-q} Issue an exiting command to the inferior process,
additionally also running @code{ess-cleanup} for disposing of any
temporary buffers (such as help buffers and edit buffers) that may have
been created.  Use this command when you have finished your R
session instead of simply quitting at the inferior process prompt,
otherwise you will need to issue the command @code{ess-cleanup}
explicitly to make sure that all the files that need to be saved have
been saved, and that all the temporary buffers have been killed.
@end deffn


@node Statistical Process running in ESS?
@section Is the Statistical Process running under ESS?

@cindex STERM
@pindex STERM
For the R languages (R, S, S-Plus) ESS sets an option in the current
process that programs in the language can check to determine the
environment in which they are currently running.

ESS sets @code{options(STERM="iESS")} for R language processes running
in an inferior @code{@iESS{[S]}} or @code{@iESS{[R]}} buffer.

ESS sets @code{options(STERM="ddeESS")} for independent S-Plus for
Windows processes running in the GUI and communicating with ESS via the
DDE (Microsoft Dynamic Data Exchange) protocol through a
@code{ddeESS[S]} buffer.

Other values of @code{options()$STERM} that we recommend are:

@itemize @bullet
@item @code{length}:     Fixed length xterm or telnet window.
@item @code{scrollable}: Unlimited length xterm or telnet window.
@item @code{server}:     S-Plus Stat Server.
@item @code{BATCH}:      BATCH.
@item @code{Rgui}:       R GUI.
@item @code{Commands}:   S-Plus GUI without DDE interface to ESS.
@end itemize

Additional values may be recommended in the future as new interaction
protocols are created.  Unlike the values @code{iESS} and @code{ddeESS},
ESS can't set these other values since the R language program is not
under the control of ESS.

@node Emacsclient
@section Using emacsclient
@cindex emacsclient

When starting R or S under Unix, ESS sets
@code{options(editor="emacsclient")}.  Under Microsoft Windows, it will
use gnuclient.exe rather than emacsclient, but the same principal
applies.  Within your R session, if you have a function
called @code{iterator}, typing @code{fix(iterator)}, will show that
function in a temporary Emacs buffer.  You can then correct the
function.  When you kill the buffer, the definition of the function is
updated.  Using @code{edit()} rather than @code{fix()} means that the
function is not updated.  Finally, the R function @code{page(x)} will
also show a text representation of the object @code{x} in a temporary
Emacs buffer.


@node Other
@section Other commands provided by inferior-ESS

The following commands are also available in the process buffer:

@deffn Command comint-interrupt-subjob
@kindex C-c C-c
@cindex aborting R commands
@cindex interrupting R commands
@cindex aborting S commands
@cindex interrupting S commands
@kbd{C-c C-c} Sends a Control-C signal to the iESS process.  This has the
effect of aborting the current command.
@end deffn

@deffn Command ess-switch-to-inferior-or-script-buffer @var{toggle-eob}
@kindex C-c C-z
@kbd{C-c C-z} When in process buffer, return to the most recent script
buffer.  When in a script buffer pop to the associated process buffer.
Consecutive presses of @kbd{C-z} switch between the script and process buffers. 

If @var{toggle-eob} is given, the value of
@code{ess-switch-to-end-of-proc-buffer} is toggled.
@end deffn

@defvr {User Option} ess-switch-to-end-of-proc-buffer
If non-@code{nil}, @code{ess-switch-to-inferior-or-script-buffer} goes
to end of process buffer.
@end defvr

Other commands available in iESS modes are discussed in
@ref{Shell Mode,,, emacs, The Gnu Emacs Reference Manual}.


@node Evaluating code
@chapter Sending code to the ESS process

Other commands are also available for evaluating portions of code in the
R process.  These commands cause the selected code to be evaluated
directly by the ESS process as if you had typed them in at the
command line; the @code{source()} function is not used.  You may choose
whether both the commands and their output appear in the process buffer
(as if you had typed in the commands yourself) or if the output alone is
echoed.  The behavior is controlled by the variable:

@defvr {User Option} ess-eval-visibly
Non-@code{nil} means @code{ess-eval-*} commands display commands and
output in the process buffer.  A value of @code{t} blocks Emacs while R
is busy.  A value of @code{nowait} does not block Emacs but printing
may be slightly off.
@end defvr

@pindex source()
@cindex echoing commands when evaluating
@cindex evaluating code with echoed commands
@cindex ESS commands blocking Emacs
Passing a prefix (@kbd{C-u}) (in Elisp terms, the argument VIS) to any of the following commands,
however, reverses the meaning of @code{ess-eval-visibly} for that
command only --- for example @kbd{C-u C-c C-j} evaluates the current
line without showing the input in the iESS buffer.  The default value of
@code{ess-eval-visibly} (@code{t}) means that ESS calls block Emacs
until they finish. This may be undesirable, especially if commands take
long to finish. Users who want input to be displayed and Emacs not to be
blocked can set @code{ess-eval-visibly} to @code{'nowait}. This sends
the input to the iESS buffer but does not wait for the process to
finish, ensuring Emacs is not blocked.

Primary commands for evaluating code are:
@cindex evaluating R expressions
@cindex evaluating S expressions

@deffn Command ess-eval-region-or-line-and-step @var{vis}
@kindex C-RET
@kbd{C-<RET>} Sends the highlighted region or current line and step to
next line of code.
@end deffn

@deffn Command ess-eval-region-or-function-or-paragraph @var{vis}
@kindex C-M-x
@kbd{C-M-x} Sends the current selected region or function or paragraph.
@end deffn

@deffn Command ess-eval-region-or-function-or-paragraph-and-step @var{vis}
@kindex C-c C-c
@kbd{C-c C-c} Like @code{ess-eval-region-or-function-or-paragraph} but
steps to next line of code.
@end deffn

Other, not so often used, evaluation commands are:

@deffn Command ess-eval-line @var{vis}
@kindex C-c C-j
@kbd{C-c C-j} Sends the current line to the ESS process.
@end deffn

@deffn Command ess-eval-line-and-go @var{vis}
@kindex C-c M-j
@kbd{C-c M-j} Like @code{ess-eval-line} but additionally switches point
to the ESS process.
@end deffn

@deffn Command ess-eval-function @var{vis} @var{no-error}
@kindex C-c C-f
@kbd{C-c C-f} Sends the function containing point to the ESS
process.
@end deffn

@deffn Command ess-eval-function-and-go @var{vis}
@kindex C-c M-f
@kbd{C-c M-f} Like @code{ess-eval-function} but additionally switches
point to the ESS process.
@end deffn

@deffn Command ess-eval-region @var{start} @var{end} @var{toggle} @var{message}
@kindex C-C C-r
@kbd{C-c C-r} Sends the current region to the ESS process.
@end deffn

@deffn Command ess-eval-region-and-go @var{start} @var{end} @var{vis}
@kindex C-c M-r
@kbd{C-c M-r} Like @code{ess-eval-region} but additionally switches
point to the ESS process.
@end deffn

@deffn Command ess-eval-buffer @var{vis}
@kindex C-c C-b
@kbd{C-c C-b} Sends the current buffer to the ESS process.
@end deffn

@deffn Command ess-eval-buffer-and-go @var{vis}
@kindex C-c M-b
@kbd{C-c M-b} Like @code{ess-eval-buffer} but additionally switches
point to the ESS process.
@end deffn

All the above @code{ess-eval-*} commands are useful for evaluating small
amounts of code and observing the results in the process buffer for
debugging purposes, or for generating transcripts from source files.
When editing R functions, it is generally preferable to use @kbd{C-c
C-l} to update the function's value.  In particular,
@code{ess-eval-buffer} is now largely obsolete.

A useful way to work is to divide the frame into two windows; one
containing the source code and the other containing the process buffer.
If you wish to make the process buffer scroll automatically when the
output reaches the bottom of the window, you will need to set the
variable @code{comint-move-point-for-output} to @code{'others} or
@code{t}.


@node Transcript Mode
@chapter Manipulating saved transcript files

Inferior R mode records the transcript (the list of all commands
executed, and their output) in the process buffer, which can be saved as
a @dfn{transcript file}, which should normally have the suffix
@file{.Rout}.  The most obvious use for a transcript file is as a static
record of the actions you have performed in a particular R session.
Sometimes, however, you may wish to re-execute commands recorded in the
transcript file by submitting them to a running ESS process.  This
is what Transcript Mode is for.

If you load file a with the suffix @file{.Rout} into Emacs, it is placed
in R Transcript Mode.  Transcript Mode is similar to inferior R mode
(@pxref{Entering commands}):
@cindex transcript mode motion
@cindex motion in transcript mode
paragraphs are defined as a command and its output, and you can move
though commands either with the paragraph commands or with @kbd{C-c C-p}
and @kbd{C-c C-n}.

@menu
* Resubmit::                    Resubmitting commands from the transcript file
* Clean::                       Cleaning transcript files
@end menu

@node Resubmit
@section Resubmitting commands from the transcript file

Three commands are provided to re-submit command lines from the
transcript file to a running ESS process.  They are:

@deffn Command ess-transcript-send-command
@kindex M-RET
@kbd{M-RET} Sends the current command line to the ESS process, and
execute it.
@end deffn

@deffn Command ess-transcript-copy-command
@kindex C-c RET
@kbd{C-c RET} Copy the current command to the ESS process, and switch to
it (ready to edit the copied command).
@end deffn

@deffn Command ess-transcript-send-command-and-move
@kindex RET
@kbd{RET} Sends the current command to the ESS process, and move to the
next command line.  This command is useful for submitting a series of
commands.
@end deffn

@noindent
Note that the first two commands are similar to those on the same keys
in inferior R Mode.  In all three cases, the commands should be
executed when the cursor is on a command line in the transcript; the
prompt is automatically removed before the command is submitted.

@node Clean
@section Cleaning transcript files

Yet another use for transcript files is to extract the command lines for
inclusion in an R source file or function.  Transcript mode provides
one command which does just this:

@deffn Command ess-transcript-clean-region @var{beg} @var{end} @var{even-if-read-only}
@kindex C-c C-w
@kbd{C-c C-w} Strip the transcript in the region (given by @var{beg} and
@var{end}), leaving only commands.  Deletes any lines not beginning with
a prompt, and then removes the prompt from those lines that remain.
Prefix argument @var{even-if-read-only} means to clean even if the
buffer is read-only.  Don't forget to remove any erroneous commands
first!
@end deffn

@noindent
The remaining command lines may then be copied to a source file or edit
buffer for inclusion in a function definition, or may be evaluated
directly (@pxref{Evaluating code}) using the code evaluation commands
from R mode, also available in R Transcript Mode.


@node Editing objects
@chapter Editing objects and functions

@cindex editing functions
ESS provides facilities for editing R objects within your Emacs
session.  Most editing is performed on R functions, although in
theory you may edit datasets as well.  Edit buffers are always
associated with files, although you may choose to make these files
temporary if you wish.  Alternatively, you may make use of a simple yet
powerful mechanism for maintaining backups of text representations of
R functions.  Error-checking is performed when R code is loaded
into the ESS process.

@menu
* Edit buffer::                 Edit objects in a specialized buffer
* Loading::                     Loading source files into the ESS process
* Error Checking::              Detecting errors in source files
* Indenting::                   Indenting and formatting S code
* Other edit buffer commands::  Commands for motion, completion and more
* Source Files::                Maintaining R source files
* Source Directories::          Names and locations of dump files
@end menu

@node Edit buffer
@section Creating or modifying R objects
@cindex edit buffer

To edit an object, type

@deffn Command ess-dump-object-into-edit-buffer @var{object}
@kindex C-c C-e C-d
@kbd{C-c C-e C-d} Edit an object in its own edit buffer.
@end deffn

from within the iESS process buffer (@code{*R*}).  You will then be
prompted for an object to edit: you may either type in the name of an
existing object (for which completion is available using the @kbd{TAB}
key),
@cindex completion, when prompted for object names
or you may enter the name of a new object.
@cindex creating new objects
@cindex new objects, creating
A buffer will be created containing the text representation of the
requested object or, if you entered the name of a non-existent object at
the prompt and the variable @code{ess-function-template}
@vindex ess-function-template
is non-@code{nil}, you will be presented with a template defined by that
variable, which defaults to a skeleton function construct.

You may then edit the function as required.  The edit buffer generated
by @code{ess-dump-object-into-edit-buffer} is placed in the @code{ESS}
major mode which provides a number of commands to facilitate editing
R source code.  Commands are provided to intelligently indent R
code, evaluate portions of R code and to move around R code
constructs.

@cindex dump files
@cindex reverting function definitions
Note: when you dump a file with @kbd{C-c C-e C-d}, ESS first checks
to see whether there already exists an edit buffer containing that
object and, if so, pops you directly to that buffer.  If not, ESS next
checks whether there is a file in the appropriate place with the
appropriate name (@pxref{Source Files}) and if so, reads in that file.
You can use this facility to return to an object you were editing in a
previous session (and which possibly was never loaded to the R
session).  Finally, if both these tests fail, the ESS process is
consulted and a @code{dump()} command issued.
@pindex dump()
If you want to force ESS to ask the ESS process for the object's
definition (say, to reformat an unmodified buffer or to revert back to
R's idea of the object's definition) pass a prefix argument to
@code{ess-dump-object-into-edit-buffer} by typing @kbd{C-u C-c C-e C-d}.

@node Loading
@section Loading source files into the ESS process

The best way to get information --- particularly function definitions
--- into R is to load them in as source file, using R's
@code{source} function.  You have already seen how to create source
files using @kbd{C-c C-e C-d}; ESS provides a complementary command for
loading source files (even files not created with ESS!) into the ESS
process, namely @code{ess-load-file} (@kbd{C-c M-l}).
@findex ess-load-file
@pindex source()
@pxref{Hot keys}.

@noindent
After typing @kbd{C-c M-l} you will prompt for the name of the file to
load into R; usually this is the current buffer's file which is the
default value (selected by simply pressing @kbd{RET} at the prompt).
You will be asked to save the buffer first if it has been modified (this
happens automatically if the buffer was generated with @kbd{C-c C-e
C-d}).  The file will then be loaded, and if it loads successfully you
will be returned to the ESS process.

@node Error Checking
@section Detecting errors in source files
@cindex errors
@cindex parsing errors
If any errors occur when loading a file with @code{C-c C-l}, ESS will
inform you of this fact.  In this case, you can jump directly to the
line in the source file which caused the error by typing @kbd{C-c `}
(@code{ess-parse-errors}).
@kindex C-c `
@findex ess-parse-errors
You will be returned to the offending file (loading it into a buffer if
necessary) with point at the line @Sl{} reported as containing the
error.  You may then correct the error, and reload the file.  Note that
none of the commands in an R source file will take effect if any
part of the file contains errors.

Sometimes the error is not caused by a syntax error (loading a
non-existent file for example). In this case typing @kbd{C-c `} will
simply display a buffer containing S's error message.  You can force
this behavior (and avoid jumping to the file when there @emph{is} a
syntax error) by passing a prefix argument to @code{ess-parse-errors}
with @kbd{C-u C-c `}.

@node Indenting
@section Indenting and formatting R code

ESS provides a sophisticated mechanism for indenting R source
code.  Compound statements (delimited by
@samp{@{} and @samp{@}}) are indented relative to their enclosing block.
In addition, the braces have been electrified to automatically indent to
the correct position when inserted, and optionally insert a newline at
the appropriate place as well.  Lines which continue an incomplete
expression are indented relative to the first line of the expression.
Function definitions, @code{if} statements, calls to @code{expression()}
and loop constructs are all recognized and indented appropriately.  User
variables are provided to control the amount of indentation in each
case, and there are also a number of predefined indentation styles to
choose from.

@cindex comments in R
Comments are also handled specially by ESS, using an idea borrowed from
the Emacs-Lisp indentation style.  By default,
comments beginning with @samp{###}
are aligned to the beginning of the line.  Comments beginning with
@samp{##} are aligned to the current level of indentation for the block
containing the comment.  Finally, comments beginning with @samp{#} are
aligned to a column on the right (the 40th column by default, but this
value is controlled by the variable @code{comment-column},)
@vindex comment-column
or just after the expression on the line containing the comment if it
extends beyond the indentation column.  You turn off the default
behavior by adding the line
@code{(setq ess-indent-with-fancy-comments nil)} to your @file{.emacs} file.
@vindex ess-indent-with-fancy-comments

ESS also supports roxygen2 entries which is R documentation maintained in
the source code as comments @xref{roxygen2}.

The indentation commands provided by ESS are:
@cindex indenting
@cindex formatting source code

@deffn Command ess-indent-or-complete
@kindex TAB
@kbd{TAB} Indents the current line as R code.

Try to indent first, and if code is already properly indented, complete
instead.  In ess-mode, only tries completion if
@code{tab-always-indent} is @code{'complete}.  See also
@code{ess-first-tab-never-complete}.
@end deffn

@defvr {User Option} ess-first-tab-never-complete
If non-@code{nil}, @kbd{TAB} never tries to complete in ess-mode.  The
default @code{'symbol} does not try to complete if the next char is a valid
symbol constituent.  There are more options, see the help (@kbd{C-h v}).
@end defvr

@deffn Command ess-indent-exp
@kindex C-M-q
@kindex M-C-q
@kindex ESC C-q
@kbd{TAB} Indents each line in the R (compound) expression which
follows point.  Very useful for beautifying your R code.
@end deffn

@deffn Command ess-electric-brace
@kindex @{
@kindex @}
@kbd{@{} @kbd{@}} The braces automatically indent to the correct
position when typed.
@end deffn

The following Emacs command are also helpful:

@table @kbd
@item RET
@itemx LFD
@kindex C-j
@code{newline-and-indent} Insert a newline, and indent the next line.
(Note that most keyboards nowadays do not have a @key{LINEFEED} key, but
@key{RET} and @kbd{C-j} are equivalent.)

@item M-;
@kindex M-;
@code{indent-dwim} Call the comment command you want (Do What I Mean).
@end table

@menu
* Styles:: Changing styles for indentation and alignment
@end menu

@node Styles
@subsection Changing styles for code indentation and alignment

The combined value of twelve variables (4 of three groups @code{ess-indent-*},
@code{ess-offset-*} and @code{ess-align-*}) that control indentation are
collectively termed a @i{style}.  ESS provides several styles covering the
common styles of indentation: @code{DEFAULT}, @code{OWN}, @code{GNU},
@code{BSD}, @code{K&R}, @code{C++}, @code{RRR}, @code{RRR+},
@code{Rstudio}, @code{Rstudio-}, and @code{CLB}.  The
variable @code{ess-style-alist} lists the value of each indentation
variable per style (and its documentation contains more).

@deffn Command ess-set-style
@kindex C-c C-e s
@kindex C-c C-e C-s
@kbd{C-c C-e C-s} (or @kbd{C-c C-e s}) sets the formatting style in this
buffer to be one of the predefined styles, see above.  The @code{DEFAULT}
style uses the default values for the indenting variables; The @code{OWN}
style allows you to use your own private values of the indentation
variable, see below.
@end deffn

@defvr {User Option} ess-style
The style to use. See the variable @code{ess-style-alist} for how
these groups (DEFAULT, OWN, GNU, RRR, @dots{}) map onto different
settings for variables. You can set this in your Emacs configuration
file:

@example
(setq ess-style 'C++)
@end example
@end defvr

@defvr {User Option} ess-style-alist
Predefined formatting styles for ESS code.  Values for all groups,
except OWN, are fixed.  To change the value of variables in the OWN
group, customize the variable @code{ess-own-style-list}.  The default
style in use is controlled by @code{ess-style}.
@end defvr

The styles @code{DEFAULT} and @code{OWN} are initially identical.  If
you wish to edit some of the default values, set
@code{ess-style} to @code{'OWN} and change
@code{ess-own-style-list}.
@xref{Customization}, for convenient ways to set both these variables.

If you prefer not to use the customization facility, you can change individual
indentation variables within a hook, for example:

@example
(defun myindent-ess-hook ()
  (setq ess-indent-level 4))
(add-hook 'ess-mode-hook 'myindent-ess-hook)
@end example

In the rare case that you'd like to add an entire new indentation style
of your own, copy the definition of @code{ess-own-style-list} to a new
variable and ensure that the last line of the @code{:set} declaration
calls @code{ess-add-style} with a unique name for your style
(e.g. @code{'MINE}).  Finally, add @code{(setq ess-style 'MINE)}
to use your new style.

@node Other edit buffer commands
@section Commands for motion, completion and more

A number of commands are provided to move across function definitions
in the edit buffer:

@deffn Command ess-goto-beginning-of-function-or-para
@kindex ESC C-a
@kindex C-M-a
@kbd{ESC C-a} aka @kbd{C-M-a} If inside a function go to the beginning
of it, otherwise go to the beginning of paragraph.
@end deffn

@deffn Command ess-goto-end-of-function-or-para
@kindex ESC C-e
@kindex C-M-e
@kbd{ESC C-e} aka @kbd{C-M-e} Move point to the end of the function
containing point.
@end deffn

@deffn Command ess-mark-function
@kindex ESC C-h
@kindex C-M-h
@kbd{ESC C-h} aka @kbd{C-M-h} Place point at the beginning of the R
function containing point, and mark at the end.
@end deffn

@noindent
Don't forget the usual Emacs commands for moving over balanced
expressions and parentheses: @xref{Lists, Lists and Sexps, Lists and
Sexps, Emacs, The GNU Emacs Reference Manual}.

@cindex completion in edit buffer
Completion is provided in the edit buffer in a similar fashion to the
process buffer: @key{TAB} first indents, and if there is nothing to
indent, completes the object or file name; @kbd{M-?} lists file
completions. See @xref{Completion}, for more.

Finally, @kbd{C-c C-z} (@code{ess-switch-to-inferior-or-script-buffer})
returns you to the @code{iESS} process buffer, if done from a script
buffer, placing point at the end of the buffer.  If this is done from
the @code{iESS} process buffer, point is taken to the script buffer.

In addition many commands available in the process buffer are also
available in the script buffer.  You can still read help files with
@kbd{C-c C-v}, edit another function with @kbd{C-c C-e C-d} and of
course @kbd{C-c C-l} can be used to load a source file into R.


@node Source Files
@section Maintaining R source files

Every edit buffer in ESS is associated with a @dfn{dump file} on disk.
Dump files are created whenever you type @kbd{C-c C-e C-d}
(@code{ess-dump-object-into-edit-buffer}), and may either be deleted
after use, or kept as a backup file or as a means of keeping several
versions of an R function.
@cindex dump files

@defvr {User Option} ess-delete-dump-files
If non-@code{nil}, dump files created with C-c C-e C-d are deleted
immediately after they are created by the ess-process.
@end defvr

Since immediately after R dumps an object's definition to a disk
file the source code on disk corresponds exactly to R's idea of the
object's definition, the disk file isn't needed; deleting it now has the
advantage that if you @emph{don't} modify the file (say, because you
just wanted to look at the definition of one of the standard R
functions) the source dump file won't be left around when you kill the
buffer.  Note that this variable only applies to files generated with
R's @code{dump} function; it doesn't apply to source files which already
exist.  The default value is @code{t}.

@defvr {User Option} ess-keep-dump-files
Variable controlling whether to delete dump files after a successful
load.  If @samp{nil}: always delete.  If @samp{ask}, confirm to delete.
If @samp{check}, confirm to delete, except for files created with
@code{ess-dump-object-into-edit-buffer}.  Anything else, never delete.
This variable only affects the behaviour of @code{ess-load-file}.  Dump
files are never deleted if an error occurs during the load.
@end defvr

After an object has been successfully (without error) loaded
back into R with @kbd{C-c C-l}, the disk file again corresponds
exactly (well, almost --- see below) to R's record of the object's
definition, and so some people prefer to delete the disk file rather
than unnecessarily use up space.  This option allows you to do just
that.

@cindex comments
@cindex project work in R
@cindex project work in S
@cindex historic backups
If the value of @code{ess-keep-dump-files} is @code{t}, dump files are
never deleted after they are loaded.  Thus you can maintain a complete
text record of the functions you have edited within ESS.  Backup files
are kept as usual, and so by using the Emacs numbered backup facility
--- @pxref{Backup Names, Single or Numbered Backups, Single or Numbered
Backups, emacs, The Gnu Emacs Reference Manual}, you can keep a historic
record of function definitions.  Another possibility is to maintain the
files with a version-control system such as git @xref{Version Control,
Version Control, Version Control, emacs, The Gnu Emacs Reference
Manual}.  As long as a dump file exists in the appropriate place for a
particular object, editing that object with @kbd{C-c C-e C-d} finds that
file for editing (unless a prefix argument is given) --- the ESS process
is not consulted.  Thus you can keep comments @emph{outside} the
function definition as a means of documentation that does not clutter
the R object itself.  Another useful feature is that you may format the
code in any fashion you please without R re-indenting the code every
time you edit it.  These features are particularly useful for
project-based work.

If the value of @code{ess-keep-dump-files} is nil, the dump file is always
silently deleted after a successful load with @kbd{C-c C-l}.  While this
is useful for files that were created with @kbd{C-c C-e C-d} it also applies
to any other file you load (say, a source file of function
definitions), and so can be dangerous to use unless you are careful.
Note that since @code{ess-keep-dump-files} is buffer-local, you can make
sure particular files are not deleted by setting it to @code{t} in the
Local Variables section of the file @xref{File Variables, Local
Variables in Files, Local Variables in Files, emacs, The Gnu Emacs
Reference Manual}.

A safer option is to set @code{ess-keep-dump-files} to @code{ask}; this
means that ESS will always ask for confirmation before deleting the
file.  Since this can get annoying if you always want to delete dump
files created with @code{C-c C-e C-d}, but not any other files, setting
@code{ess-keep-dump-files} to @code{check} (the default value) will
silently delete dump files created with @kbd{C-c C-e C-d} in the current
Emacs session, but query for any other file.  Note that in any case you
will only be asked for confirmation once per file, and your answer is
remembered for the rest of the Emacs session.

Note that in all cases, if an error (such as a syntax error) is detected
while loading the file with @kbd{C-c C-l}, the dump file is @emph{never}
deleted.  This is so that you can edit the file in a new Emacs session
if you happen to quit Emacs before correcting the error.

@cindex autosaving
Dump buffers are always autosaved, regardless of the value of
@code{ess-keep-dump-files}.

@node Source Directories
@section Names and locations of dump files

@cindex dump file names
Every dump file should be given a unique file name, usually the dumped
object name with some additions.

@defvr {User Option} ess-dump-filename-template
Template for filenames of dumped objects.  @code{%s} is replaced by the
object name.
@end defvr

@noindent
By default, dump file names are the user name, followed by @samp{.} and
the object and ending with @samp{.R}.  Thus if user @code{joe} dumps the
object @code{myfun} the dump file will have name @file{joe.myfun.R}.
The username part is included to avoid clashes when dumping into a
publicly-writable directory, such as @file{/tmp}; you may wish to remove
this part if you are dumping into a directory owned by you.

@cindex dump file directories
You may also specify the directory in which dump files are written:

@defvr {User Option} ess-source-directory
Directory name (ending in a slash) where R dump files are to be written.
@end defvr

By default, dump files are always written to @file{/tmp}, which is fine
when @code{ess-keep-dump-files} is @code{nil}.  If you are keeping dump
files, then you will probably want to keep them somewhere in your home
directory, say @file{~/R-source}.  This could be achieved by including
the following line in your Emacs configuration file:
@example
(setq ess-source-directory (expand-file-name "~/R-source/"))
@end example

If you would prefer to keep your dump files in separate directories
depending on the value of some variable, ESS provides a facility for
this also.  By setting @code{ess-source-directory} to a lambda
expression which evaluates to a directory name, you have a great deal of
flexibility in selecting the directory for a particular source file to
appear in.  The lambda expression is evaluated with the process buffer
as the current buffer and so you can use the variables local to that
buffer to make your choice.  For example, the following expression
causes source files to be saved in the subdirectory @file{Src} of the
directory the ESS process was run in.

@example
(setq ess-source-directory
      (lambda ()
         (concat ess-directory "Src/")))
@end example

@noindent
@vindex ess-directory
(@code{ess-directory} is a buffer-local variable in process buffers
which records the directory the ESS process was run from.)  This is
useful if you keep your dump files and you often edit objects with the
same name in different ESS processes.  Alternatively, if you often
change your R working directory during an R session, you may
like to keep dump files in some subdirectory of the directory pointed to
by the first element of the current search list.  This way you can edit
objects of the same name in different directories during the one R
session:
@cindex search list
@cindex working directory
@example
(setq ess-source-directory
   (lambda ()
       (file-name-as-directory
        (expand-file-name (concat
                           (car ess-search-list)
                           "/.Src")))))
@end example
@vindex ess-search-list

If the directory generated by the lambda function does not exist but can
be created, you will be asked whether you wish to create the directory.
If you choose not to, or the directory cannot be created, you will not
be able to edit functions.

@node Help
@chapter Reading help files
@cindex help files

ESS provides an easy-to-use facility for reading R help files from
within Emacs.  From within the ESS process buffer or any ESS edit
buffer, typing @kbd{C-c C-v} (@code{ess-display-help-on-object})
@findex ess-display-help-on-object
will prompt you for the name of an object for which you would like
documentation.  Completion is provided over all objects which have help
files.

If the requested object has documentation, you will be popped into a
buffer (named @code{*help(@var{obj-name})*}) containing the help file.
This buffer is placed in a special ESS help mode which disables the
usual editing commands but which provides a number of keys for paging
through the help file.

Help commands:

@table @kbd
@item ?
@findex ess-describe-help-mode
@code{ess-describe-help-mode} Pops up a help buffer with a list of the
commands available in R help mode.

@item h
@findex ess-display-help-on-object
@code{ess-display-help-on-object} Pop up a help buffer for a different
object.
@end table

Paging commands:
@cindex paging commands in help buffers

@table @kbd
@item DEL
@code{scroll-down-command} Move one screen backwards through the help file.

@item SPC
@code{scroll-up-command} Move one screen forwards through the help file.

@item >
@itemx <
@code{end/beginning-of-buffer} Move to the end or beginning of the help file,
respectively.
@end table

Section-based motion commands:

@table @kbd
@item n
@itemx p
@findex ess-skip-to-next-section
@findex ess-skip-to-previous-section
@code{ess-skip-to-previous-section} and @code{ess-skip-to-next-section}
Move to the next and previous section header in the help file,
respectively.  A section header consists of a number of capitalized
words, followed by a colon.
@end table

In addition, the @kbd{s} key followed by one of the following letters
will jump to a particular section in the help file.
Note that the exact headings available and capitalization scheme may vary across languages.

You may use @kbd{s ?} to get the current list of active key bindings.
@findex ess-skip-to-help-section

@table @samp
@item a
Arguments:

@item b
Background:

@item B
Bugs:

@item d
Description:

@item D
Details:

@item e
Examples:

@item n
Note:

@item O
Optional Arguments:

@item R
Required Arguments:

@item r
References:

@item s
See Also:

@item S
Side Effects:

@item u
Usage:

@item v
Value:

@item ?
Pops up a help buffer with a list of the defined section motion keys.
@end table

Evaluation:

@table @kbd
@item l
@findex ess-eval-line-and-step
@code{ess-eval-line-and-step} Evaluates the current line in the ESS
process, and moves to the next line.  Useful for running examples in
help files.

@item r
@findex ess-eval-region
@code{ess-eval-region} Send the contents of the current region to the
ESS process.  Useful for running examples in help files.
@end table

Quit commands:

@table @kbd
@item q
@findex ess-switch-to-end-of-ESS
@code{ess-help-quit} Return to previously selected buffer, and bury the
help buffer.

@item k
@code{kill-buffer} Return to previously selected buffer, and kills the
help buffer.

@item x
@code{ess-kill-buffer-and-go} Return to the ESS process, killing this
help buffer.
@end table

Miscellaneous:

@table @kbd
@item i
@code{ess-display-index} Prompt for a package and display it's help
index.

@item v
@code{ess-display-vignettes} Display all available vignettes.

@item w
@code{ess-display-help-in-browser} Display current help page with the
web browser.

@item /
@code{isearch-forward} Same as @kbd{C-s}.
@end table

In addition, all of the ESS commands available in the edit buffers are
also available in R help mode (@pxref{Edit buffer}).  Of course, the
usual (non-editing) Emacs commands are available, and for convenience
the digits and @key{-} act as prefix arguments.

If a help buffer already exists for an object for which help is
requested, that buffer is popped to immediately; the ESS process is
not consulted at all.  If the contents of the help file have changed,
you either need to kill the help buffer first, or pass a prefix argument
(with @kbd{C-u}) to @code{ess-display-help-on-object}.

Help buffers are marked as temporary buffers in ESS, and are deleted
when @code{ess-quit} or @code{ess-cleanup} are called.

Help buffers normally appear in another window within the current
frame.  If you wish help buffers to appear in their own frame (either
one per help buffer, or one for all help buffers), you can customize the
variable @code{ess-help-own-frame}.

Help buffers are displayed by calling the function
@code{ess-display-help}. You can customize where these buffers are
displayed by adding an entry in @code{display-buffer-alist}; for
examples, see @xref{Controlling buffer display,,, ess}. Or by
customizing the options @code{ess-help-own-frame},
@code{ess-help-frame-alist}, and @code{ess-display-buffer-reuse-frame}.

@findex ess-quit
@findex ess-cleanup
@cindex temporary buffers

@node Completion
@chapter Completion
@menu
* Object names::
* Function arguments::
* Minibuffer completion::
* Company::
* Icicles::
@end menu

@node  Object names
@section Completion of object names
@cindex completion of object names
@cindex command-line completion
@vindex ess-first-tab-never-complete

The @key{TAB} key is for completion. The value of the variable
@code{ess-first-tab-never-complete} controls when completion is allowed to
occur. In @code{ess-mode} @key{TAB} first tries to indent, and if there is
nothing to indent, complete the object name instead.

@table @kbd
@item TAB
@findex comint-dynamic-complete
@code{comint-dynamic-complete} Complete the R object name or
filename before point.
@end table

When the cursor is just after a partially-completed object name,
pressing @key{TAB} provides completion in a similar fashion to the rest of Emacs.  ESS maintains a list of all objects known to R
at any given time, which basically consists of all objects (functions
and datasets) in every attached directory listed by the @code{search()}
command
@pindex search()
along with the component objects of attached data frames
@cindex data frames

For example, consider three functions @code{binomplot()},
@code{binom.test()} and @code{binomial()}.  Typing @kbd{bin TAB} will
insert the characters @samp{om}, completing the longest prefix
(@samp{binom}) which distinguishes these three commands.  Pressing
@kbd{TAB} once more provides a list of the three commands which have
this prefix, allowing you to add more characters (say, @samp{.}) which
specify the function you desire.  After entering more characters
pressing @kbd{TAB} yet again will complete the object name up to
uniqueness, etc.  If you just wish to see what completions exist without
adding any extra characters, type @kbd{M-?}.

@deffn Command ess-complete-object-name
@kindex M-?
@kbd{M-?} List all possible completions of the object name at point.
@end deffn

ESS also provides completion over the components of named lists and
environments (after @samp{$}), S4 classes slots (after @@), package and
namespace objects (after :: and :::).
@cindex lists, completion on
@cindex completion on lists

@cindex completion on file names
Completion is also provided over file names, which is particularly
useful when using R functions such as @code{get()} or @code{scan()}
which require fully expanded file names.

In the iESS buffer, if the cursor is not in a string and does
not follow a (partial) object name, the @key{TAB} key has a third use:
it expands history references.  @xref{History expansion}.

Efficiency in completion is gained by maintaining a cache of objects
currently known to R; when a new object becomes available or is deleted,
only one component of the cache corresponding to the associated
directory needs to be refreshed.  If ESS ever becomes confused about
what objects are available for completion (such as when if refuses to
complete an object you @strong{know} is there), the command @kbd{M-x
ess-resynch}
@findex ess-resynch
forces the @emph{entire} cache to be refreshed, which should fix the
problem.

@node Function arguments
@section Completion of function arguments

When inside a function call (i.e. following `@code{(}'), @kbd{TAB}
completion also provides function arguments. If function is a generic,
completion will provide all the arguments of @code{S3} methods known to
@code{R}.

@cindex Space in function argument completion
In R versions newer than 2.14.1, ESS uses R's built-in completion
engine and will honor its settings. You may read about them by issuing
@code{?rc.options} at the prompt. For example, to have the equals sign
for function arguments contain spaces on both sides (e.g.
@code{lm(for)} suggests @code{formula = } rather than
@code{formula=}), you can adjust @code{funarg.suffix}:

@example
utils::rc.options(funarg.suffix = " = ")
@end example

A related functionality is @xref{ESS ElDoc}, which displays function
arguments in the echo area whenever the point is inside a function call.

@node Minibuffer completion
@section Minibuffer completion

@cindex IDO completions
@findex ess-use-ido, ess-completing-read
ESS uses @code{IDO} mechanism (part of default
Emacs) for minibuffer completion if the @code{ido} package is available
and the value of @code{ess-use-ido} it @code{t} (the default). The
completion command @code{ess-completing-read} falls back on classic
@code{completion-read} interface if this feature is not available for
whatever reason.

@node Company
@section Company
@cindex company
Another popular package for completion is company, short for complete
anything.  ESS provides support for company out-of-the-box. To disable
company support, set @code{ess-use-company} to @code{nil}. You can set
it to @code{'script-only} to only activate company in R scripts.

@node Icicles
@section Icicles
@cindex icicles
Another option for comprehensively handling completion in Emacs is via
Icicles (@uref{https://www.emacswiki.org/emacs/Icicles}).  It allows
users to have completions shown temporarily in the standard
@samp{*Completions*} buffer, and interactively select completion
candidates using several methods.  As of version 2013.04.04, Icicles
provides support for completion in ESS.  Please consult Icicles
documentation, which is easily accessible via @kbd{customize-group
Icicles}, for more details on installation and customization options.

Once installed, Icicles can be activated by evaluating):

@example
(require 'icicles)
(icy-mode 1)
@end example

Icicles can be toggled by typing @kbd{M-x icy}.

When Icicles is on, @kbd{TAB} offers completion, provided the conditions
determined by @code{ess-first-tab-never-complete} allow it.  Typing
@kbd{M-TAB} will attempt completion regardless.  Typing @kbd{M-?}  in
@code{ESS} or @code{iESS} modes brings up the relevant completion
candidates from which to choose.  Minibuffer input filters the available
candidates.  Use @kbd{TAB} for prefix completion or @kbd{S-TAB} for
substring or regexp completion.  Use @kbd{S-SPC} to match an additional
pattern (repeatable).  You can cycle among the matching candidates,
choosing with @kbd{RET} or @kbd{mouse-2}.


@node Developing with ESS
@chapter Developing with ESS

ESS provides several tools to help you with the development of your R
packages:

@menu
* ESS tracebug::                Visual debugging, breakpoints, tracing, watch etc.
* Editing documentation::       Tools to edit R documentation
* Namespaced Evaluation::               Develop your packages on the fly
@end menu

@node ESS tracebug
@section ESS tracebug
@cindex ESS tracebug
@findex ess-tracebug

@menu
* Getting started with tracebug:: Quick tutorial
@end menu

ESS @code{tracebug} offers visual debugging, interactive error
navigation, interactive backtrace, breakpoint manipulation, control over
R error actions, watch window and interactive flagging/unflagging of
functions for debugging.

@code{ess-tracebug} is on by default. You can toggle it on
and off with @kbd{M-x} @code{ess-tracebug}. To disable startup
activation of @code{ess-tracebug} set @code{ess-use-tracebug} to nil.

Tracebug functionality can be found on @code{ess-dev-map}, bound to
@kbd{C-c C-t}. Additionally, when subprocess is in a debugging state
@code{ess-debug-minor-mode} is active and the following additional
shortcuts are available:

@verbatim
* Interactive Debugging (`ess-debug-minor-mode-map'):

 M-C   . Continue                  . `ess-debug-command-continue'
 M-C-C . Continue multi            . `ess-debug-command-continue-multi'
 M-N   . Next step                 . `ess-debug-command-next'
 M-C-N . Next step multi           . `ess-debug-command-next-multi'
 M-U   . Up frame                  . `ess-debug-command-up'
 M-Q   . Quit debugging            . `ess-debug-command-quit'

@end verbatim


These are all the tracebug commands defined in @code{ess-dev-map}
(@kbd{C-c C-t ?} to show this table):

@verbatim

* Breakpoints (`ess-dev-map'):

 b   . Set BP (repeat to cycle BP type) . `ess-bp-set'
 B   . Set conditional BP               . `ess-bp-set-conditional'
 k   . Kill BP                          . `ess-bp-kil'
 K   . Kill all BPs                     . `ess-bp-kill-all'
 o   . Toggle BP state                  . `ess-bp-toggle-state'
 l   . Set logger BP                    . `ess-bp-set-logger'
 n   . Goto next BP                     . `ess-bp-next'
 p   . Goto previous BP                 . `ess-bp-previous'

  (C- prefixed equivalents are also defined)

* Debugging (`ess-dev-map'):
 `   . Show traceback                       . `ess-show-traceback' (also on C-c `)
 ~   . Show callstack                       . `ess-show-call-stack' (also on C-c ~)
 e   . Toggle error action (repeat to cycle). `ess-debug-toggle-error-action'
 d   . Flag for debugging                   . `ess-debug-flag-for-debugging'
 u   . Unflag for debugging                 . `ess-debug-unflag-for-debugging'
 w   . Watch window                         . `ess-watch'

  (C- prefixed equivalents are also defined)

* Navigation to errors (general Emacs functionality):

 C-x `, M-g n   . `next-error'
 M-g p          . `previous-error'

* Misc:

?   . Show this help		. `ess-tracebug-show-help'

@end verbatim

To configure how electric watch window splits the display see
@code{ess-watch-width-threshold} and @code{ess-watch-height-threshold}
variables.

@emph{Note:} Currently, ess-tracebug does not detect some of R's debug
related messages in non-English locales. To set your R messages to
English add the following line to your .Rprofile init file:

@verbatim
   Sys.setlocale("LC_MESSAGES", "C")
@end verbatim

@node Getting started with tracebug
@subsection Getting started with tracebug
@cindex tracebug tutorial
@findex getting started with tracebug

Consider a buffer with the following function:

@verbatim
test <- function(x){
  mean(x),
}
@end verbatim

Evaluating the function (e.g. @code{C-c C-c}) results in an error
about an unexpected comma. You can use @code{next-error} (bound by
default to @code{C-x `}, @code{M-g n}, and @code{M-g M-n}) to jump to
the place where the error occurred. Alternatively, use the mouse to
click on the error to jump to where it occurred.

Correct the error by deleting the comma. Now put point on @code{mean}
and set the breakpoint using @kbd{C-c C-t b} (@code{ess-bp-set}) and
reevaluate the function. Jump to the inferior buffer (possibly using
@code{C-c C-z}) and evaluate @code{test(1:10)}. An interactive debug
process starts, stopping at the breakpoint we just specified. Here you
can debug your function (what is @code{x} at this point?). Use
@code{M-N} to continue.

Let's replace our test function with one slightly more complicated:

@verbatim
test <- function(x){
  x <- x + 1
  y <- mean(x)
  x <- ifelse(x > 5, x, x - 100)
  list(x, y)
}
@end verbatim

Try setting multiple breakpoints. You can unset a breakpoint by killing
it with @kbd{C-c C-t k}. You can set conditional breakpoints too. Try
setting one by placing point on the line @code{x <- x + 1} and doing
@kbd{C-c C-t B}. ESS will ask for the condition. Let's set it to
@code{!is.numeric(x)}. After re-evaluating @code{test}, try calling
@code{test(1:100)} and @code{test('foo')}.

You can remove all breakpoints with @kbd{C-c C-t K}.

You can flag a function for debugging (similar to calling
@code{debug(test)} at R's prompt) by doing @kbd{C-c C-t d}. Try this
yourself by putting point over @code{test} and doing @kbd{C-c C-t d}.

If an error occurs, you can get the complete call stack by doing
@kbd{C-c `} or @kbd{C-c C-t `} (@code{ess-show-traceback}).

Tracebug also offers a watch window where you can watch values of
objects. Open it with @kbd{C-c C-t w} (@code{ess-watch}). Initially you
aren't watching anything. Add something with @kbd{a} (e.g. @code{a
test}). The watch window displays what the object is at any given time
and automatically updates. Quit the watch window with @kbd{q}.

@node Editing documentation
@section Editing documentation

ESS provides two ways of writing documentation for R objects. Either
using the standard R documentation system or using in-source
documentation written as structured comment fields for use with the
roxygen2 package.

@menu
* R documentation files::       Edit objects in a specialized buffer
* roxygen2::                     Loading source files into the ESS process
@end menu

@node R documentation files
@subsection Editing R documentation (Rd) files

@R{} objects are documented in files written in the @dfn{R
documentation} (``Rd''), a simple markup language closely resembling
(La)@TeX{}, which can be processed into a variety of formats, including
La@TeX{}, @acronym{HTML}, and plain text.  Rd format is described in
section ``Rd format'' of the ``Writing R Extensions'' manual in the R
distribution. ESS has several features that facilitate editing Rd files.

Visiting an Rd file as characterized by its extension @file{Rd} will
activate Rd Mode, which provides several facilities for making editing R
documentation files more convenient, by helping with indentation,
insertions, even doing some of the typing for you (with Abbrev Mode),
and by showing Rd keywords, strings, etc.@: in different faces (with
Font Lock Mode).

In Rd mode, the following special Emacs commands can be used in addition
to the standard Emacs commands.

@table @kbd
@item C-h m
Describe the features of Rd mode.

@item LFD
@itemx RET
Reindent the current line, insert a newline and indent the new line
(@code{reindent-then-newline-and-indent}).  An abbrev before point is
expanded if @code{abbrev-mode} is non-@code{nil}.

@item TAB
Indent current line based on its contents and on previous lines.
(@code{indent-according-to-mode}).

@item C-c C-e
Insert a ``skeleton'' with Rd markup for at least all mandatory entries
in Rd files (@code{Rd-mode-insert-skeleton}).  Note that many users
might prefer to use the R function @code{prompt} on an existing R object
to generate a non-empty Rd ``shell'' documenting the object (which
already has all information filled in which can be obtained from the
object).

@item C-c C-f
Insert ``font'' specifiers for some of the Rd markup commands markup
available for emphasizing or quoting text, including markup for URLs and
email addresses (@code{Rd-font}).  @kbd{C-c C-f} is only a prefix; see
e.g.@: @kbd{C-c C-f TAB} for the available bindings.  Note that
currently, not all of the Rd text markup as described in section
``Marking text'' of ``Writing R Extensions'' can be accessed via
@kbd{C-c C-f}.

@item C-c C-j
Insert a suitably indented  @samp{\item@{} on the next line
(@code{Rd-mode-insert-item}).

@item C-c C-p
Preview a plain text version (``help file'', @pxref{Help}) generated
from the Rd file (@code{Rd-preview-help}).
@end table

In addition, when editing Rd files one can interact with a running R
process in a similar way as when editing R language files.  For example,
@kbd{C-c C-v} provides access to on-line help, and @kbd{C-c C-n} sends
the current line to the R process for evaluation.  This interaction is
particularly useful when editing the examples in the Rd file.  See
@kbd{C-h m} for all available commands.

Rd mode also provides access to abbreviations for most of the Rd markup
commands.  Type @kbd{M-x list-abbrevs} with Abbrev mode turned on to
list all available abbrevs.  Note that all Rd abbrevs start with a grave
accent.

Rd mode can be customized via the following variables.

@defvr {User Option} Rd-mode-hook
Hook to be run when Rd mode is entered.
@end defvr

@defvr {User Option} Rd-indent-level
The indentation of Rd code with respect to containing blocks.  Default
is 2.
@end defvr

@defvr {User Option} Rd-to-help-command
The shell command used for converting Rd source to help text.  Default
is @samp{R CMD Rd2txt}.
@end defvr

To automatically turn on the abbrev and font-lock features of Rd mode,
add the following lines to one of your Emacs startup files:

@example
(add-hook 'Rd-mode-hook
          (lambda ()
           (abbrev-mode 1)
           (font-lock-mode 1)))
@end example

@node roxygen2
@subsection Editing roxygen2 documentation
@cindex roxygen2
@cindex roxy
@cindex ess-roxy

The roxygen2 R package makes it possible to keep the intended contents
for Rd files as structured comments in the R source files. roxygen2 can
then parse R files and generate appropriate Rd files from these
comments, fill the usage fields as well as updating @file{NAMESPACE}
files. See the roxygen2 documentation found via
@uref{https://roxygen2.r-lib.org} for more information on its usage. An
example of an roxygen2 entry for a simple R function can look like this:

@example
@verbatim
##' Description of the function
##'
##' Further details about this function
##' @title A title
##' @param me all parameters must be listed and documented
##' @return Description of the return value
##' @author The author
myfun <- function(me)
  cat("Hello", me, "\n")
@end verbatim
@end example
The entry is immediately preceding the object to document and all lines
start with the roxygen2 prefix string, in this case @code{##'}. ESS
provides support to edit these documentation entries by providing line
filling, navigation, template generation etc. Syntax highlighting is
provided.

roxygen2 is customized by the variables in the customization group ``Ess
Roxy''. Customizations include the roxygen2 prefix, use of folding
to toggle visibility of roxygen2 entries and the roxygen2 template.

All ESS roxygen2 support is defined in @file{ess-roxy.el} which is loaded
by default in ESS. The following special Emacs commands are provided.

@deffn Command ess-roxy-update-entry
@kindex C-c C-o C-o
@kbd{C-c C-o C-o} Generate a roxygen2 template or update the parameter
list in roxygen2 entry at point (or above the function at the
point). Documented parameters that are not in the function are placed
last in the list, parameters that are not documented and not in the
definition are dropped. Parameter descriptions are filled if
@code{ess-roxy-fill-param-p} is non-nil.
@end deffn

@deffn Command ess-roxy-toggle-roxy-region @var{beg} @var{end}
@kindex C-c C-o C-c
@kbd{C-c C-o C-c} Toggle the presence of the leading roxygen2 string on
all lines in the marked region (between @var{beg} and
@var{end}. Convenient for transferring text to roxygen2 entries and to
evaluate example fields.
@end deffn

@deffn Command ess-roxy-preview-Rd @var{name-file}
@kindex C-c C-o C-r
@kbd{C-c C-o C-r} Use the attached R process to parse the entry at point
to obtain the Rd code. Convenient for previewing and checking
syntax. When used with the prefix argument @var{name-file},
i.e. @kbd{C-u C-c C-e C-r}, place the content in a buffer associated
with a Rd file with the same name as the documentation. Requires the
roxygen2 package to be installed.
@end deffn

@deffn Command ess-roxy-preview-HTML @var{visit-instead-of-open}
@kindex C-c C-o C-t
@kbd{C-c C-o C-t} Use the attached R process to parse the entry at to
generate HTML for the entry and open it in a browser. When used with the
prefix argument @var{visit-instead-of-open}, i.e. @kbd{C-u C-c C-e C-t},
visit the generated HTML file instead.  Requires the roxygen2 and tools
packages to be installed.
@end deffn

@deffn Command ess-roxy-previous-entry
@kindex C-c C-o p
@kbd{C-c C-o p} Go to start of the roxygen2 entry above point.
@end deffn

@deffn Command ess-roxy-next-entry
@kindex C-c C-o n
@kbd{C-c C-o n} Go to end of the roxygen2 entry above point.
@end deffn

@deffn Command ess-roxy-hide-all
@kindex C-c C-o C-h
@kbd{C-c C-o C-h} Use the hideshow mode to fold away the visibility of
all roxygen2 entries. Hide-show support must be enabled for this binding
to get defined.
@end deffn

ESS also advises the following standard editing functions in order to
make roxygen2 editing more intuitive:

@c not very precise, TAB calls it indirectly
@table @kbd
@item TAB
@findex ess-R-complete-object-name
@code{ess-R-complete-object-name} Complete roxygen2 tag at
point. E.g. doing @kbd{TAB} when the point is at the end of @code{@@par}
completes to @code{@@param}.

@item M-h
@findex mark-paragraph
@code{mark-paragraph} If the transient mark mode is active, place mark
and point at start end end of the field at point and activate the mark.

@item TAB
@findex ess-indent-command
@code{ess-indent-command} If hide-show support is enabled, fold away the
visibility of the roxygen2 entry at point.

@item M-q
@findex fill-paragraph
@code{fill-paragraph} Fill the roxygen2 field at point.

@item C-a
@findex ess-roxy-move-beginning-of-line
@code{ess-roxy-move-beginning-of-line} Move to the point directly to the right of
the roxygen2 start string. If already there, move to the beginning of the line.

@item RET
@findex newline-and-indent
@code{newline-and-indent} Insert a new line and the roxygen2 prefix
string.
@end table

@node Namespaced Evaluation
@section Namespaced Evaluation
@cindex ess developer

In non package files evaluation commands, @xref{Evaluating code}, send
portions of the current buffer environment (@code{R_GlobalEnv}. When
developing packages, ESS sends code to the corresponding package
namespace and (for visible objects) into package environment (visible on
search path). All objects that are assigned are displayed in the
minibuffer alongside the environment in which they are assigned.

Here is a short overview of how namespace and package environments work
in R. All objects defined in a package 'foo' are stored in an
environment called 'namespace:foo'. Parent environment of
'namespace:foo' is an environment 'imports:foo' which contains copies of
all objects from other packages which 'foo' imports. Parent environment
of 'imports:foo' is the 'namespace:base'. Parent environment of
'namespace:base' is .GlobalEnv. Thus functions and methods stored in
'namespace:foo' see all the objects in .GlobalEnv unless shadowed by
objects in 'imports:foo', 'namespace:base', or 'namespace:foo'
itself. There is another environment associated with 'foo' -
'package:foo'. This environment stores *copies* of exported objects from
'namespace:foo' and is placed on the search() path, i.e. if 'foo' is
loaded and if you start with .GlobalEnv and iteratively call
parent.env() you will get eventually to 'package:foo'. Thus all methods
and functions defined in .GlobalEnv can "see" objects in 'package:foo'
environment. See also
@uref{https://cran.r-project.org/doc/manuals/R-ints.html#Namespaces}.

Occasionally you want to evaluate into a package from a non-package file,
or the other way around, evaluate into @code{GlobalEnv} from inside a
package. In such cases @kbd{C-c C-t C-s} is your friend.

@deffn Command ess-r-set-evaluation-env @var{arg}
@kindex C-c C-t C-s
@kbd{C-c C-t C-s} Set or unset the current evaluation environment (a package).
@end deffn

@node Extras
@chapter Other ESS features and tools

ESS has a few extra features, which didn't fit anywhere else.
@c todo: sort alphabetically
@menu
* ESS ElDoc::                   Display function arguments in minibuffer
* ESS Flymake::                 On-the-fly syntax checking
* Handy commands::              Usefully commands at your fingertips
* Highlighting::                Syntactic highlighting of buffers
* Parens::                      Parenthesis matching
* Graphics::                    Using graphics with ESS
* Imenu::                       Support for Imenu in ESS
* Toolbar::                     Support for toolbar in ESS
* Xref::                        Xref
* Rdired::                      Directory editor for R objects
* Package listing::             R object/package management and help utilities
* Org::                         Interaction with Org mode
* Sweave and AUCTeX::           Support for Sweave in ESS and AUCTeX
@end menu

@node ESS ElDoc
@section ElDoc
@cindex ElDoc
In @code{ElDoc} mode, the echo area displays function's arguments at
point. ElDoc is active by default in @code{ess-mode} and
@code{inferior-ess-mode} buffers. To activate it only in @code{ess-mode}
buffers, place the following into your init file:

@example
(setq ess-use-eldoc 'script-only)
@end example

@defvr {User Option} ess-use-eldoc
If @samp{t}, activate eldoc in ess-mode and inferior-ess-mode buffers.
If @samp{'script-only} activate in ess-mode buffers only.  Set
@code{ess-use-eldoc} to @code{nil} to stop using @code{ElDoc}
altogether.
@end defvr

@defvr {User Option} ess-eldoc-show-on-symbol
This variable controls whether the help is shown only inside function
calls. If set to @samp{t}, @code{ElDoc} shows help string whenever the
point is on a symbol, otherwise (the default), shows only when the point
is in a function call, i.e. after @samp{'('}.
@end defvr

@defvr {User Option} ess-eldoc-abbreviation-style
The variable determines how the doc string should be abbreviated to fit
into minibuffer. Posible values are @samp{nil}, @samp{mild},
@samp{normal}, @samp{strong} and @samp{aggressive}. Please see the
documentation of the variable for more details. The default filter is
@samp{normal}.
@end defvr

Ess-eldoc also honors the value of
@code{eldoc-echo-area-use-multiline-p}, which if set to @samp{nil}, will
cause the truncation of doc string indifferent of the value of
@code{ess-eldoc-abbreviation-style}. This way you can combine different
filter levels with the truncation.

@node ESS Flymake
@section Flymake
@cindex flymake
The minor mode @code{flymake-mode} provides on-the-fly syntax
checking. ESS provides support for flymake in R-mode in Emacs versions
26 and newer. It is enabled by default, to disable it you may set
@code{ess-use-flymake} to @code{nil}. In order to use it, you may need
to install the @code{lintr} R package, available from CRAN.

@defvr {User Option} ess-use-flymake
When non-nil, use flymake. If @code{'process}, only use flymake when the
buffer has an inferior process running.
@end defvr

@defvr {User Option} ess-r-flymake-linters
This variable describes the linters to use. It can either be a string
with an R expression to be used as-is or a list of strings where each
element is passed to @code{lintr::with_defaults}. See the help page
for @code{lintr::default_linters} for information on available linters
and their defaults.
@end defvr

@defvr {User Option} ess-r-flymake-lintr-cache
When @code{t} (the default), lintr uses a cache. Change to @code{nil}
to disable lintr's caching mechanism.
@end defvr

@node Handy commands
@section Handy commands
@cindex Handy commands
@findex ess-handy-commands
@vindex ess-handy-commands
@findex ess-smart-comma
@kindex ,

@deffn Command ess-handy-commands
Request and execute a command from @code{ess-handy-commands} list.
@end deffn

@defvr {User Option} ess-handy-commands
An alist of custom ESS commands available for call by
@code{ess-handy-commands} and @code{ess-smart-comma} function.

Currently contains:

@table @asis
@item change-directory
    @code{ess-change-directory}
@item help-index
          @code{ess-display-index}
@item help-object
         @code{ess-display-help-on-object}
@item vignettes
           @code{ess-display-vignettes}
@item objects[ls]
         @code{ess-execute-objects}
@item search
              @code{ess-execute-search}
@item set-width
           @code{ess-execute-screen-options}
@item install.packages
    @code{ess-install.packages}
@item library
             @code{ess-library}
@item setRepos
            @code{ess-setRepositories}
@item sos
                 @code{ess-sos}
@end table
@end defvr

@c If ``@kbd{,}`` (@code{ess-smart-comma}) is invoked at the beginning of
@c line of an ESS inferior buffer (i.e. at process marker), ESS requests and
@c executes a command from @code{`ess-handy-commands'} list. If
@c @code{ess-R-smart-operators} is t @code{`ess-smart-comma} also inserts
@c `` `` after comma.

Handy commands: @code{ess-library}, @code{ess-install.packages}, etc -
ask for item with completion and execute the correspond
command. @code{ess-sos} is a interface to @code{findFn} function in
package @code{sos}. If package @code{sos} is not found, ask user for
interactive install.


@node Highlighting
@section Syntactic highlighting of buffers

ESS provides Font-Lock (@pxref{Faces,,Using Multiple Typefaces, emacs,
The Gnu Emacs Reference Manual}) patterns for inferior R Mode, S Mode,
and R Transcript Mode buffers.
@cindex font-lock mode
@cindex highlighting

The font-lock patterns are defined by the following variables, which you
may modify if desired:

@defvr {User Option} inferior-R-font-lock-keywords
Font-lock patterns for inferior *R* processes.  (There is a
corresponding @code{inferior-S-font-lock-keywords} for *S* processes.)
The default value highlights prompts, inputs, assignments, output
messages, vector and matrix labels, and literals such as @samp{NA} and
@code{TRUE}.
@end defvr

@defvr {User Option} ess-R-font-lock-keywords
Font-lock patterns for ESS R programming mode.  (There is a
corresponding @code{ess-S-font-lock-keywords} for S buffers.) The
default value highlights function names, literals, assignments, source
functions and reserved words.
@end defvr

@node Parens
@section Parenthesis matching

Emacs has facilities for highlighting the parenthesis matching the
parenthesis at point.  This feature is very useful when trying to
examine which parentheses match each other.  This highlighting also
indicates when parentheses are not matching. You may activate it putting
this in your Emacs configuration file:

@example
(show-paren-mode)
@end example


@node Graphics
@section Using graphics with ESS

@cindex graphics
One of the main features of R is its ability to generate high-resolution
graphics plots. When using R in a windowing environment, no additional
configuration is necessary; plots will be shown in a new (non-Emacs)
window.

If not using a windowing environment or S, ESS provides a number of
features for dealing with such plots.

@menu
* printer::                     The printer() graphics driver
* X11::                         The X11() (and other X Windows based) driver
* winjava::                     Java Graphics Device
@end menu

@node printer
@subsection Using ESS with the @code{printer()} driver

This is the simplest (and least desirable) method of using graphics
within ESS.  S's @code{printer()} device driver produces crude character
based plots which can be contained within the ESS process buffer
itself.  To start using character graphics, issue the S command
@example
printer(width=79)
@end example
@pindex printer()
(the @code{width=79} argument prevents Emacs line-wrapping at column
80 on an 80-column terminal.  Use a different value for a terminal with
a different number of columns.) Plotting commands do not generate
graphics immediately, but are stored until the @code{show()} command
is issued, which displays the current figure.

@node X11
@subsection Using ESS with windowing devices

@cindex X Windows
Of course, the ideal way to use graphics with ESS is to use a windowing
system.  Under X Windows, or X11, this requires that the DISPLAY environment
variable be appropriately set.

@node winjava
@subsection Java Graphics Device

@cindex winjava

S+6.1 and newer on Windows contains a java library that supports graphics.  Send
the commands:

@example
library(winjava)
java.graph()
@end example

@noindent to start the graphics driver.  This allows you to use ESS for both
interaction and graphics within S-PLUS.  (Thanks to Tim Hesterberg for
this information.)

@node Imenu
@section Imenu

Imenu is an Emacs tool for providing mode-specific buffer indexes.  In
some of the ESS editing modes (currently SAS and R), support for Imenu
is provided.  For example, in R mode buffers, the menubar should display
an item called "Imenu-R".  Within this menubar you will then be offered
bookmarks to particular parts of your source file (such as the starting
point of each function definition).

Imenu works by searching your buffer for lines that match what ESS
thinks is the beginning of a suitable entry, for example the beginning of a
function definition.  To examine the regular expression that ESS uses,
check the value of @code{imenu-generic-expression}.  This value is set
by various ESS variables such as @code{ess-imenu-S-generic-expression}.

@node Toolbar
@section Toolbar

The R and S editing modes have support for a toolbar.  This toolbar
provides icons to act as shortcuts for starting new S/R processes, or
for evaluating regions of your source buffers.  The toolbar should be
present if your Emacs can display images.  @xref{Customization}, for ways
to change the toolbar.

@node Xref
@section Xref
@cindex xref
@cindex finding function definitions
Xref is an Emacs interface that supports finding ``identifiers,''
usually function definitions in ESS's view. ESS ships with support for
Xref in Emacs versions 25.1 and higher. For how to use this feature, see
@xref{Xref,,, emacs, The Gnu Emacs Reference Manual}.

@node Rdired
@section Rdired

Ess-rdired provides a dired-like buffer for viewing, editing and
plotting objects in your current R session.  If you are used to using
the dired (directory editor) facility in Emacs, this mode gives you
similar functionality for R objects.

Start an R session with @kbd{M-x R} and then store a few
variables, such as:

@example
s <- sin(seq(from=0, to=8*pi, length=100))
x <- c(1, 4, 9)
y <- rnorm(20)
z <- TRUE
@end example

Then use @kbd{M-x ess-rdired} to create a buffer listing the
objects in your current environment and display it in a new window:
@example
            mode length
  s      numeric    100
  x      numeric      3
  y      numeric     20
  z      logical      1
@end example

Type @kbd{C-h m} or @kbd{?} to get a list of the keybindings for this
mode.  For example, with your point on the line of a variable, `p' will
plot the object, `v' will view it, and `d' will mark the object for
deletion (`x' will actually perform the deletion).


@node Package listing
@section Package listing

ESS[R] provides several commands to list and manage packages and
objects under the @code{C-c C-.} keymap:

@deffn Command ess-r-package-list-local-packages
@kindex C-c C-. l
@kbd{C-c C-. l} List all packages in all available libraries.
@end deffn

@deffn Command ess-r-package-list-available-packages
@kindex C-c C-. r
@kbd{C-c C-. r} List available packages from repositories listed by
@command{getOptions(``repos'')} in the current R session.
@end deffn

@deffn Command ess-r-package-update-packages @var{lib} @var{repos}
@kindex C-c C-. u
@kbd{C-c C-. u} Update packages in a particular library @var{lib} and
repository @var{repos}.
@end deffn

@deffn Command ess-rutils-rm-all
@kindex C-c C-. m
@kbd{C-c C-. m} Remove all R objects.
@end deffn

@deffn Command ess-rutils-load-workspace
@kindex C-c C-. w
@kbd{C-c C-. w} Load a workspace file into R.
@end deffn

@deffn Command ess-rutils-save-workspace
@kindex C-c C-. s
@kbd{C-c C-. s} Save a workspace file.
@end deffn

@deffn Command ess-change-directory
@kindex C-c C-. d
@kbd{C-c C-. d} Change the working directory for the current R session.
@end deffn

@deffn Command ess-rutils-html-docs
@kindex C-c C-. H
@kbd{C-c C-. H} Use @command{browse-url} to navigate R html
documentation.
@end deffn

Functions for listing objects and packages
(@command{ess-rutils-local-pkgs}, @command{ess-rutils-repos-pkgs}, and
@command{ess-rutils-objs}) show results in a separate buffer and window,
in @code{ess-rutils-mode}, providing useful key bindings in this mode
(type @kbd{?} in this buffer for a description).


@node Org
@section Interaction with Org mode

Org-mode (@uref{https://orgmode.org}) now supports reproducible research
and literate programming in many languages (including R) -- see chapter
15 of the Org manual
(@uref{https://orgmode.org/org.html#Working-with-Source-Code}).  For ESS
users, this offers a document-based work environment within which to
embed ESS usage.  R code lives in code blocks of an Org document, from
which it can be edited in ess-mode, evaluated, extracted ("tangled") to
pure code files.  The code can also be exported ("woven") with the
surrounding text to several formats including HTML and LaTeX.  Results of
evaluation including figures can be captured in the Org document, and
data can be passed from the Org document (e.g. from a table) to the ESS
R process.  (This section contributed by Dan Davison and Eric Schulte.)


@node Sweave and AUCTeX
@section Support for Sweave in ESS and AUCTeX

Libraries for literate data analysis are obsolete and not loaded by
default. This includes @code{ess-noweb}, @code{ess-swv}, and related
functionality like @code{Rnw-mode}. Users are encouraged to switch to
one of several other packages that deal with these modes. For example,
polymode @url{https://github.com/polymode/poly-R/},
@url{https://polymode.github.io/}, or markdown-mode with edit-indirect
@url{https://jblevins.org/projects/markdown-mode}.

ESS provides support for writing and processing Sweave
(@uref{https://leisch.userweb.mwn.de/Sweave/}), building up on
Emacs' ess-noweb-mode for literate programming.  When working on an Sweave
document, the following key bindings are available:

@deffn Command ess-swv-weave @var{choose}
@kindex M-n s
@kbd{M-n s} Run Sweave on the current .Rnw file.  If @var{choose} is
non-@samp{nil}, offer a menu of available weavers.
@end deffn

@deffn Command ess-swv-latex
@kindex M-n l
@kbd{M-n l} Run LaTeX after Sweave'ing.
@end deffn

@deffn Command ess-swv-PS
@kbd{M-n p} Generate and display a postscript file after LaTeX'ing.
@end deffn

@deffn Command ess-swv-PDF @var{pdflatex-cmd}
@kindex M-n P
@kbd{M-n P} Generate and display a PDF file after LaTeX'ing.  Optional
argument @var{pdflatex-cmd} is the command to use, which by default, is
the command used to generate the PDF file is the first element of
@code{ess-swv-pdflatex-commands}.
@end deffn

@defvr {User Option} ess-swv-pdflatex-commands
Commands used by @code{ess-swv-PDF} to run a version of pdflatex; the
first entry is the default command.
@end defvr

Sweave'ing with @code{ess-swv-weave} starts an inferior-ESS process, if
one is not available.  Other commands are available from the
@samp{Sweaving, Tangling, ...} submenu of the Noweb menu.

AUCTeX (@uref{https://www.gnu.org/software/auctex}) users may prefer to
set the variable @code{ess-swv-plug-into-AUCTeX-p} (available from the
``ESS Sweave'' customization group) to t.  Alternatively, the same can
be achieved by activating the entry ``AUCTeX Interface'' from the
@samp{Sweaving, Tangling, ...} submenu, which toggles this variable on
or off.  When the interface is activated, new entries for Sweave'ing and
LaTeX'ing thereafter are available from AUCTeX's ``Command'' menu.
Sweave'ing can, thus, be done by @kbd{C-c C-c Sweave RET} without an
inferior-ESS process.  Similarly, LaTeX'ing can be done by @kbd{C-c C-c
LaTeXSweave RET}.  In both cases, the process can be monitored with
@kbd{C-c C-l} (@code{TeX-recenter-output-buffer}).  Open the viewer with
@kbd{C-c C-v} (@code{TeX-view}), as usual in AUCTeX.

@node ESS for R
@chapter Overview of ESS features for the S family
@menu
* ESS(R)--Editing files::
* iESS(R)--Inferior ESS processes::
* Philosophies for using ESS(R)::
* Example ESS usage::
@end menu
@include help-s.texi

@node ESS for SAS
@chapter ESS for SAS

@menu
* ESS(SAS)--Design philosophy::
* ESS(SAS)--Editing files::
* ESS(SAS)--TAB key::
* ESS(SAS)--Batch SAS processes::
* ESS(SAS)--Function keys for batch processing::
* iESS(SAS)--Interactive SAS processes::
* iESS(SAS)--Common problems::
* ESS(SAS)--Graphics::
* ESS(SAS)--Windows::
@end menu
@include help-sas.texi

@node ESS for BUGS
@chapter ESS for BUGS

@include help-bugs.texi

@node ESS for JAGS
@chapter ESS for JAGS

@include help-jags.texi


@node Mailing lists/bug reports
@chapter Bugs and Bug Reporting, Mailing Lists

@menu
* Bugs::
* Reporting Bugs::
* Mailing Lists::
* Help with Emacs::
@end menu

@node Bugs
@section Bugs
@cindex bugs
@include bugs.texi

@node Reporting Bugs
@section Reporting Bugs
@cindex bug reports
@findex ess-submit-bug-report
@include bugrept.texi

@node Mailing Lists
@section Mailing Lists

@include mailing.texi

@node Help with Emacs
@section Help with Emacs

Emacs is a complex editor with many abilities that we do not have space
to describe here.  If you have problems with other features of Emacs
(e.g. printing), there are several sources to consult, including the
Emacs FAQs (try @kbd{M-x view-emacs-faq})
and EmacsWiki (@uref{https://www.emacswiki.org}).  Please consult them
before asking on the mailing list about issues that are not specific to
ESS.

@node Customization
@appendix Customizing ESS
@cindex customization

ESS can be easily customized to your taste simply by including the
appropriate lines in your Emacs configuration file.  There are numerous
variables which affect the behavior of ESS in certain situations which
can be modified to your liking.  Keybindings may be set or changed to
your preferences, and for per-buffer customizations hooks are also
available.

Most of these variables can be viewed and set using the Custom facility
within Emacs.  Type @kbd{M-x customize-group RET ess RET} to see all the
ESS variables that can be customized.  Variables are grouped by subject
to make it easy to find related variables.

@node Indices
@unnumbered Indices

@menu
* Key index::
* Function and program index::
* Variable index::
* Concept index::
@end menu

@node Key index
@unnumberedsec Key index
@printindex ky

@node Function and program index
@unnumberedsec Function and program index
@printindex fn

@node Variable index
@unnumberedsec Variable index
@printindex vr

@node Concept index
@unnumberedsec Concept Index
@printindex cp


@bye
@iftex
@lucidbook
@bindingoffset = 0.5in
@parindent = 0pt
@end iftex

@comment Local Variables:
@comment TeX-master: "ess.texi"
@comment End:
