R/R-lang.html
Tom Callaway 2b16cd30cb 3.3.1
2016-07-05 14:26:48 -04:00

6226 lines
337 KiB
HTML
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<!-- This manual is for R, version 3.3.1 (2016-06-21).
Copyright (C) 2000-2016 R Core Team
Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation
approved by the R Core Team. -->
<!-- Created by GNU Texinfo 6.1, http://www.gnu.org/software/texinfo/ -->
<head>
<title>R Language Definition</title>
<meta name="description" content="R Language Definition">
<meta name="keywords" content="R Language Definition">
<meta name="resource-type" content="document">
<meta name="distribution" content="global">
<meta name="Generator" content="texi2any">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link href="#Top" rel="start" title="Top">
<link href="#Function-and-Variable-Index" rel="index" title="Function and Variable Index">
<link href="#SEC_Contents" rel="contents" title="Table of Contents">
<style type="text/css">
<!--
a.summary-letter {text-decoration: none}
blockquote.indentedblock {margin-right: 0em}
blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
blockquote.smallquotation {font-size: smaller}
div.display {margin-left: 3.2em}
div.example {margin-left: 3.2em}
div.lisp {margin-left: 3.2em}
div.smalldisplay {margin-left: 3.2em}
div.smallexample {margin-left: 3.2em}
div.smalllisp {margin-left: 3.2em}
kbd {font-style: oblique}
pre.display {font-family: inherit}
pre.format {font-family: inherit}
pre.menu-comment {font-family: serif}
pre.menu-preformatted {font-family: serif}
pre.smalldisplay {font-family: inherit; font-size: smaller}
pre.smallexample {font-size: smaller}
pre.smallformat {font-family: inherit; font-size: smaller}
pre.smalllisp {font-size: smaller}
span.nolinebreak {white-space: nowrap}
span.roman {font-family: initial; font-weight: normal}
span.sansserif {font-family: sans-serif; font-weight: normal}
ul.no-bullet {list-style: none}
body {
margin-left: 5%;
margin-right: 5%;
}
h1 {
background: white;
color: rgb(25%, 25%, 25%);
font-family: monospace;
font-size: xx-large;
text-align: center;
}
h2 {
background: white;
color: rgb(40%, 40%, 40%);
font-family: monospace;
font-size: x-large;
text-align: center;
}
h3 {
background: white;
color: rgb(40%, 40%, 40%);
font-family: monospace;
font-size: large;
}
h4 {
background: white;
color: rgb(40%, 40%, 40%);
font-family: monospace;
}
span.samp {
font-family: monospace;
}
span.command {
font-family: monospace;
}
span.option {
font-family: monospace;
}
span.file {
font-family: monospace;
}
span.env {
font-family: monospace;
}
ul {
margin-top: 0.25ex;
margin-bottom: 0.25ex;
}
li {
margin-top: 0.25ex;
margin-bottom: 0.25ex;
}
p {
margin-top: 0.6ex;
margin-bottom: 1.2ex;
}
-->
</style>
</head>
<body lang="en">
<h1 class="settitle" align="center">R Language Definition</h1>
<a name="SEC_Contents"></a>
<h2 class="contents-heading">Table of Contents</h2>
<div class="contents">
<ul class="no-bullet">
<li><a name="toc-Introduction-1" href="#Introduction">1 Introduction</a></li>
<li><a name="toc-Objects-1" href="#Objects">2 Objects</a>
<ul class="no-bullet">
<li><a name="toc-Basic-types-1" href="#Basic-types">2.1 Basic types</a>
<ul class="no-bullet">
<li><a name="toc-Vectors" href="#Vector-objects">2.1.1 Vectors</a></li>
<li><a name="toc-Lists" href="#List-objects">2.1.2 Lists</a></li>
<li><a name="toc-Language-objects-1" href="#Language-objects">2.1.3 Language objects</a>
<ul class="no-bullet">
<li><a name="toc-Symbol-objects-1" href="#Symbol-objects">2.1.3.1 Symbol objects</a></li>
</ul></li>
<li><a name="toc-Expression-objects-1" href="#Expression-objects">2.1.4 Expression objects</a></li>
<li><a name="toc-Function-objects-1" href="#Function-objects">2.1.5 Function objects</a></li>
<li><a name="toc-NULL" href="#NULL-object">2.1.6 NULL</a></li>
<li><a name="toc-Builtin-objects-and-special-forms-1" href="#Builtin-objects-and-special-forms">2.1.7 Builtin objects and special forms</a></li>
<li><a name="toc-Promise-objects-1" href="#Promise-objects">2.1.8 Promise objects</a></li>
<li><a name="toc-Dot_002ddot_002ddot-1" href="#Dot_002ddot_002ddot">2.1.9 Dot-dot-dot</a></li>
<li><a name="toc-Environments" href="#Environment-objects">2.1.10 Environments</a></li>
<li><a name="toc-Pairlist-objects-1" href="#Pairlist-objects">2.1.11 Pairlist objects</a></li>
<li><a name="toc-The-_0060_0060Any_0027_0027-type" href="#Any_002dtype">2.1.12 The &ldquo;Any&rdquo; type</a></li>
</ul></li>
<li><a name="toc-Attributes-1" href="#Attributes">2.2 Attributes</a>
<ul class="no-bullet">
<li><a name="toc-Names-1" href="#Names">2.2.1 Names</a></li>
<li><a name="toc-Dimensions-1" href="#Dimensions">2.2.2 Dimensions</a></li>
<li><a name="toc-Dimnames-1" href="#Dimnames">2.2.3 Dimnames</a></li>
<li><a name="toc-Classes-1" href="#Classes">2.2.4 Classes</a></li>
<li><a name="toc-Time-series-attributes-1" href="#Time-series-attributes">2.2.5 Time series attributes</a></li>
<li><a name="toc-Copying-of-attributes-1" href="#Copying-of-attributes">2.2.6 Copying of attributes</a></li>
</ul></li>
<li><a name="toc-Special-compound-objects-1" href="#Special-compound-objects">2.3 Special compound objects</a>
<ul class="no-bullet">
<li><a name="toc-Factors-1" href="#Factors">2.3.1 Factors</a></li>
<li><a name="toc-Data-frame-objects-1" href="#Data-frame-objects">2.3.2 Data frame objects</a></li>
</ul></li>
</ul></li>
<li><a name="toc-Evaluation-of-expressions-1" href="#Evaluation-of-expressions">3 Evaluation of expressions</a>
<ul class="no-bullet">
<li><a name="toc-Simple-evaluation-1" href="#Simple-evaluation">3.1 Simple evaluation</a>
<ul class="no-bullet">
<li><a name="toc-Constants-1" href="#Constants">3.1.1 Constants</a></li>
<li><a name="toc-Symbol-lookup-1" href="#Symbol-lookup">3.1.2 Symbol lookup</a></li>
<li><a name="toc-Function-calls-1" href="#Function-calls">3.1.3 Function calls</a></li>
<li><a name="toc-Operators-1" href="#Operators">3.1.4 Operators</a></li>
</ul></li>
<li><a name="toc-Control-structures-1" href="#Control-structures">3.2 Control structures</a>
<ul class="no-bullet">
<li><a name="toc-if-1" href="#if">3.2.1 if</a></li>
<li><a name="toc-Looping-1" href="#Looping">3.2.2 Looping</a></li>
<li><a name="toc-repeat-1" href="#repeat">3.2.3 repeat</a></li>
<li><a name="toc-while-1" href="#while">3.2.4 while</a></li>
<li><a name="toc-for-1" href="#for">3.2.5 for</a></li>
<li><a name="toc-switch-1" href="#switch">3.2.6 switch</a></li>
</ul></li>
<li><a name="toc-Elementary-arithmetic-operations-1" href="#Elementary-arithmetic-operations">3.3 Elementary arithmetic operations</a>
<ul class="no-bullet">
<li><a name="toc-Recycling-rules-1" href="#Recycling-rules">3.3.1 Recycling rules</a></li>
<li><a name="toc-Propagation-of-names-1" href="#Propagation-of-names">3.3.2 Propagation of names</a></li>
<li><a name="toc-Dimensional-attributes-1" href="#Dimensional-attributes">3.3.3 Dimensional attributes</a></li>
<li><a name="toc-NA-handling-1" href="#NA-handling">3.3.4 NA handling</a></li>
</ul></li>
<li><a name="toc-Indexing-1" href="#Indexing">3.4 Indexing</a>
<ul class="no-bullet">
<li><a name="toc-Indexing-by-vectors-1" href="#Indexing-by-vectors">3.4.1 Indexing by vectors</a></li>
<li><a name="toc-Indexing-matrices-and-arrays-1" href="#Indexing-matrices-and-arrays">3.4.2 Indexing matrices and arrays</a></li>
<li><a name="toc-Indexing-other-structures-1" href="#Indexing-other-structures">3.4.3 Indexing other structures</a></li>
<li><a name="toc-Subset-assignment-1" href="#Subset-assignment">3.4.4 Subset assignment</a></li>
</ul></li>
<li><a name="toc-Scope-of-variables-1" href="#Scope-of-variables">3.5 Scope of variables</a>
<ul class="no-bullet">
<li><a name="toc-Global-environment-1" href="#Global-environment">3.5.1 Global environment</a></li>
<li><a name="toc-Lexical-environment-1" href="#Lexical-environment">3.5.2 Lexical environment</a></li>
<li><a name="toc-The-call-stack" href="#Stacks">3.5.3 The call stack</a></li>
<li><a name="toc-Search-path-1" href="#Search-path">3.5.4 Search path</a></li>
</ul></li>
</ul></li>
<li><a name="toc-Functions-1" href="#Functions">4 Functions</a>
<ul class="no-bullet">
<li><a name="toc-Writing-functions-1" href="#Writing-functions">4.1 Writing functions</a>
<ul class="no-bullet">
<li><a name="toc-Syntax-and-examples-1" href="#Syntax-and-examples">4.1.1 Syntax and examples</a></li>
<li><a name="toc-Arguments-1" href="#Arguments">4.1.2 Arguments</a></li>
</ul></li>
<li><a name="toc-Functions-as-objects-1" href="#Functions-as-objects">4.2 Functions as objects</a></li>
<li><a name="toc-Evaluation-1" href="#Evaluation">4.3 Evaluation</a>
<ul class="no-bullet">
<li><a name="toc-Evaluation-environment-1" href="#Evaluation-environment">4.3.1 Evaluation environment</a></li>
<li><a name="toc-Argument-matching-1" href="#Argument-matching">4.3.2 Argument matching</a></li>
<li><a name="toc-Argument-evaluation-1" href="#Argument-evaluation">4.3.3 Argument evaluation</a></li>
<li><a name="toc-Scope-1" href="#Scope">4.3.4 Scope</a></li>
</ul></li>
</ul></li>
<li><a name="toc-Object_002doriented-programming-1" href="#Object_002doriented-programming">5 Object-oriented programming</a>
<ul class="no-bullet">
<li><a name="toc-Definition-1" href="#Definition">5.1 Definition</a></li>
<li><a name="toc-Inheritance-1" href="#Inheritance">5.2 Inheritance</a></li>
<li><a name="toc-Method-dispatching-1" href="#Method-dispatching">5.3 Method dispatching</a></li>
<li><a name="toc-UseMethod-1" href="#UseMethod">5.4 UseMethod</a></li>
<li><a name="toc-NextMethod-1" href="#NextMethod">5.5 NextMethod</a></li>
<li><a name="toc-Group-methods-1" href="#Group-methods">5.6 Group methods</a></li>
<li><a name="toc-Writing-methods-1" href="#Writing-methods">5.7 Writing methods</a></li>
</ul></li>
<li><a name="toc-Computing-on-the-language-1" href="#Computing-on-the-language">6 Computing on the language</a>
<ul class="no-bullet">
<li><a name="toc-Direct-manipulation-of-language-objects-1" href="#Direct-manipulation-of-language-objects">6.1 Direct manipulation of language objects</a></li>
<li><a name="toc-Substitutions-1" href="#Substitutions">6.2 Substitutions</a></li>
<li><a name="toc-More-on-evaluation-1" href="#More-on-evaluation">6.3 More on evaluation</a></li>
<li><a name="toc-Evaluation-of-expression-objects-1" href="#Evaluation-of-expression-objects">6.4 Evaluation of expression objects</a></li>
<li><a name="toc-Manipulation-of-function-calls-1" href="#Manipulation-of-function-calls">6.5 Manipulation of function calls</a></li>
<li><a name="toc-Manipulation-of-functions-1" href="#Manipulation-of-functions">6.6 Manipulation of functions</a></li>
</ul></li>
<li><a name="toc-System-and-foreign-language-interfaces-1" href="#System-and-foreign-language-interfaces">7 System and foreign language interfaces</a>
<ul class="no-bullet">
<li><a name="toc-Operating-system-access-1" href="#Operating-system-access">7.1 Operating system access</a></li>
<li><a name="toc-Foreign-language-interfaces-1" href="#Foreign-language-interfaces">7.2 Foreign language interfaces</a></li>
<li><a name="toc-_002eInternal-and-_002ePrimitive-1" href="#g_t_002eInternal-and-_002ePrimitive">7.3 .Internal and .Primitive</a></li>
</ul></li>
<li><a name="toc-Exception-handling-1" href="#Exception-handling">8 Exception handling</a>
<ul class="no-bullet">
<li><a name="toc-stop-1" href="#stop">8.1 stop</a></li>
<li><a name="toc-warning-1" href="#warning">8.2 warning</a></li>
<li><a name="toc-on_002eexit-1" href="#on_002eexit">8.3 on.exit</a></li>
<li><a name="toc-Error-options-1" href="#Error-options">8.4 Error options</a></li>
</ul></li>
<li><a name="toc-Debugging-1" href="#Debugging">9 Debugging</a>
<ul class="no-bullet">
<li><a name="toc-browser-1" href="#browser">9.1 browser</a></li>
<li><a name="toc-debug_002fundebug-1" href="#debug_002fundebug">9.2 debug/undebug</a></li>
<li><a name="toc-trace_002funtrace-1" href="#trace_002funtrace">9.3 trace/untrace</a></li>
<li><a name="toc-traceback-1" href="#traceback">9.4 traceback</a></li>
</ul></li>
<li><a name="toc-Parser-1" href="#Parser">10 Parser</a>
<ul class="no-bullet">
<li><a name="toc-The-parsing-process-1" href="#The-parsing-process">10.1 The parsing process</a>
<ul class="no-bullet">
<li><a name="toc-Modes-of-parsing-1" href="#Modes-of-parsing">10.1.1 Modes of parsing</a></li>
<li><a name="toc-Internal-representation-1" href="#Internal-representation">10.1.2 Internal representation</a></li>
<li><a name="toc-Deparsing-1" href="#Deparsing">10.1.3 Deparsing</a></li>
</ul></li>
<li><a name="toc-Comments-1" href="#Comments">10.2 Comments</a></li>
<li><a name="toc-Tokens-1" href="#Tokens">10.3 Tokens</a>
<ul class="no-bullet">
<li><a name="toc-Constants-2" href="#Literal-constants">10.3.1 Constants</a></li>
<li><a name="toc-Identifiers-1" href="#Identifiers">10.3.2 Identifiers</a></li>
<li><a name="toc-Reserved-words-1" href="#Reserved-words">10.3.3 Reserved words</a></li>
<li><a name="toc-Special-operators-1" href="#Special-operators">10.3.4 Special operators</a></li>
<li><a name="toc-Separators-1" href="#Separators">10.3.5 Separators</a></li>
<li><a name="toc-Operator-tokens-1" href="#Operator-tokens">10.3.6 Operator tokens</a></li>
<li><a name="toc-Grouping-1" href="#Grouping">10.3.7 Grouping</a></li>
<li><a name="toc-Indexing-tokens-1" href="#Indexing-tokens">10.3.8 Indexing tokens</a></li>
</ul></li>
<li><a name="toc-Expressions-1" href="#Expressions">10.4 Expressions</a>
<ul class="no-bullet">
<li><a name="toc-Function-calls-2" href="#Function-calls-_0028expressions_0029">10.4.1 Function calls</a></li>
<li><a name="toc-Infix-and-prefix-operators-1" href="#Infix-and-prefix-operators">10.4.2 Infix and prefix operators</a></li>
<li><a name="toc-Index-constructions-1" href="#Index-constructions">10.4.3 Index constructions</a></li>
<li><a name="toc-Compound-expressions-1" href="#Compound-expressions">10.4.4 Compound expressions</a></li>
<li><a name="toc-Flow-control-elements-1" href="#Flow-control-elements">10.4.5 Flow control elements</a></li>
<li><a name="toc-Function-definitions-1" href="#Function-definitions">10.4.6 Function definitions</a></li>
</ul></li>
<li><a name="toc-Directives-1" href="#Directives">10.5 Directives</a></li>
</ul></li>
<li><a name="toc-Function-and-Variable-Index-1" href="#Function-and-Variable-Index">Function and Variable Index</a></li>
<li><a name="toc-Concept-Index-1" href="#Concept-Index">Concept Index</a></li>
<li><a name="toc-References-1" href="#References">Appendix A References</a></li>
</ul>
</div>
<a name="Top"></a>
<div class="header">
<p>
Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="R-Language-Definition"></a>
<h1 class="top">R Language Definition</h1>
<p>This is an introduction to the R language, explaining evaluation,
parsing, object oriented programming, computing on the language, and so
forth.
</p>
<p>This manual is for R, version 3.3.1 (2016-06-21).
</p>
<p>Copyright &copy; 2000&ndash;2016 R Core Team
</p>
<blockquote>
<p>Permission is granted to make and distribute verbatim copies of this
manual provided the copyright notice and this permission notice are
preserved on all copies.
</p>
<p>Permission is granted to copy and distribute modified versions of this
manual under the conditions for verbatim copying, provided that the
entire resulting derived work is distributed under the terms of a
permission notice identical to this one.
</p>
<p>Permission is granted to copy and distribute translations of this manual
into another language, under the above conditions for modified versions,
except that this permission notice may be stated in a translation
approved by the R Core Team.
</p></blockquote>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Introduction" accesskey="1">Introduction</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Objects" accesskey="2">Objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Evaluation-of-expressions" accesskey="3">Evaluation of expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Functions" accesskey="4">Functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Object_002doriented-programming" accesskey="5">Object-oriented programming</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Computing-on-the-language" accesskey="6">Computing on the language</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#System-and-foreign-language-interfaces" accesskey="7">System and foreign language interfaces</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Exception-handling" accesskey="8">Exception handling</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Debugging" accesskey="9">Debugging</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Parser">Parser</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Function-and-Variable-Index">Function and Variable Index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Concept-Index">Concept Index</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#References">References</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Introduction"></a>
<div class="header">
<p>
Next: <a href="#Objects" accesskey="n" rel="next">Objects</a>, Previous: <a href="#Top" accesskey="p" rel="prev">Top</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Introduction-1"></a>
<h2 class="chapter">1 Introduction</h2>
<p>R is a system for statistical computation and graphics. It
provides, among other things, a programming language, high level
graphics, interfaces to other languages and debugging facilities. This
manual details and defines the R language.
</p>
<p>The R language is a dialect of S which was designed in the 1980s
and has been in widespread use in the statistical community since.
Its principal designer, John M. Chambers, was awarded the 1998 ACM
Software Systems Award for S.
</p>
<p>The language syntax has a superficial similarity with C, but the
semantics are of the FPL (functional programming language) variety with
stronger affinities with Lisp and <acronym>APL</acronym>. In particular, it
allows &ldquo;computing on the language&rdquo;, which in turn makes it possible to
write functions that take expressions as input, something that is often
useful for statistical modeling and graphics.
</p>
<p>It is possible to get quite far using R interactively, executing
<a name="index-expression"></a>
simple expressions from the command line. Some users may never need to
go beyond that level, others will want to write their own functions
either in an ad hoc fashion to systematize repetitive work or with the
perspective of writing add-on packages for new functionality.
</p>
<p>The purpose of this manual is to document the language <em>per se</em>.
That is, the objects that it works on, and the details of the expression
evaluation process, which are useful to know when programming R
functions. Major subsystems for specific tasks, such as graphics, are
only briefly described in this manual and will be documented separately.
</p>
<p>Although much of the text will equally apply to S, there are also
some substantial differences, and in order not to confuse the issue we
shall concentrate on describing R.
</p>
<p>The design of the language contains a number of fine points and
common pitfalls which may surprise the user. Most of these are due to
consistency considerations at a deeper level, as we shall explain.
There are also a number of useful shortcuts and idioms, which allow the
user to express quite complicated operations succinctly. Many of these
become natural once one is familiar with the underlying concepts. In
some cases, there are multiple ways of performing a task, but some of
the techniques will rely on the language implementation, and others work
at a higher level of abstraction. In such cases we shall indicate the
preferred usage.
</p>
<p>Some familiarity with R is assumed. This is not an introduction to
R but rather a programmers&rsquo; reference manual. Other manuals provide
complementary information: in particular <a href="./R-intro.html#Preface">Preface</a> in <cite>An
Introduction to R</cite> provides an introduction to R and <a href="./R-exts.html#System-and-foreign-language-interfaces">System and
foreign language interfaces</a> in <cite>Writing R Extensions</cite> details
how to extend R using compiled code.
</p>
<hr>
<a name="Objects"></a>
<div class="header">
<p>
Next: <a href="#Evaluation-of-expressions" accesskey="n" rel="next">Evaluation of expressions</a>, Previous: <a href="#Introduction" accesskey="p" rel="prev">Introduction</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Objects-1"></a>
<h2 class="chapter">2 Objects</h2>
<p>In every computer language
<a name="index-variable"></a>
variables provide a means of accessing the data stored in memory. R
does not provide direct access to the computer&rsquo;s memory but rather
provides a number of specialized data structures we will refer to as
<a name="index-object"></a>
objects. These objects
are referred to through symbols or variables. In R, however, the
symbols are themselves objects and can be manipulated in the same way as
any other object. This is different from many other languages and has
wide ranging effects.
</p>
<p>In this chapter we provide preliminary descriptions of the various data
structures provided in R. More detailed discussions of many of them
will be found in the subsequent chapters. The R specific function
<code>typeof</code>
<a name="index-typeof"></a>
<a name="index-type"></a>
returns the <em>type</em> of an R object. Note that in the C code
underlying R, all objects are pointers to a structure with typedef
<code>SEXPREC</code>; the different R data types are represented in C by
<code>SEXPTYPE</code>, which determines how the information in the various
parts of the structure is used.
</p>
<p>The following table describes the possible values returned by
<code>typeof</code> and what they are.
</p>
<blockquote>
<table summary="">
<tr><td width="20%"><code>&quot;NULL&quot;</code></td><td width="70%">NULL</td></tr>
<tr><td width="20%"><code>&quot;symbol&quot;</code></td><td width="70%">a variable name</td></tr>
<tr><td width="20%"><code>&quot;pairlist&quot;</code></td><td width="70%">a pairlist object (mainly internal)</td></tr>
<tr><td width="20%"><code>&quot;closure&quot;</code></td><td width="70%">a function</td></tr>
<tr><td width="20%"><code>&quot;environment&quot;</code></td><td width="70%">an environment
<a name="index-evaluation_002c-lazy"></a></td></tr>
<tr><td width="20%"><code>&quot;promise&quot;</code></td><td width="70%">an object used to implement lazy evaluation</td></tr>
<tr><td width="20%"><code>&quot;language&quot;</code></td><td width="70%">an R language construct</td></tr>
<tr><td width="20%"><code>&quot;special&quot;</code></td><td width="70%">an internal function that does not evaluate its arguments</td></tr>
<tr><td width="20%"><code>&quot;builtin&quot;</code></td><td width="70%">an internal function that evaluates its arguments</td></tr>
<tr><td width="20%"><code>&quot;char&quot;</code></td><td width="70%">a &lsquo;scalar&rsquo; string object (internal only) ***</td></tr>
<tr><td width="20%"><code>&quot;logical&quot;</code></td><td width="70%">a vector containing logical values</td></tr>
<tr><td width="20%"><code>&quot;integer&quot;</code></td><td width="70%">a vector containing integer values</td></tr>
<tr><td width="20%"><code>&quot;double&quot;</code></td><td width="70%">a vector containing real values</td></tr>
<tr><td width="20%"><code>&quot;complex&quot;</code></td><td width="70%">a vector containing complex values</td></tr>
<tr><td width="20%"><code>&quot;character&quot;</code></td><td width="70%">a vector containing character values</td></tr>
<tr><td width="20%"><code>&quot;...&quot;</code></td><td width="70%">the special variable length argument ***</td></tr>
<tr><td width="20%"><code>&quot;any&quot;</code></td><td width="70%">a special type that matches all types: there are no objects of this type</td></tr>
<tr><td width="20%"><code>&quot;expression&quot;</code></td><td width="70%">an expression object</td></tr>
<tr><td width="20%"><code>&quot;list&quot;</code></td><td width="70%">a list</td></tr>
<tr><td width="20%"><code>&quot;bytecode&quot;</code></td><td width="70%">byte code (internal only) ***</td></tr>
<tr><td width="20%"><code>&quot;externalptr&quot;</code></td><td width="70%">an external pointer object</td></tr>
<tr><td width="20%"><code>&quot;weakref&quot;</code></td><td width="70%">a weak reference object</td></tr>
<tr><td width="20%"><code>&quot;raw&quot;</code></td><td width="70%">a vector containing bytes</td></tr>
<tr><td width="20%"><code>&quot;S4&quot;</code></td><td width="70%">an S4 object which is not a simple object</td></tr>
</table>
</blockquote>
<p>Users cannot easily get hold of objects of types marked with a &lsquo;***&rsquo;.
</p>
<a name="index-mode-3"></a>
<a name="index-mode"></a>
<p>Function <code>mode</code> gives information about the <em>mode</em> of an object
in the sense of Becker, Chambers &amp; Wilks (1988), and is more compatible
with other implementations of the S language.
<a name="index-storage_002emode"></a>
Finally, the function <code>storage.mode</code> returns the <em>storage mode</em>
of its argument in the sense of Becker et al. (1988). It is generally
used when calling functions written in another language, such as C or
FORTRAN, to ensure that R objects have the data type expected by the
routine being called. (In the S language, vectors with integer or
real values are both of mode <code>&quot;numeric&quot;</code>, so their storage modes
need to be distinguished.)
</p>
<div class="example">
<pre class="example">&gt; x &lt;- 1:3
&gt; typeof(x)
[1] &quot;integer&quot;
&gt; mode(x)
[1] &quot;numeric&quot;
&gt; storage.mode(x)
[1] &quot;integer&quot;
</pre></div>
<p>R
<a name="index-object-1"></a>
objects are often coerced to different
<a name="index-type-1"></a>
types during computations.
There are also many functions available to perform explicit
<a name="index-coercion"></a>
coercion.
When programming in the R language the type of an object generally
doesn&rsquo;t affect the computations, however, when dealing with foreign
languages or the operating system it is often necessary to ensure that
an object is of the correct type.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Basic-types" accesskey="1">Basic types</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Attributes" accesskey="2">Attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Special-compound-objects" accesskey="3">Special compound objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Basic-types"></a>
<div class="header">
<p>
Next: <a href="#Attributes" accesskey="n" rel="next">Attributes</a>, Previous: <a href="#Objects" accesskey="p" rel="prev">Objects</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="index-type-2"></a>
<a name="Basic-types-1"></a>
<h3 class="section">2.1 Basic types</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Vector-objects" accesskey="1">Vector objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#List-objects" accesskey="2">List objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Language-objects" accesskey="3">Language objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Expression-objects" accesskey="4">Expression objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Function-objects" accesskey="5">Function objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#NULL-object" accesskey="6">NULL object</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Builtin-objects-and-special-forms" accesskey="7">Builtin objects and special forms</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Promise-objects" accesskey="8">Promise objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dot_002ddot_002ddot" accesskey="9">Dot-dot-dot</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Environment-objects">Environment objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Pairlist-objects">Pairlist objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Any_002dtype">Any-type</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Vector-objects"></a>
<div class="header">
<p>
Next: <a href="#List-objects" accesskey="n" rel="next">List objects</a>, Previous: <a href="#Basic-types" accesskey="p" rel="prev">Basic types</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Vectors"></a>
<h4 class="subsection">2.1.1 Vectors</h4>
<a name="index-vector"></a>
<p>Vectors can be thought of as contiguous cells containing data. Cells
are accessed through
<a name="index-index"></a>
indexing operations such as
<code>x[5]</code>. More details are given in <a href="#Indexing">Indexing</a>.
</p>
<a name="index-type-3"></a>
<a name="index-mode-1"></a>
<a name="index-atomic"></a>
<p>R has six basic (&lsquo;atomic&rsquo;) vector types: logical, integer, real,
complex, string (or character) and raw. The modes and storage modes for
the different vector types are listed in the following table.
</p>
<blockquote>
<table summary="">
<thead><tr><th>typeof</th><th>mode</th><th>storage.mode</th></tr></thead>
<tr><td><code>logical</code></td><td><code>logical</code></td><td><code>logical</code></td></tr>
<tr><td><code>integer</code></td><td><code>numeric</code></td><td><code>integer</code></td></tr>
<tr><td><code>double</code></td><td><code>numeric</code></td><td><code>double</code></td></tr>
<tr><td><code>complex</code></td><td><code>complex</code></td><td><code>complex</code></td></tr>
<tr><td><code>character</code></td><td><code>character</code></td><td><code>character</code></td></tr>
<tr><td><code>raw</code></td><td><code>raw</code></td><td><code>raw</code></td></tr>
</table>
</blockquote>
<p>Single numbers, such as <code>4.2</code>, and strings, such as <code>&quot;four
point two&quot;</code> are still vectors, of length 1; there are no more basic
types. Vectors with length zero are possible (and useful).
</p>
<p>String vectors have mode and storage mode <code>&quot;character&quot;</code>. A single
element of a character vector is often referred to as a <em>character
string</em>.
</p>
<hr>
<a name="List-objects"></a>
<div class="header">
<p>
Next: <a href="#Language-objects" accesskey="n" rel="next">Language objects</a>, Previous: <a href="#Vector-objects" accesskey="p" rel="prev">Vector objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Lists"></a>
<h4 class="subsection">2.1.2 Lists</h4>
<p>Lists (&ldquo;generic vectors&rdquo;) are another kind of data storage. Lists
have elements, each of which can contain any type of R object, i.e.
the elements of a list do not have to be of the same type. List
elements are accessed through three different
<a name="index-index-1"></a>
indexing operations.
These are explained in detail in <a href="#Indexing">Indexing</a>.
</p>
<p>Lists are vectors, and the basic vector types are referred to as
<em>atomic vectors</em> where it is necessary to exclude lists.
</p>
<hr>
<a name="Language-objects"></a>
<div class="header">
<p>
Next: <a href="#Expression-objects" accesskey="n" rel="next">Expression objects</a>, Previous: <a href="#List-objects" accesskey="p" rel="prev">List objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Language-objects-1"></a>
<h4 class="subsection">2.1.3 Language objects</h4>
<p>There are three types of objects that constitute the R language.
They are <em>calls</em>, <em>expressions</em>, and <em>names</em>.
<a name="index-call"></a>
<a name="index-expression-1"></a>
<a name="index-name"></a>
Since R has objects of type <code>&quot;expression&quot;</code> we will try to avoid
the use of the word expression in other contexts. In particular
syntactically correct expressions will be referred to as
<em>statements</em>.
<a name="index-statement"></a>
</p>
<p>These objects have modes <code>&quot;call&quot;</code>, <code>&quot;expression&quot;</code>, and
<code>&quot;name&quot;</code>, respectively.
</p>
<p>They can be created directly from expressions using the <code>quote</code>
mechanism and converted to and from lists by the <code>as.list</code> and
<code>as.call</code> functions.
<a name="index-quote"></a>
<a name="index-as_002elist"></a>
<a name="index-as_002ecall"></a>
Components of the
<a name="index-parsing"></a>
parse tree can be extracted using the standard
indexing operations.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Symbol-objects" accesskey="1">Symbol objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Symbol-objects"></a>
<div class="header">
<p>
Previous: <a href="#Language-objects" accesskey="p" rel="prev">Language objects</a>, Up: <a href="#Language-objects" accesskey="u" rel="up">Language objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Symbol-objects-1"></a>
<h4 class="subsubsection">2.1.3.1 Symbol objects</h4>
<a name="index-symbol"></a>
<p>Symbols refer to R
<a name="index-object-2"></a>
objects. The
<a name="index-name-1"></a>
name of any R object is usually a
symbol. Symbols can be created through the functions <code>as.name</code> and
<code>quote</code>.
</p>
<a name="index-symbol-1"></a>
<a name="index-mode-2"></a>
<p>Symbols have mode <code>&quot;name&quot;</code>, storage mode <code>&quot;symbol&quot;</code>, and type
<code>&quot;symbol&quot;</code>. They can be
<a name="index-coercion-1"></a>
coerced to and from character strings
using <code>as.character</code> and <code>as.name</code>.
<a name="index-as_002echaracter"></a>
<a name="index-as_002ename"></a>
<a name="index-parsing-1"></a>
They naturally appear as atoms of parsed expressions, try e.g.
<code>as.list(quote(x + y))</code>.
</p>
<hr>
<a name="Expression-objects"></a>
<div class="header">
<p>
Next: <a href="#Function-objects" accesskey="n" rel="next">Function objects</a>, Previous: <a href="#Language-objects" accesskey="p" rel="prev">Language objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Expression-objects-1"></a>
<h4 class="subsection">2.1.4 Expression objects</h4>
<p>In R one can have objects of type <code>&quot;expression&quot;</code>. An
<em>expression</em> contains one or more statements. A statement is a
syntactically correct collection of
<a name="index-token"></a>
tokens.
<a name="index-expression-object"></a>
Expression objects are special language objects which contain parsed but
unevaluated R statements. The main difference is that an expression
object can contain several such expressions. Another more subtle
difference is that objects of type <code>&quot;expression&quot;</code> are only
<a name="index-evaluation_002c-expression"></a>
evaluated when
explicitly passed to <code>eval</code>, whereas other language objects may get
evaluated in some unexpected cases.
</p>
<p>An
<a name="index-expression-object-1"></a>
expression object behaves much like a list and its components should
be accessed in the same way as the components of a list.
</p>
<hr>
<a name="Function-objects"></a>
<div class="header">
<p>
Next: <a href="#NULL-object" accesskey="n" rel="next">NULL object</a>, Previous: <a href="#Expression-objects" accesskey="p" rel="prev">Expression objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-objects-1"></a>
<h4 class="subsection">2.1.5 Function objects</h4>
<a name="index-function"></a>
<p>In R functions are objects and can be manipulated in much the same
way as any other object. Functions (or more precisely, function
closures) have three basic components: a formal argument list, a body
and an
<a name="index-environment"></a>
environment. The argument list is a comma-separated list of
arguments. An
<a name="index-argument"></a>
argument can be a symbol, or a &lsquo;<samp><var>symbol</var> =
<var>default</var></samp>&rsquo; construct, or the special argument &lsquo;<samp>...</samp>&rsquo;. The
second form of argument is used to specify a default value for an
argument. This value will be used if the function is called without any
value specified for that argument. The &lsquo;<samp>...</samp>&rsquo; argument is special
and can contain any number of arguments. It is generally used if the
number of arguments is unknown or in cases where the arguments will be
passed on to another function.
</p>
<p>The body is a parsed R statement. It is usually a collection of
statements in braces but it can be a single statement, a symbol or even
a constant.
</p>
<p>A function&rsquo;s
<a name="index-function-1"></a>
<a name="index-environment-1"></a>
environment is the environment that was active at the time
that the function was created. Any symbols bound in that environment
are <em>captured</em> and available to the function. This combination of
the code of the function and the bindings in its environment is called a
&lsquo;function closure&rsquo;, a term from functional programming theory. In this
document we generally use the term &lsquo;function&rsquo;, but use &lsquo;closure&rsquo; to
emphasize the importance of the attached environment.
</p>
<p>It is possible to extract and manipulate the three parts of a closure
object using <code>formals</code>, <code>body</code>, and <code>environment</code>
constructs (all three can also be used on the left hand side of
<a name="index-assignment"></a>
assignments).
<a name="index-formals"></a>
<a name="index-body"></a>
<a name="index-environment-20"></a>
The last of these can be used to remove unwanted environment capture.
</p>
<p>When a function is called, a new environment (called the
<em>evaluation environment</em>) is created, whose enclosure (see
<a href="#Environment-objects">Environment objects</a>) is the environment from the function closure.
This new environment is initially populated with the unevaluated
arguments to the function; as evaluation proceeds, local variables are
created within it.
</p>
<a name="index-function-2"></a>
<p>There is also a facility for converting functions to and from list
structures using <code>as.list</code> and <code>as.function</code>.
<a name="index-as_002efunction"></a>
These have been included to provide compatibility with S and their
use is discouraged.
</p>
<hr>
<a name="NULL-object"></a>
<div class="header">
<p>
Next: <a href="#Builtin-objects-and-special-forms" accesskey="n" rel="next">Builtin objects and special forms</a>, Previous: <a href="#Function-objects" accesskey="p" rel="prev">Function objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="NULL"></a>
<h4 class="subsection">2.1.6 NULL</h4>
<p>There is a special object called <code>NULL</code>. It is used whenever there
is a need to indicate or specify that an object is absent. It should not be
confused with a vector or list of zero length.
<a name="index-NULL"></a>
</p>
<p>The <code>NULL</code> object has no type and no modifiable properties. There
is only one <code>NULL</code> object in R, to which all instances refer. To
test for <code>NULL</code> use <code>is.null</code>. You cannot set attributes on
<code>NULL</code>.
</p>
<hr>
<a name="Builtin-objects-and-special-forms"></a>
<div class="header">
<p>
Next: <a href="#Promise-objects" accesskey="n" rel="next">Promise objects</a>, Previous: <a href="#NULL-object" accesskey="p" rel="prev">NULL object</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Builtin-objects-and-special-forms-1"></a>
<h4 class="subsection">2.1.7 Builtin objects and special forms</h4>
<p>These two kinds of object contain the builtin
<a name="index-function-3"></a>
<a name="index-_002ePrimitive"></a>
<a name="index-_002eInternal"></a>
functions of R, i.e., those that are displayed as <code>.Primitive</code>
in code listings (as well as those accessed via the <code>.Internal</code>
function and hence not user-visible as objects). The difference between
the two lies in the argument handling. Builtin functions have all
their arguments evaluated and passed to the internal function, in
accordance with <em>call-by-value</em>, whereas special functions pass the
unevaluated arguments to the internal function.
</p>
<p>From the R language, these objects are just another kind of function.
The <code>is.primitive</code> function can distinguish them from interpreted
<a name="index-function-4"></a>
functions.
</p>
<hr>
<a name="Promise-objects"></a>
<div class="header">
<p>
Next: <a href="#Dot_002ddot_002ddot" accesskey="n" rel="next">Dot-dot-dot</a>, Previous: <a href="#Builtin-objects-and-special-forms" accesskey="p" rel="prev">Builtin objects and special forms</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Promise-objects-1"></a>
<h4 class="subsection">2.1.8 Promise objects</h4>
<a name="index-promise"></a>
<p>Promise objects are part of R&rsquo;s lazy evaluation mechanism. They
contain three slots: a value, an expression, and an
<a name="index-environment-2"></a>
environment. When a
<a name="index-function-5"></a>
<a name="index-function-argument"></a>
function is called the arguments are matched and then each of the formal
arguments is bound to a promise. The expression that was given for that
formal argument and a pointer to the environment the function was called
from are stored in the promise.
</p>
<p>Until that argument is accessed there is no <em>value</em> associated with
the promise. When the argument is accessed, the stored expression is
<a name="index-evaluation_002c-expression-1"></a>
evaluated in the stored environment, and the result is returned. The
result is also saved by
the promise. The <code>substitute</code> function will extract the content
of the expression slot. This allows the programmer to
access either the value or the expression associated with the promise.
</p>
<p>Within the R language, promise objects are almost only seen
implicitly: actual function arguments are of this type. There is also a
<code>delayedAssign</code> function that will make a promise out of an
expression. There is generally no way in R code to check whether an
object is a promise or not, nor is there a way to use R code to
determine the environment of a promise.
</p>
<hr>
<a name="Dot_002ddot_002ddot"></a>
<div class="header">
<p>
Next: <a href="#Environment-objects" accesskey="n" rel="next">Environment objects</a>, Previous: <a href="#Promise-objects" accesskey="p" rel="prev">Promise objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dot_002ddot_002ddot-1"></a>
<h4 class="subsection">2.1.9 Dot-dot-dot</h4>
<p>The &lsquo;<samp>...</samp>&rsquo; object type is stored as a type of pairlist. The
components of &lsquo;<samp>...</samp>&rsquo; can be accessed in the usual pairlist manner
from C code, but is not easily accessed as an object in interpreted
code. The object can be captured as a list, so for example in
<code>table</code> one sees
</p>
<div class="example">
<pre class="example"> args &lt;- list(...)
## ....
for (a in args) {
## ....
</pre></div>
<a name="index-function-6"></a>
<a name="index-function-argument-1"></a>
<p>If a function has &lsquo;<samp>...</samp>&rsquo; as a formal argument then any actual
arguments that do not match a formal argument are matched with
&lsquo;<samp>...</samp>&rsquo;.
</p>
<hr>
<a name="Environment-objects"></a>
<div class="header">
<p>
Next: <a href="#Pairlist-objects" accesskey="n" rel="next">Pairlist objects</a>, Previous: <a href="#Dot_002ddot_002ddot" accesskey="p" rel="prev">Dot-dot-dot</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Environments"></a>
<h4 class="subsection">2.1.10 Environments</h4>
<a name="index-environment-3"></a>
<p>Environments can be thought of as consisting of two things. A
<em>frame</em>, consisting of a set of symbol-value pairs, and an
<em>enclosure</em>, a pointer to an enclosing environment. When R
looks up the value for a symbol the frame is examined and if a
matching symbol is found its value will be returned. If not, the
enclosing environment is then accessed and the process repeated.
Environments form a tree structure in which the enclosures play the
role of parents. The tree of environments is rooted in an empty
<a name="index-emptyenv"></a>
environment, available through <code>emptyenv()</code>, which has no parent.
It is the direct parent of the environment of the base package
<a name="index-baseenv"></a>
(available through the <code>baseenv()</code> function). Formerly
<code>baseenv()</code> had the special value <code>NULL</code>, but as from
version 2.4.0, the use of <code>NULL</code> as an environment is defunct.
</p>
<p>Environments are created implicitly by function calls, as described in
<a href="#Function-objects">Function objects</a> and <a href="#Lexical-environment">Lexical environment</a>. In this case the
environment contains the variables local to the function (including the
arguments), and its enclosure is the environment of the currently called
function. Environments may also be created directly by <code>new.env</code>.
<a name="index-new_002eenv"></a>
The frame content of an environment can be accessed and manipulated by
use of <code>ls</code>, <code>get</code> and <code>assign</code> as well as <code>eval</code> and
<code>evalq</code>.
</p>
<p>The <code>parent.env</code> function may be used to access the enclosure of
an environment.
</p>
<p>Unlike most other R objects, environments are not copied when passed
to functions or used in assignments. Thus, if you assign the same
environment to several symbols and change one, the others will change
too. In particular, assigning attributes to an environment can lead to
surprises.
</p>
<hr>
<a name="Pairlist-objects"></a>
<div class="header">
<p>
Next: <a href="#Any_002dtype" accesskey="n" rel="next">Any-type</a>, Previous: <a href="#Environment-objects" accesskey="p" rel="prev">Environment objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Pairlist-objects-1"></a>
<h4 class="subsection">2.1.11 Pairlist objects</h4>
<p>Pairlist objects are similar to Lisp&rsquo;s dotted-pair lists. They are used
extensively in the internals of R, but are rarely visible in
interpreted code, although they are returned by <code>formals</code>, and can
be created by (e.g.) the <code>pairlist</code> function. A zero-length
pairlist is <code>NULL</code>, as would be expected in Lisp but in contrast to
a zero-length list.
<a name="index-pairlist"></a>
Each such object has three slots, a CAR value, a CDR value and a TAG
value. The TAG value is a text string and CAR and CDR usually
represent, respectively, a list item (head) and the remainder (tail) of
the list with a NULL object as terminator (the CAR/CDR terminology is
traditional Lisp and originally referred to the address and decrement
registers on an early 60&rsquo;s IBM computer).
</p>
<p>Pairlists are handled in the R language in exactly the same way as
generic vectors (&ldquo;lists&rdquo;). In particular, elements are accessed using
the same <code>[[]]</code> syntax. The use of pairlists is deprecated since
generic vectors are usually more efficient to use. When an internal
pairlist is accessed from R it is generally (including when
subsetted) converted to a generic vector.
</p>
<p>In a very few cases pairlists are user-visible: one is <code>.Options</code>.
</p>
<hr>
<a name="Any_002dtype"></a>
<div class="header">
<p>
Previous: <a href="#Pairlist-objects" accesskey="p" rel="prev">Pairlist objects</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-_0060_0060Any_0027_0027-type"></a>
<h4 class="subsection">2.1.12 The &ldquo;Any&rdquo; type</h4>
<p>It is not really possible for an object to be of &ldquo;Any&rdquo; type, but it is
nevertheless a valid type value. It gets used in certain (rather rare)
circumstances, e.g. <code>as.vector(x, &quot;any&quot;)</code>, indicating that type
<a name="index-coercion-2"></a>
coercion should not be done.
</p>
<hr>
<a name="Attributes"></a>
<div class="header">
<p>
Next: <a href="#Special-compound-objects" accesskey="n" rel="next">Special compound objects</a>, Previous: <a href="#Basic-types" accesskey="p" rel="prev">Basic types</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Attributes-1"></a>
<h3 class="section">2.2 Attributes</h3>
<a name="index-attributes"></a>
<a name="index-object-3"></a>
<p>All objects except <code>NULL</code> can have one or more attributes attached
to them. Attributes are stored as a pairlist where all elements are
named, but should be thought of as a set of name=value pairs. A listing
of the attributes can be obtained using <code>attributes</code> and set by
<code>attributes&lt;-</code>,
<a name="index-attributes-1"></a>
<a name="index-attributes_003c_002d"></a>
individual components are accessed using <code>attr</code> and <code>attr&lt;-</code>.
<a name="index-attr"></a>
<a name="index-attr_003c_002d"></a>
</p>
<p>Some attributes have special accessor
<a name="index-function_002c-accessor"></a>
functions (e.g. <code>levels&lt;-</code>
for factors) and these should be used when available. In addition to
hiding details of implementation they may perform additional operations.
R attempts to intercept calls to <code>attr&lt;-</code> and to
<code>attributes&lt;-</code> that involve the special attributes and enforces
the consistency checks.
</p>
<p>Matrices and arrays are simply vectors with the attribute <code>dim</code> and
optionally <code>dimnames</code> attached to the vector.
</p>
<p>Attributes are used to implement the class structure used in R. If an
object has a <code>class</code> attribute then that attribute will be examined
during
<a name="index-evaluation_002c-symbol"></a>
evaluation. The class structure in R is described in detail
in <a href="#Object_002doriented-programming">Object-oriented programming</a>.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Names" accesskey="1">Names</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dimensions" accesskey="2">Dimensions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dimnames" accesskey="3">Dimnames</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Classes" accesskey="4">Classes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Time-series-attributes" accesskey="5">Time series attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Copying-of-attributes" accesskey="6">Copying of attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Names"></a>
<div class="header">
<p>
Next: <a href="#Dimensions" accesskey="n" rel="next">Dimensions</a>, Previous: <a href="#Attributes" accesskey="p" rel="prev">Attributes</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Names-1"></a>
<h4 class="subsection">2.2.1 Names</h4>
<p>A <code>names</code> attribute, when present, labels the individual elements of
a vector or list. When an object is printed the <code>names</code> attribute,
when present, is used to label the elements. The <code>names</code> attribute
can also be used for indexing purposes, for example,
<code>quantile(x)[&quot;25%&quot;]</code>.
</p>
<p>One may get and set the names using <code>names</code> and <code>names&lt;-</code>
constructions.
<a name="index-names"></a>
<a name="index-names_003c_002d"></a>
<a name="index-type-4"></a>
The latter will perform the necessary consistency checks to ensure that
the names attribute has the proper type and length.
</p>
<p>Pairlists and one-dimensional arrays are treated specially. For pairlist
objects, a virtual <code>names</code> attribute is used; the <code>names</code>
attribute is really constructed from the tags of the list components.
For one-dimensional arrays the <code>names</code> attribute really accesses
<code>dimnames[[1]]</code>.
</p>
<hr>
<a name="Dimensions"></a>
<div class="header">
<p>
Next: <a href="#Dimnames" accesskey="n" rel="next">Dimnames</a>, Previous: <a href="#Names" accesskey="p" rel="prev">Names</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dimensions-1"></a>
<h4 class="subsection">2.2.2 Dimensions</h4>
<p>The <code>dim</code> attribute is used to implement arrays. The content of
the array is stored in a vector in column-major order and the <code>dim</code>
attribute is a vector of integers specifying the respective extents of
the array. R ensures that the length of the vector is the product of
the lengths of the dimensions. The length of one or more dimensions may
be zero.
</p>
<a name="index-vector-1"></a>
<p>A vector is not the same as a one-dimensional array since the latter has
a <code>dim</code> attribute of length one, whereas the former has no
<code>dim</code> attribute.
</p>
<hr>
<a name="Dimnames"></a>
<div class="header">
<p>
Next: <a href="#Classes" accesskey="n" rel="next">Classes</a>, Previous: <a href="#Dimensions" accesskey="p" rel="prev">Dimensions</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dimnames-1"></a>
<h4 class="subsection">2.2.3 Dimnames</h4>
<p>Arrays may name each dimension separately using the <code>dimnames</code>
attribute which is a list of character vectors. The <code>dimnames</code>
list may itself have names which are then used for extent headings when
printing arrays.
</p>
<hr>
<a name="Classes"></a>
<div class="header">
<p>
Next: <a href="#Time-series-attributes" accesskey="n" rel="next">Time series attributes</a>, Previous: <a href="#Dimnames" accesskey="p" rel="prev">Dimnames</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Classes-1"></a>
<h4 class="subsection">2.2.4 Classes</h4>
<p>R has an elaborate class system<a name="DOCF1" href="#FOOT1"><sup>1</sup></a>, principally controlled via
the <code>class</code> attribute. This attribute is a character vector
containing the list of classes that an object inherits from. This forms
the basis of the &ldquo;generic methods&rdquo; functionality in R.
</p>
<p>This attribute can be accessed and manipulated virtually without
restriction by users. There is no checking that an object actually
contains the components that class methods expect. Thus, altering the
<code>class</code> attribute should be done with caution, and when they are
available specific creation and
<a name="index-coercion-3"></a>
coercion functions should be preferred.
</p>
<hr>
<a name="Time-series-attributes"></a>
<div class="header">
<p>
Next: <a href="#Copying-of-attributes" accesskey="n" rel="next">Copying of attributes</a>, Previous: <a href="#Classes" accesskey="p" rel="prev">Classes</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Time-series-attributes-1"></a>
<h4 class="subsection">2.2.5 Time series attributes</h4>
<p>The <code>tsp</code> attribute is used to hold parameters of time series,
start, end, and frequency. This construction is mainly used to handle
series with periodic substructure such as monthly or quarterly data.
</p>
<hr>
<a name="Copying-of-attributes"></a>
<div class="header">
<p>
Previous: <a href="#Time-series-attributes" accesskey="p" rel="prev">Time series attributes</a>, Up: <a href="#Attributes" accesskey="u" rel="up">Attributes</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Copying-of-attributes-1"></a>
<h4 class="subsection">2.2.6 Copying of attributes</h4>
<p>Whether attributes should be copied when an object is altered is a
complex area, but there are some general rules (Becker, Chambers &amp;
Wilks, 1988, pp. 144&ndash;6).
</p>
<p>Scalar functions (those which operate element-by-element on a vector and
whose output is similar to the input) should preserve attributes (except
perhaps class).
</p>
<p>Binary operations normally copy most attributes from the longer argument
(and if they are of the same length from both, preferring the values on
the first). Here &lsquo;most&rsquo; means all except the <code>names</code>, <code>dim</code>
and <code>dimnames</code> which are set appropriately by the code for the
operator.
</p>
<p>Subsetting (other than by an empty index) generally drops all attributes
except <code>names</code>, <code>dim</code> and <code>dimnames</code> which are reset as
appropriate. On the other hand, subassignment generally preserves
attributes even if the length is changed. Coercion drops all
attributes.
</p>
<p>The default method for sorting drops all attributes except names, which
are sorted along with the object.
</p>
<hr>
<a name="Special-compound-objects"></a>
<div class="header">
<p>
Previous: <a href="#Attributes" accesskey="p" rel="prev">Attributes</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Special-compound-objects-1"></a>
<h3 class="section">2.3 Special compound objects</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Factors" accesskey="1">Factors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Data-frame-objects" accesskey="2">Data frame objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Factors"></a>
<div class="header">
<p>
Next: <a href="#Data-frame-objects" accesskey="n" rel="next">Data frame objects</a>, Previous: <a href="#Special-compound-objects" accesskey="p" rel="prev">Special compound objects</a>, Up: <a href="#Special-compound-objects" accesskey="u" rel="up">Special compound objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Factors-1"></a>
<h4 class="subsection">2.3.1 Factors</h4>
<p>Factors are used to describe items that can have a finite number of
values (gender, social class, etc.). A factor has a <code>levels</code>
attribute and class <code>&quot;factor&quot;</code>. Optionally, it may also contain a
<code>contrasts</code> attribute which controls the parametrisation used when
the factor is used in a
<a name="index-function_002c-modeling"></a>
<a name="index-modeling-function"></a>
modeling functions.
</p>
<p>A factor may be purely nominal or may have ordered categories. In the
latter case, it should be defined as such and have a <code>class</code> vector
<code>c(&quot;ordered&quot;,&quot; factor&quot;)</code>.
</p>
<p>Factors are currently implemented using an integer array to specify the
actual levels and a second array of names that are mapped to the
integers. Rather unfortunately users often make use of the
implementation in order to make some calculations easier. This,
however, is an implementation issue and is not guaranteed to hold in all
implementations of R.
</p>
<hr>
<a name="Data-frame-objects"></a>
<div class="header">
<p>
Previous: <a href="#Factors" accesskey="p" rel="prev">Factors</a>, Up: <a href="#Special-compound-objects" accesskey="u" rel="up">Special compound objects</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Data-frame-objects-1"></a>
<h4 class="subsection">2.3.2 Data frame objects</h4>
<p>Data frames are the R structures which most closely mimic the SAS or
SPSS data set, i.e. a &ldquo;cases by variables&rdquo; matrix of data.
</p>
<p>A data frame is a list of vectors, factors, and/or matrices all having
the same length (number of rows in the case of matrices). In addition,
a data frame generally has a <code>names</code> attribute labeling the
variables and a <code>row.names</code> attribute for labeling the cases.
</p>
<p>A data frame can contain a list that is the same length as the other
components. The list can contain elements of differing lengths thereby
providing a data structure for ragged arrays. However, as of this
writing such arrays are not generally handled correctly.
</p>
<hr>
<a name="Evaluation-of-expressions"></a>
<div class="header">
<p>
Next: <a href="#Functions" accesskey="n" rel="next">Functions</a>, Previous: <a href="#Objects" accesskey="p" rel="prev">Objects</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Evaluation-of-expressions-1"></a>
<h2 class="chapter">3 Evaluation of expressions</h2>
<p>When a user types a command at the prompt (or when an expression is read
from a file) the first thing that happens to it is that the command is
transformed by the
<a name="index-parsing-2"></a>
parser into an internal representation. The
evaluator executes parsed R expressions and returns the value of the
expression. All expressions have a value. This is the core of the
language.
</p>
<p>This chapter describes the basic mechanisms of the evaluator, but avoids
discussion of specific functions or groups of functions which are
described in separate chapters later on or where the help pages should
be sufficient documentation.
</p>
<p>Users can construct expressions and invoke the evaluator on them.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Simple-evaluation" accesskey="1">Simple evaluation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Control-structures" accesskey="2">Control structures</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Elementary-arithmetic-operations" accesskey="3">Elementary arithmetic operations</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Indexing" accesskey="4">Indexing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Scope-of-variables" accesskey="5">Scope of variables</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Simple-evaluation"></a>
<div class="header">
<p>
Next: <a href="#Control-structures" accesskey="n" rel="next">Control structures</a>, Previous: <a href="#Evaluation-of-expressions" accesskey="p" rel="prev">Evaluation of expressions</a>, Up: <a href="#Evaluation-of-expressions" accesskey="u" rel="up">Evaluation of expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Simple-evaluation-1"></a>
<h3 class="section">3.1 Simple evaluation</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Constants" accesskey="1">Constants</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Symbol-lookup" accesskey="2">Symbol lookup</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Function-calls" accesskey="3">Function calls</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Operators" accesskey="4">Operators</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Constants"></a>
<div class="header">
<p>
Next: <a href="#Symbol-lookup" accesskey="n" rel="next">Symbol lookup</a>, Previous: <a href="#Simple-evaluation" accesskey="p" rel="prev">Simple evaluation</a>, Up: <a href="#Simple-evaluation" accesskey="u" rel="up">Simple evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Constants-1"></a>
<h4 class="subsection">3.1.1 Constants</h4>
<p>Any number typed directly at the prompt is a constant and is evaluated.
</p>
<div class="example">
<pre class="example">&gt; 1
[1] 1
</pre></div>
<p>Perhaps unexpectedly, the number returned from the expression <code>1</code>
is a numeric. In most cases, the difference between an integer and a
numeric value will be unimportant as R will do the right thing when
using the numbers. There are, however, times when we would like to
explicitly create an integer value for a constant. We can do this by
calling the function <code>as.integer</code> or using various other
techniques. But perhaps the simplest approach is to qualify our
constant with the suffix character &lsquo;L&rsquo;.
For example, to create the integer value 1, we might use
</p>
<div class="example">
<pre class="example">&gt; 1L
[1]
</pre></div>
<p>We can use the &lsquo;L&rsquo; suffix to qualify any number with the intent of
making it an explicit integer. So &lsquo;0x10L&rsquo; creates the integer value
16 from the hexadecimal representation. The constant <code>1e3L</code> gives 1000
as an integer rather than a numeric value and is equivalent to <code>1000L</code>.
(Note that the &lsquo;L&rsquo; is treated as qualifying the term <code>1e3</code> and not the
<code>3</code>.) If we qualify a value with &lsquo;L&rsquo; that is not an integer value,
e.g. <code>1e-3L</code>, we get a warning and the numeric value is created.
A warning is also created if there is an unnecessary decimal point
in the number, e.g. <code>1.L</code>.
</p>
<p>We get a syntax error when using &lsquo;L&rsquo; with complex numbers,
e.g. <code>12iL</code> gives an error.
</p>
<p>Constants are fairly boring and to do more we need symbols.
</p>
<hr>
<a name="Symbol-lookup"></a>
<div class="header">
<p>
Next: <a href="#Function-calls" accesskey="n" rel="next">Function calls</a>, Previous: <a href="#Constants" accesskey="p" rel="prev">Constants</a>, Up: <a href="#Simple-evaluation" accesskey="u" rel="up">Simple evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Symbol-lookup-1"></a>
<h4 class="subsection">3.1.2 Symbol lookup</h4>
<p>When a new variable is created it must have a
<a name="index-name-2"></a>
name so it can be referenced and it usually has a value. The name itself is a
<a name="index-symbol-2"></a>
symbol.
When a symbol is
<a name="index-evaluation_002c-symbol-1"></a>
evaluated its
<a name="index-value"></a>
value is returned. Later we shall
explain in detail how to determine the value associated with a symbol.
</p>
<p>In this small example <code>y</code> is a symbol and its value is 4. A symbol
is an R object too, but one rarely needs to deal with symbols
directly, except when doing &ldquo;programming on the language&rdquo;
(<a href="#Computing-on-the-language">Computing on the language</a>).
</p>
<div class="example">
<pre class="example">&gt; y &lt;- 4
&gt; y
[1] 4
</pre></div>
<hr>
<a name="Function-calls"></a>
<div class="header">
<p>
Next: <a href="#Operators" accesskey="n" rel="next">Operators</a>, Previous: <a href="#Symbol-lookup" accesskey="p" rel="prev">Symbol lookup</a>, Up: <a href="#Simple-evaluation" accesskey="u" rel="up">Simple evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-calls-1"></a>
<h4 class="subsection">3.1.3 Function calls</h4>
<p>Most of the computations carried out in R involve the evaluation of
functions. We will also refer to this as
<a name="index-function-invocation"></a>
function <em>invocation</em>.
Functions are invoked by name with a list of arguments separated by
commas.
</p>
<div class="example">
<pre class="example">&gt; mean(1:10)
[1] 5.5
</pre></div>
<p>In this example the function <code>mean</code> was called with one argument,
the vector of integers from 1 to 10.
</p>
<p>R contains a huge number of functions with different purposes. Most
are used for producing a result which is an R object, but others are
used for their side effects, e.g., printing and plotting functions.
</p>
<a name="index-function-7"></a>
<a name="index-function-arguments"></a>
<p>Function calls can have <em>tagged</em> (or <em>named</em>) arguments, as in
<code>plot(x, y, pch = 3)</code>. Arguments without tags are known as
<em>positional</em> since the function must distinguish their meaning from
their sequential positions among the arguments of the call, e.g., that
<code>x</code> denotes the abscissa variable and <code>y</code> the ordinate. The
use of tags/names is an obvious convenience for functions with a large
number of optional arguments.
</p>
<a name="index-function_002c-assignment"></a>
<p>A special type of function calls can appear on the left hand side of
the
<a name="index-assignment-1"></a>
assignment operator as in
</p>
<div class="example">
<pre class="example">&gt; class(x) &lt;- &quot;foo&quot;
</pre></div>
<p>What this construction really does is to call the function
<code>class&lt;-</code> with the original object and the right hand side. This
function performs the modification of the object and returns the result
which is then stored back into the original variable. (At least
conceptually, this is what happens. Some additional effort is made to
avoid unnecessary data duplication.)
</p>
<hr>
<a name="Operators"></a>
<div class="header">
<p>
Previous: <a href="#Function-calls" accesskey="p" rel="prev">Function calls</a>, Up: <a href="#Simple-evaluation" accesskey="u" rel="up">Simple evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Operators-1"></a>
<h4 class="subsection">3.1.4 Operators</h4>
<p>R allows the use of arithmetic expressions using operators similar to
those of the C programming language, for instance
</p>
<div class="example">
<pre class="example">&gt; 1 + 2
[1] 3
</pre></div>
<p>Expressions can be grouped using parentheses, mixed with function calls,
and assigned to variables in a straightforward manner
</p>
<div class="example">
<pre class="example">&gt; y &lt;- 2 * (a + log(x))
</pre></div>
<p>R contains a number of operators. They are listed in the table
below.
</p>
<blockquote>
<table summary="">
<tr><td width="10%"><code>-</code></td><td width="70%">Minus, can be unary or binary</td></tr>
<tr><td width="10%"><code>+</code></td><td width="70%">Plus, can be unary or binary</td></tr>
<tr><td width="10%"><code>!</code></td><td width="70%">Unary not</td></tr>
<tr><td width="10%"><code>~</code></td><td width="70%">Tilde, used for model formulae, can be either unary or binary</td></tr>
<tr><td width="10%"><code>?</code></td><td width="70%">Help</td></tr>
<tr><td width="10%"><code>:</code></td><td width="70%">Sequence, binary (in model formulae: interaction)</td></tr>
<tr><td width="10%"><code>*</code></td><td width="70%">Multiplication, binary</td></tr>
<tr><td width="10%"><code>/</code></td><td width="70%">Division, binary</td></tr>
<tr><td width="10%"><code>^</code></td><td width="70%">Exponentiation, binary</td></tr>
<tr><td width="10%"><code>%<var>x</var>%</code></td><td width="70%">Special binary operators, <var>x</var> can be replaced by any valid name</td></tr>
<tr><td width="10%"><code>%%</code></td><td width="70%">Modulus, binary</td></tr>
<tr><td width="10%"><code>%/%</code></td><td width="70%">Integer divide, binary</td></tr>
<tr><td width="10%"><code>%*%</code></td><td width="70%">Matrix product, binary</td></tr>
<tr><td width="10%"><code>%o%</code></td><td width="70%">Outer product, binary</td></tr>
<tr><td width="10%"><code>%x%</code></td><td width="70%">Kronecker product, binary</td></tr>
<tr><td width="10%"><code>%in%</code></td><td width="70%">Matching operator, binary (in model formulae: nesting)</td></tr>
<tr><td width="10%"><code>&lt;</code></td><td width="70%">Less than, binary</td></tr>
<tr><td width="10%"><code>&gt;</code></td><td width="70%">Greater than, binary</td></tr>
<tr><td width="10%"><code>==</code></td><td width="70%">Equal to, binary</td></tr>
<tr><td width="10%"><code>&gt;=</code></td><td width="70%">Greater than or equal to, binary</td></tr>
<tr><td width="10%"><code>&lt;=</code></td><td width="70%">Less than or equal to, binary</td></tr>
<tr><td width="10%"><code>&amp;</code></td><td width="70%">And, binary, vectorized</td></tr>
<tr><td width="10%"><code>&amp;&amp;</code></td><td width="70%">And, binary, not vectorized</td></tr>
<tr><td width="10%"><code>|</code></td><td width="70%">Or, binary, vectorized</td></tr>
<tr><td width="10%"><code>||</code></td><td width="70%">Or, binary, not vectorized</td></tr>
<tr><td width="10%"><code>&lt;-</code></td><td width="70%">Left assignment, binary</td></tr>
<tr><td width="10%"><code>-&gt;</code></td><td width="70%">Right assignment, binary</td></tr>
<tr><td width="10%"><code>$</code></td><td width="70%">List subset, binary</td></tr>
</table>
</blockquote>
<p>Except for the syntax, there is no difference between applying an
operator and calling a function. In fact, <code>x + y</code> can equivalently
be written <code>`+`(x, y)</code>. Notice that since &lsquo;<samp>+</samp>&rsquo; is a
non-standard function name, it needs to be quoted.
</p>
<a name="index-vector-2"></a>
<p>R deals with entire vectors of data at a time, and most of the
elementary operators and basic mathematical functions like <code>log</code>
are vectorized (as indicated in the table above). This means that
e.g. adding two vectors of the same length will create a vector
containing the element-wise sums, implicitly looping over the vector
index. This applies also to other operators like <code>-</code>, <code>*</code>,
and <code>/</code> as well as to higher dimensional structures. Notice in
particular that multiplying two matrices does not produce the usual
matrix product (the <code>%*%</code> operator exists for that purpose). Some
finer points relating to vectorized operations will be discussed in
<a href="#Elementary-arithmetic-operations">Elementary arithmetic operations</a>.
</p>
<p>To access individual elements of an atomic vector, one generally uses
the <code>x[i]</code> construction.
</p>
<div class="example">
<pre class="example">&gt; x &lt;- rnorm(5)
&gt; x
[1] -0.12526937 -0.27961154 -1.03718717 -0.08156527 1.37167090
&gt; x[2]
[1] -0.2796115
</pre></div>
<p>List components are more commonly accessed using <code>x$a</code> or
<code>x[[i]]</code>.
</p>
<div class="example">
<pre class="example">&gt; x &lt;- options()
&gt; x$prompt
[1] &quot;&gt; &quot;
</pre></div>
<p>Indexing constructs can also appear on the right hand side of an
<a name="index-assignment-2"></a>
assignment.
</p>
<p>Like the other operators, indexing is really done by functions, and one
could have used <code>`[`(x, 2)</code> instead of <code>x[2]</code>.
</p>
<p>R&rsquo;s indexing operations contain many advanced features which are
further described in <a href="#Indexing">Indexing</a>.
</p>
<hr>
<a name="Control-structures"></a>
<div class="header">
<p>
Next: <a href="#Elementary-arithmetic-operations" accesskey="n" rel="next">Elementary arithmetic operations</a>, Previous: <a href="#Simple-evaluation" accesskey="p" rel="prev">Simple evaluation</a>, Up: <a href="#Evaluation-of-expressions" accesskey="u" rel="up">Evaluation of expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Control-structures-1"></a>
<h3 class="section">3.2 Control structures</h3>
<p>Computation in R consists of sequentially evaluating
<em>statements</em>. Statements, such as <code>x&lt;-1:10</code> or
<code>mean(y)</code>, can be separated by either a semi-colon or a new line.
Whenever the
<a name="index-evaluation_002c-statement"></a>
evaluator is presented with a syntactically complete
statement that statement is evaluated and the <em>value</em> returned.
The result of evaluating a statement can be referred to as the value of
the statement<a name="DOCF2" href="#FOOT2"><sup>2</sup></a> The value can
always be assigned to a symbol.
</p>
<p>Both semicolons and new lines can be used to separate statements. A
semicolon always indicates the end of a statement while a new line
<em>may</em> indicate the end of a statement. If the current statement is
not syntactically complete new lines are simply ignored by the
evaluator. If the session is interactive the prompt changes from
&lsquo;<samp>&gt;</samp>&rsquo; to &lsquo;<samp>+</samp>&rsquo;.
</p>
<div class="example">
<pre class="example">&gt; x &lt;- 0; x + 5
[1] 5
&gt; y &lt;- 1:10
&gt; 1; 2
[1] 1
[1] 2
</pre></div>
<p>Statements can be grouped together using braces &lsquo;<samp>{</samp>&rsquo; and &lsquo;<samp>}</samp>&rsquo;.
A group of statements is sometimes called a <em>block</em>. Single
statements are evaluated when a new line is typed at the end of the
syntactically complete statement. Blocks are not evaluated until a new
line is entered after the closing brace. In the remainder of this
section, <em>statement</em> refers to either a single statement or a
block.
</p>
<div class="example">
<pre class="example">&gt; { x &lt;- 0
+ x + 5
+ }
[1] 5
</pre></div>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#if" accesskey="1">if</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Looping" accesskey="2">Looping</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#repeat" accesskey="3">repeat</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#while" accesskey="4">while</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#for" accesskey="5">for</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#switch" accesskey="6">switch</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="if"></a>
<div class="header">
<p>
Next: <a href="#Looping" accesskey="n" rel="next">Looping</a>, Previous: <a href="#Control-structures" accesskey="p" rel="prev">Control structures</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="if-1"></a>
<h4 class="subsection">3.2.1 if</h4>
<p>The <code>if</code>/<code>else</code> statement conditionally evaluates two
statements. There is a <em>condition</em> which is evaluated and if the
<em>value</em> is <code>TRUE</code> then the first statement is evaluated;
otherwise the second statement will be evaluated. The
<code>if</code>/<code>else</code> statement returns, as its value, the value of the
statement that was selected. The formal syntax is
</p>
<div class="example">
<pre class="example">if ( <var>statement1</var> )
<var>statement2</var>
else
<var>statement3</var>
</pre></div>
<p>First, <var>statement1</var> is evaluated to yield <var>value1</var>. If
<var>value1</var> is a logical vector with first element <code>TRUE</code> then
<var>statement2</var> is evaluated. If the first element of <var>value1</var> is
<code>FALSE</code> then <var>statement3</var> is evaluated. If <var>value1</var> is a
numeric vector then <var>statement3</var> is evaluated when the first element
of <var>value1</var> is zero and otherwise <var>statement2</var> is evaluated.
Only the first element of <var>value1</var> is used. All other elements are
ignored. If <var>value1</var> has any type other than a logical or a numeric
vector an error is signalled.
</p>
<p><code>if</code>/<code>else</code> statements can be used to avoid numeric problems
such as taking the logarithm of a negative number. Because
<code>if</code>/<code>else</code> statements are the same as other statements you
can assign the value of them. The two examples below are equivalent.
</p>
<div class="example">
<pre class="example">&gt; if( any(x &lt;= 0) ) y &lt;- log(1+x) else y &lt;- log(x)
&gt; y &lt;- if( any(x &lt;= 0) ) log(1+x) else log(x)
</pre></div>
<p>The <code>else</code> clause is optional. The statement <code>if(any(x &lt;= 0))
x &lt;- x[x &lt;= 0]</code> is valid. When the <code>if</code> statement is not in a
block the <code>else</code>, if present, must appear on the same line as
the end of <var>statement2</var>. Otherwise the new line at the end of
<var>statement2</var> completes the <code>if</code> and yields a syntactically
complete statement that is evaluated. A simple solution is to use a
compound statement wrapped in braces, putting the <code>else</code> on the
same line as the closing brace that marks the end of the statement.
</p>
<p><code>if</code>/<code>else</code> statements can be nested.
</p>
<div class="example">
<pre class="example">if ( <var>statement1</var> ) {
<var>statement2</var>
} else if ( <var>statement3</var> ) {
<var>statement4</var>
} else if ( <var>statement5</var> ) {
<var>statement6</var>
} else
<var>statement8</var>
</pre></div>
<p>One of the even numbered statements will be evaluated and the resulting
value returned. If the optional <code>else</code> clause is omitted and all
the odd numbered <var>statement</var>s evaluate to <code>FALSE</code> no statement
will be evaluated and <code>NULL</code> is returned.
</p>
<p>The odd numbered <var>statement</var>s are evaluated, in order, until one
evaluates to <code>TRUE</code> and then the associated even numbered
<var>statement</var> is evaluated. In this example, <var>statement6</var> will
only be evaluated if <var>statement1</var> is <code>FALSE</code> and
<var>statement3</var> is <code>FALSE</code> and <var>statement5</var> is <code>TRUE</code>.
There is no limit to the number of <code>else if</code> clauses that are
permitted.
</p>
<hr>
<a name="Looping"></a>
<div class="header">
<p>
Next: <a href="#repeat" accesskey="n" rel="next">repeat</a>, Previous: <a href="#if" accesskey="p" rel="prev">if</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Looping-1"></a>
<h4 class="subsection">3.2.2 Looping</h4>
<p>R has three statements that provide explicit
looping.<a name="DOCF3" href="#FOOT3"><sup>3</sup></a> They are <code>for</code>, <code>while</code> and
<code>repeat</code>. The two built-in constructs, <code>next</code> and
<code>break</code>, provide additional control over the evaluation.
R provides other functions for
implicit looping such as <code>tapply</code>, <code>apply</code>, and <code>lapply</code>.
In addition many operations, especially arithmetic ones, are vectorized
so you may not need to use a loop.
</p>
<p>There are two statements that can be used to explicitly control looping.
They are <code>break</code> and <code>next</code>.
<a name="index-break"></a>
<a name="index-next"></a>
The <code>break</code> statement causes an exit from the innermost loop that
is currently being executed. The <code>next</code> statement immediately
causes control to return to the start of the loop. The next iteration
of the loop (if there is one) is then executed. No statement below
<code>next</code> in the current loop is evaluated.
</p>
<p>The value returned by a loop statement is always <code>NULL</code>
and is returned invisibly.
</p>
<hr>
<a name="repeat"></a>
<div class="header">
<p>
Next: <a href="#while" accesskey="n" rel="next">while</a>, Previous: <a href="#Looping" accesskey="p" rel="prev">Looping</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="repeat-1"></a>
<h4 class="subsection">3.2.3 repeat</h4>
<a name="index-repeat"></a>
<p>The <code>repeat</code> statement causes repeated evaluation of the body until
a break is specifically requested. This means that you need to be
careful when using <code>repeat</code> because of the danger of an infinite
loop. The syntax of the <code>repeat</code> loop is
</p>
<div class="example">
<pre class="example">repeat <var>statement</var>
</pre></div>
<p>When using <code>repeat</code>, <var>statement</var> must be a block statement.
You need to both perform some computation and test whether or not to
break from the loop and usually this requires two statements.
</p>
<hr>
<a name="while"></a>
<div class="header">
<p>
Next: <a href="#for" accesskey="n" rel="next">for</a>, Previous: <a href="#repeat" accesskey="p" rel="prev">repeat</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="while-1"></a>
<h4 class="subsection">3.2.4 while</h4>
<a name="index-while"></a>
<p>The <code>while</code> statement is very similar to the <code>repeat</code>
statement. The syntax of the <code>while</code> loop is
</p>
<div class="example">
<pre class="example">while ( <var>statement1</var> ) <var>statement2</var>
</pre></div>
<p>where <var>statement1</var> is evaluated and if its value is <code>TRUE</code> then
<var>statement2</var> is evaluated. This process continues until
<var>statement1</var> evaluates to <code>FALSE</code>.
</p>
<hr>
<a name="for"></a>
<div class="header">
<p>
Next: <a href="#switch" accesskey="n" rel="next">switch</a>, Previous: <a href="#while" accesskey="p" rel="prev">while</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="for-1"></a>
<h4 class="subsection">3.2.5 for</h4>
<a name="index-for"></a>
<p>The syntax of the <code>for</code> loop is
</p>
<div class="example">
<pre class="example">for ( <var>name</var> in <var>vector</var> )
<var>statement1</var>
</pre></div>
<p>where <var>vector</var> can be either a vector or a list. For each element
in <var>vector</var> the variable <var>name</var> is set to the value of that
element and <var>statement1</var> is evaluated. A side effect is that the
variable <var>name</var> still exists after the loop has concluded and it has
the value of the last element of <var>vector</var> that the loop was
evaluated for.
</p>
<hr>
<a name="switch"></a>
<div class="header">
<p>
Previous: <a href="#for" accesskey="p" rel="prev">for</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="switch-1"></a>
<h4 class="subsection">3.2.6 switch</h4>
<a name="index-switch"></a>
<p>Technically speaking, <code>switch</code> is just another function, but its
semantics are close to those of control structures of other programming
languages.
</p>
<p>The syntax is
</p>
<div class="example">
<pre class="example">switch (<var>statement</var>, <var>list</var>)
</pre></div>
<p>where the elements of <var>list</var> may be named. First, <var>statement</var>
is evaluated and the result, <var>value</var>, obtained. If <var>value</var> is a
number between 1 and the length of <var>list</var> then the corresponding
element of <var>list</var> is evaluated and the result returned. If <var>value</var>
is too large or too small <code>NULL</code> is returned.
</p>
<div class="example">
<pre class="example">&gt; x &lt;- 3
&gt; switch(x, 2+2, mean(1:10), rnorm(5))
[1] 2.2903605 2.3271663 -0.7060073 1.3622045 -0.2892720
&gt; switch(2, 2+2, mean(1:10), rnorm(5))
[1] 5.5
&gt; switch(6, 2+2, mean(1:10), rnorm(5))
NULL
</pre></div>
<p>If <var>value</var> is a character vector then the element of &lsquo;<samp>...</samp>&rsquo; with
a name that exactly matches <var>value</var> is evaluated. If there is no
match a single unnamed argument will be used as a default. If no
default is specified, <code>NULL</code> is returned.
</p>
<div class="example">
<pre class="example">&gt; y &lt;- &quot;fruit&quot;
&gt; switch(y, fruit = &quot;banana&quot;, vegetable = &quot;broccoli&quot;, &quot;Neither&quot;)
[1] &quot;banana&quot;
&gt; y &lt;- &quot;meat&quot;
&gt; switch(y, fruit = &quot;banana&quot;, vegetable = &quot;broccoli&quot;, &quot;Neither&quot;)
[1] &quot;Neither&quot;
</pre></div>
<p>A common use of <code>switch</code> is to branch according to the character
value of one of the arguments to a function.
</p>
<div class="example">
<pre class="example">&gt; centre &lt;- function(x, type) {
+ switch(type,
+ mean = mean(x),
+ median = median(x),
+ trimmed = mean(x, trim = .1))
+ }
&gt; x &lt;- rcauchy(10)
&gt; centre(x, &quot;mean&quot;)
[1] 0.8760325
&gt; centre(x, &quot;median&quot;)
[1] 0.5360891
&gt; centre(x, &quot;trimmed&quot;)
[1] 0.6086504
</pre></div>
<p><code>switch</code> returns either the value of the statement that was
evaluated or <code>NULL</code> if no statement was evaluated.
</p>
<p>To choose from a list of alternatives that already exists <code>switch</code>
may not be the best way to select one for evaluation. It is often
better to use <code>eval</code> and the subset operator, <code>[[</code>, directly
via <code>eval(x[[condition]])</code>.
</p>
<hr>
<a name="Elementary-arithmetic-operations"></a>
<div class="header">
<p>
Next: <a href="#Indexing" accesskey="n" rel="next">Indexing</a>, Previous: <a href="#Control-structures" accesskey="p" rel="prev">Control structures</a>, Up: <a href="#Evaluation-of-expressions" accesskey="u" rel="up">Evaluation of expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Elementary-arithmetic-operations-1"></a>
<h3 class="section">3.3 Elementary arithmetic operations</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Recycling-rules" accesskey="1">Recycling rules</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Propagation-of-names" accesskey="2">Propagation of names</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Dimensional-attributes" accesskey="3">Dimensional attributes</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#NA-handling" accesskey="4">NA handling</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<p>In this section, we discuss the finer points of the rules that apply to
basic operation like addition or multiplication of two vectors or
matrices.
</p>
<hr>
<a name="Recycling-rules"></a>
<div class="header">
<p>
Next: <a href="#Propagation-of-names" accesskey="n" rel="next">Propagation of names</a>, Previous: <a href="#Elementary-arithmetic-operations" accesskey="p" rel="prev">Elementary arithmetic operations</a>, Up: <a href="#Elementary-arithmetic-operations" accesskey="u" rel="up">Elementary arithmetic operations</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Recycling-rules-1"></a>
<h4 class="subsection">3.3.1 Recycling rules</h4>
<p>If one tries to add two structures with a different number of elements,
then the shortest is recycled to length of longest. That is, if for
instance you add <code>c(1, 2, 3)</code> to a six-element vector then you will
really add <code>c(1, 2, 3, 1, 2, 3)</code>. If the length of the longer
vector is not a multiple of the shorter one, a warning is given.
</p>
<p>As from R 1.4.0, any arithmetic operation involving a zero-length
vector has a zero-length result.
</p>
<hr>
<a name="Propagation-of-names"></a>
<div class="header">
<p>
Next: <a href="#Dimensional-attributes" accesskey="n" rel="next">Dimensional attributes</a>, Previous: <a href="#Recycling-rules" accesskey="p" rel="prev">Recycling rules</a>, Up: <a href="#Elementary-arithmetic-operations" accesskey="u" rel="up">Elementary arithmetic operations</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Propagation-of-names-1"></a>
<h4 class="subsection">3.3.2 Propagation of names</h4>
<a name="index-name-3"></a>
<p>propagation of names (first one wins, I think - also if it has no
names?? &mdash;- first one *with names* wins, recycling causes shortest to
lose names)
</p>
<hr>
<a name="Dimensional-attributes"></a>
<div class="header">
<p>
Next: <a href="#NA-handling" accesskey="n" rel="next">NA handling</a>, Previous: <a href="#Propagation-of-names" accesskey="p" rel="prev">Propagation of names</a>, Up: <a href="#Elementary-arithmetic-operations" accesskey="u" rel="up">Elementary arithmetic operations</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Dimensional-attributes-1"></a>
<h4 class="subsection">3.3.3 Dimensional attributes</h4>
<p>(matrix+matrix, dimensions must match. vector+matrix: first recycle,
then check if dims fit, error if not)
</p>
<hr>
<a name="NA-handling"></a>
<div class="header">
<p>
Previous: <a href="#Dimensional-attributes" accesskey="p" rel="prev">Dimensional attributes</a>, Up: <a href="#Elementary-arithmetic-operations" accesskey="u" rel="up">Elementary arithmetic operations</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="NA-handling-1"></a>
<h4 class="subsection">3.3.4 NA handling</h4>
<p>Missing values in the statistical sense, that is, variables whose value
is not known, have the value <code>NA</code>. This should not be confused with
the <code>missing</code> property for a function argument that has not been
supplied (see <a href="#Arguments">Arguments</a>).
<a name="index-missing"></a>
<a name="index-NA"></a>
<a name="index-NaN"></a>
</p>
<a name="index-type-5"></a>
<p>As the elements of an atomic vector must be of the same type there are
multiple types of <code>NA</code> values. There is one case where this is
particularly important to the user. The default type of <code>NA</code> is
<code>logical</code>, unless coerced to some other type, so the appearance of
a missing value may trigger logical rather than numeric indexing (see
<a href="#Indexing">Indexing</a> for details).
</p>
<p>Numeric and logical calculations with <code>NA</code> generally return
<code>NA</code>. In cases where the result of the operation would be the same
for all possible values the <code>NA</code> could take, the operation may
return this value. In particular, &lsquo;<samp>FALSE &amp; NA</samp>&rsquo; is <code>FALSE</code>,
&lsquo;<samp>TRUE | NA</samp>&rsquo; is <code>TRUE</code>. <code>NA</code> is not equal to any other
value or to itself; testing for <code>NA</code> is done using <code>is.na</code>.
<a name="index-is_002ena"></a>
However, an <code>NA</code> value will match another <code>NA</code> value in
<code>match</code>.
</p>
<p>Numeric calculations whose result is undefined, such as &lsquo;<samp>0/0</samp>&rsquo;,
produce the value <code>NaN</code>. This exists only in the <code>double</code>
type and for real or imaginary components of the complex type. The
function <code>is.nan</code> is provided to check specifically for
<a name="index-is_002enan"></a>
<code>NaN</code>, <code>is.na</code> also returns <code>TRUE</code> for <code>NaN</code>.
<a name="index-coercion-4"></a>
Coercing <code>NaN</code> to logical or integer type gives an <code>NA</code> of the
appropriate type, but coercion to character gives the string
<code>&quot;NaN&quot;</code>. <code>NaN</code> values are incomparable so tests of equality
or collation involving <code>NaN</code> will result in <code>NA</code>. They are
regarded as matching any <code>NaN</code> value (and no other value, not even
<code>NA</code>) by <code>match</code>.
</p>
<p>The <code>NA</code> of character type is as from R 1.5.0 distinct from the
string <code>&quot;NA&quot;</code>. Programmers who need to specify an explicit string
<code>NA</code> should use &lsquo;<samp>as.character(NA)</samp>&rsquo; rather than <code>&quot;NA&quot;</code>, or
set elements to <code>NA</code> using <code>is.na&lt;-</code>.
</p>
<p>As from R 2.5.0 there are constants <code>NA_integer_</code>,
<code>NA_real_</code>, <code>NA_complex_</code> and <code>NA_character_</code> which will
generate (in the parser) an <code>NA</code> value of the appropriate type,
and will be used in deparsing when it is not otherwise possible to
identify the type of an <code>NA</code> (and the <code>control</code> options ask
for this to be done).
</p>
<p>There is no <code>NA</code> value for raw vectors.
</p>
<hr>
<a name="Indexing"></a>
<div class="header">
<p>
Next: <a href="#Scope-of-variables" accesskey="n" rel="next">Scope of variables</a>, Previous: <a href="#Elementary-arithmetic-operations" accesskey="p" rel="prev">Elementary arithmetic operations</a>, Up: <a href="#Evaluation-of-expressions" accesskey="u" rel="up">Evaluation of expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Indexing-1"></a>
<h3 class="section">3.4 Indexing</h3>
<p>R contains several constructs which allow access to individual
elements or subsets through indexing operations. In the case of the
basic vector types one can access the i-th element using <code>x[i]</code>,
but there is also indexing of lists, matrices, and multi-dimensional
arrays. There are several forms of indexing in addition to indexing
with a single integer. Indexing can be used both to extract part of an
object and to replace parts of an object (or to add parts).
</p>
<p>R has three basic indexing operators, with syntax displayed by the
following examples
</p>
<div class="example">
<pre class="example">x[i]
x[i, j]
x[[i]]
x[[i, j]]
x$a
x$&quot;a&quot;
</pre></div>
<a name="index-_005b"></a>
<a name="index-_005b_005b"></a>
<a name="index-_0024"></a>
<a name="index-index-2"></a>
<p>For vectors and matrices the <code>[[</code> forms are rarely used, although
they have some slight semantic differences from the <code>[</code> form (e.g.
it drops any <code>names</code> or <code>dimnames</code> attribute, and that partial
matching is used for character indices). When indexing
multi-dimensional structures with a single index, <code>x[[i]]</code> or
<code>x[i]</code> will return the <code>i</code>th sequential element of <code>x</code>.
</p>
<p>For lists, one generally uses <code>[[</code> to select any single element,
whereas <code>[</code> returns a list of the selected elements.
</p>
<p>The <code>[[</code> form allows only a single element to be selected using
integer or character indices, whereas <code>[</code> allows indexing by
vectors. Note though that for a list or other recursive object, the
index can be a vector and each element of the vector is applied in
turn to the list, the selected component, the selected component of
that component, and so on. The result is still a single element.
</p>
<p>The form using <code>$</code> applies to recursive objects such as lists and
pairlists. It allows only a literal character string or a symbol as the
index. That is, the index is not computable: for cases where you need
to evaluate an expression to find the index, use <code>x[[expr]]</code>. When
<code>$</code> is applied to a non-recursive object the result used to be
always <code>NULL</code>: as from R 2.6.0 this is an error.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Indexing-by-vectors" accesskey="1">Indexing by vectors</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Indexing-matrices-and-arrays" accesskey="2">Indexing matrices and arrays</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Indexing-other-structures" accesskey="3">Indexing other structures</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Subset-assignment" accesskey="4">Subset assignment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Indexing-by-vectors"></a>
<div class="header">
<p>
Next: <a href="#Indexing-matrices-and-arrays" accesskey="n" rel="next">Indexing matrices and arrays</a>, Previous: <a href="#Indexing" accesskey="p" rel="prev">Indexing</a>, Up: <a href="#Indexing" accesskey="u" rel="up">Indexing</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Indexing-by-vectors-1"></a>
<h4 class="subsection">3.4.1 Indexing by vectors</h4>
<p>R allows some powerful constructions using vectors as indices. We
shall discuss indexing of simple vectors first. For simplicity, assume
that the expression is <code>x[i]</code>. Then the following possibilities
exist according to the type of <code>i</code>.
</p>
<ul>
<li> <a name="index-index-3"></a>
<strong>Integer</strong>. All elements of <code>i</code> must have the same sign. If
they are positive, the elements of <code>x</code> with those index numbers are
selected. If <code>i</code> contains negative elements, all elements except
those indicated are selected.
<p>If <code>i</code> is positive and exceeds <code>length(x)</code> then the
corresponding selection is <code>NA</code>. Negative out of bounds values
for <code>i</code> are silently disregarded since R version 2.6.0, S compatibly,
as they mean to drop non-existing elements and that is an empty operation
(&ldquo;no-op&rdquo;).
</p>
<p>A special case is the zero index, which has null effects: <code>x[0]</code> is
an empty vector and otherwise including zeros among positive or negative
indices has the same effect as if they were omitted.
</p>
</li><li> <strong>Other numeric</strong>. Non-integer values are converted to integer
(by truncation towards zero) before use.
</li><li> <strong>Logical</strong>. The indexing <code>i</code> should generally have the same
length as <code>x</code>. If it is shorter, then its elements will be
recycled as discussed in <a href="#Elementary-arithmetic-operations">Elementary arithmetic operations</a>. If it
is longer, then <code>x</code> is conceptually extended with <code>NA</code>s. The
selected values of <code>x</code> are those for which <code>i</code> is <code>TRUE</code>.
</li><li> <a name="index-partial-matching"></a>
<strong>Character</strong>. The strings in <code>i</code> are matched against the
names attribute of <code>x</code> and the resulting integers are used. For
<code>[[</code> and <code>$</code> partial matching is used if exact matching fails,
so <code>x$aa</code> will match <code>x$aabb</code> if <code>x</code> does not contain a component
named <code>&quot;aa&quot;</code> and <code>&quot;aabb&quot;</code> is the only name which has prefix
<code>&quot;aa&quot;</code>. For <code>[[</code>, partial matching can be controlled via the
<code>exact</code> argument which defaults to <code>NA</code> indicating that
partial matching is allowed, but should result in a warning when it
occurs. Setting <code>exact</code> to <code>TRUE</code> prevents partial matching
from occurring, a <code>FALSE</code> value allows it and does not issue any
warnings. Note that <code>[</code> always requires an exact match. The string
<code>&quot;&quot;</code> is treated specially: it indicates &lsquo;no name&rsquo; and matches no
element (not even those without a name). Note that partial matching is
only used when extracting and not when replacing.
</li><li> <strong>Factor</strong>. The result is identical to <code>x[as.integer(i)]</code>.
The factor levels are never used. If so desired, use
<code>x[as.character(i)]</code> or a similar construction.
</li><li> <strong>Empty</strong>. The expression <code>x[]</code> returns <code>x</code>, but drops
&ldquo;irrelevant&rdquo; attributes from the result. Only <code>names</code> and in
multi-dimensional arrays <code>dim</code> and <code>dimnames</code> attributes are
retained.
</li><li> <strong>NULL</strong>. This is treated as if it were <code>integer(0)</code>.
</li></ul>
<p>Indexing with a missing (i.e. <code>NA</code>) value gives an <code>NA</code>
result. This rule applies also to the case of logical indexing,
i.e. the elements of <code>x</code> that have an <code>NA</code> selector in
<code>i</code> get included in the result, but their value will be <code>NA</code>.
<a name="index-NA-1"></a>
</p>
<p>Notice however, that there are different modes of <code>NA</code>&mdash;the
literal constant is of mode <code>&quot;logical&quot;</code>, but it is frequently
automatically coerced to other types. One effect of this is that
<code>x[NA]</code> has the length of <code>x</code>, but <code>x[c(1, NA)]</code> has
length 2. That is because the rules for logical indices apply in the
former case, but those for integer indices in the latter.
</p>
<p>Indexing with <code>[</code> will also carry out the relevant subsetting of
any names attributes.
</p>
<hr>
<a name="Indexing-matrices-and-arrays"></a>
<div class="header">
<p>
Next: <a href="#Indexing-other-structures" accesskey="n" rel="next">Indexing other structures</a>, Previous: <a href="#Indexing-by-vectors" accesskey="p" rel="prev">Indexing by vectors</a>, Up: <a href="#Indexing" accesskey="u" rel="up">Indexing</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Indexing-matrices-and-arrays-1"></a>
<h4 class="subsection">3.4.2 Indexing matrices and arrays</h4>
<a name="index-index-4"></a>
<p>Subsetting multi-dimensional structures generally follows the same rules
as single-dimensional indexing for each index variable, with the
relevant component of <code>dimnames</code> taking the place of <code>names</code>.
A couple of special rules apply, though:
</p>
<p>Normally, a structure is accessed using the number of indices
corresponding to its dimension. It is however also possible to use a
single index in which case the <code>dim</code> and <code>dimnames</code> attributes
are disregarded and the result is effectively that of <code>c(m)[i]</code>.
Notice that <code>m[1]</code> is usually very different from <code>m[1, ]</code> or
<code>m[, 1]</code>.
</p>
<p>It is possible to use a matrix of integers as an index. In this case,
the number of columns of the matrix should match the number of
dimensions of the structure, and the result will be a vector with length
as the number of rows of the matrix. The following example shows how
to extract the elements <code>m[1, 1]</code> and <code>m[2, 2]</code> in one
operation.
</p>
<div class="example">
<pre class="example">&gt; m &lt;- matrix(1:4, 2)
&gt; m
[,1] [,2]
[1,] 1 3
[2,] 2 4
&gt; i &lt;- matrix(c(1, 1, 2, 2), 2, byrow = TRUE)
&gt; i
[,1] [,2]
[1,] 1 1
[2,] 2 2
&gt; m[i]
[1] 1 4
</pre></div>
<p>Indexing matrices may not contain negative indices. <code>NA</code> and
zero values are allowed: rows in an index matrix containing a zero are
ignored, whereas rows containing an <code>NA</code> produce an <code>NA</code> in
the result.
</p>
<p>Both in the case of using a single
<a name="index-index-5"></a>
index and in matrix indexing, a <code>names</code> attribute is used if
present, as had the structure been one-dimensional.
</p>
<p>If an indexing operation causes the result to have one of its extents of
length one, as in selecting a single slice of a three-dimensional matrix
with (say) <code>m[2, , ]</code>, the corresponding dimension is generally
dropped from the result. If a single-dimensional structure results, a
vector is obtained. This is occasionally undesirable and can be turned
off by adding the &lsquo;<samp>drop = FALSE</samp>&rsquo; to the indexing operation. Notice
that this is an additional argument to the <code>[</code> function and doesn&rsquo;t
add to the index count. Hence the correct way of selecting the first
row of a matrix as a <em>1</em> by <em>n</em> matrix is <code>m[1, , drop =
FALSE]</code>. Forgetting to disable the dropping feature is a common cause
of failure in general subroutines where an index occasionally, but not
usually has length one. This rule still applies to a one-dimensional
array, where any subsetting will give a vector result unless &lsquo;<samp>drop
= FALSE</samp>&rsquo; is used.
</p>
<p>Notice that vectors are distinct from one-dimensional arrays in that the
latter have <code>dim</code> and <code>dimnames</code> attributes (both of length
one). One-dimensional arrays are not easily obtained from subsetting
operations but they can be constructed explicitly and are returned by
<code>table</code>. This is sometimes useful because the elements of the
<code>dimnames</code> list may themselves be named, which is not the case for
the <code>names</code> attribute.
</p>
<p>Some operations such as <code>m[FALSE, ]</code> result in structures in which
a dimension has zero extent. R generally tries to handle these
structures sensibly.
</p>
<hr>
<a name="Indexing-other-structures"></a>
<div class="header">
<p>
Next: <a href="#Subset-assignment" accesskey="n" rel="next">Subset assignment</a>, Previous: <a href="#Indexing-matrices-and-arrays" accesskey="p" rel="prev">Indexing matrices and arrays</a>, Up: <a href="#Indexing" accesskey="u" rel="up">Indexing</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Indexing-other-structures-1"></a>
<h4 class="subsection">3.4.3 Indexing other structures</h4>
<p>The operator <code>[</code> is a generic function which allows class methods
to be added, and the <code>$</code> and <code>[[</code> operators likewise. Thus,
it is possible to have user-defined indexing operations for any
structure. Such a function, say <code>[.foo</code> is called with a set of
arguments of which the first is the structure being indexed and the rest
are the indices. In the case of <code>$</code>, the index argument is of mode
<code>&quot;symbol&quot;</code> even when using the <code>x$&quot;abc&quot;</code> form. It is
important to be aware that class methods do not necessarily behave in
the same way as the basic methods, for example with respect to partial
matching.
</p>
<p>The most important example of a class method for <code>[</code> is that used
for data frames. It is not described in detail here (see the help
page for <code>[.data.frame</code>, but in broad terms, if two indices are
supplied (even if one is empty) it creates matrix-like indexing for a
structure that is basically a list of vectors of the same length. If a
single index is supplied, it is interpreted as indexing the list of
columns&mdash;in that case the <code>drop</code> argument is ignored, with a
warning.
</p>
<p>The basic operators <code>$</code> and <code>[[</code> can be applied to
environments. Only character indices are allowed and no partial
matching is done.
</p>
<hr>
<a name="Subset-assignment"></a>
<div class="header">
<p>
Previous: <a href="#Indexing-other-structures" accesskey="p" rel="prev">Indexing other structures</a>, Up: <a href="#Indexing" accesskey="u" rel="up">Indexing</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Subset-assignment-1"></a>
<h4 class="subsection">3.4.4 Subset assignment</h4>
<a name="index-assignment-3"></a>
<a name="index-complex-assignment"></a>
<p>Assignment to subsets of a structure is a special case of a general
mechanism for complex assignment:
</p><div class="example">
<pre class="example">x[3:5] &lt;- 13:15
</pre></div>
<p>The result of this command is as if the following had been executed
</p><div class="example">
<pre class="example">`*tmp*` &lt;- x
x &lt;- &quot;[&lt;-&quot;(`*tmp*`, 3:5, value=13:15)
rm(`*tmp*`)
</pre></div>
<p>Note that the index is first converted to a numeric index and then the
elements are replaced sequentially along the numeric index, as if a
<code>for</code> loop had been used. Any existing variable called
<code>`*tmp*`</code> will be overwritten and deleted, and this variable name
should not be used in code.
</p>
<p>The same mechanism can be applied to functions other than <code>[</code>. The
replacement function has the same name with <code>&lt;-</code> pasted on. Its last
argument, which must be called <code>value</code>, is the new value to be
assigned. For example,
</p><div class="example">
<pre class="example">names(x) &lt;- c(&quot;a&quot;,&quot;b&quot;)
</pre></div>
<p>is equivalent to
</p><div class="example">
<pre class="example">`*tmp*` &lt;- x
x &lt;- &quot;names&lt;-&quot;(`*tmp*`, value=c(&quot;a&quot;,&quot;b&quot;))
rm(`*tmp*`)
</pre></div>
<p>Nesting of complex assignments is evaluated recursively
</p><div class="example">
<pre class="example">names(x)[3] &lt;- &quot;Three&quot;
</pre></div>
<p>is equivalent to
</p><div class="example">
<pre class="example">`*tmp*` &lt;- x
x &lt;- &quot;names&lt;-&quot;(`*tmp*`, value=&quot;[&lt;-&quot;(names(`*tmp*`), 3, value=&quot;Three&quot;))
rm(`*tmp*`)
</pre></div>
<p>Complex assignments in the enclosing environment (using <code>&lt;&lt;-</code>) are
also permitted:
</p><div class="example">
<pre class="example">names(x)[3] &lt;&lt;- &quot;Three&quot;
</pre></div>
<p>is equivalent to
</p><div class="example">
<pre class="example">`*tmp*` &lt;&lt;- get(x, envir=parent.env(), inherits=TRUE)
names(`*tmp*`)[3] &lt;- &quot;Three&quot;
x &lt;&lt;- `*tmp*`
rm(`*tmp*`)
</pre></div>
<p>and also to
</p><div class="example">
<pre class="example">`*tmp*` &lt;- get(x,envir=parent.env(), inherits=TRUE)
x &lt;&lt;- &quot;names&lt;-&quot;(`*tmp*`, value=&quot;[&lt;-&quot;(names(`*tmp*`), 3, value=&quot;Three&quot;))
rm(`*tmp*`)
</pre></div>
<p>Only the target variable is evaluated in the enclosing environment, so
</p><div class="example">
<pre class="example">e&lt;-c(a=1,b=2)
i&lt;-1
local({
e &lt;- c(A=10,B=11)
i &lt;-2
e[i] &lt;&lt;- e[i]+1
})
</pre></div>
<p>uses the local value of <code>i</code> on both the LHS and RHS, and the local
value of <code>e</code> on the RHS of the superassignment statement. It sets
<code>e</code> in the outer environment to
</p><div class="example">
<pre class="example"> a b
1 12
</pre></div>
<p>That is, the superassignment is equivalent to the four lines
</p><div class="example">
<pre class="example">`*tmp*` &lt;- get(e, envir=parent.env(), inherits=TRUE)
`*tmp*`[i] &lt;- e[i]+1
e &lt;&lt;- `*tmp*`
rm(`*tmp*`)
</pre></div>
<p>Similarly
</p><div class="example">
<pre class="example">x[is.na(x)] &lt;&lt;- 0
</pre></div>
<p>is equivalent to
</p><div class="example">
<pre class="example">`*tmp*` &lt;- get(x,envir=parent.env(), inherits=TRUE)
`*tmp*`[is.na(x)] &lt;- 0
x &lt;&lt;- `*tmp*`
rm(`*tmp*`)
</pre></div>
<p>and not to
</p><div class="example">
<pre class="example">`*tmp*` &lt;- get(x,envir=parent.env(), inherits=TRUE)
`*tmp*`[is.na(`*tmp*`)] &lt;- 0
x &lt;&lt;- `*tmp*`
rm(`*tmp*`)
</pre></div>
<p>These two candidate interpretations differ only if there is also a
local variable <code>x</code>. It is a good idea to avoid having a local
variable with the same name as the target variable of a
superassignment. As this case was handled incorrectly in versions
1.9.1 and earlier there must not be a serious need for such code.
</p>
<hr>
<a name="Scope-of-variables"></a>
<div class="header">
<p>
Previous: <a href="#Indexing" accesskey="p" rel="prev">Indexing</a>, Up: <a href="#Evaluation-of-expressions" accesskey="u" rel="up">Evaluation of expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Scope-of-variables-1"></a>
<h3 class="section">3.5 Scope of variables</h3>
<a name="index-scope"></a>
<a name="index-name-4"></a>
<p>Almost every programming language has a set of scoping rules, allowing
the same name to be used for different objects. This allows, e.g., a
local variable in a function to have the same name as a global object.
</p>
<p>R uses a <em>lexical scoping</em> model, similar to languages like
Pascal. However, R is a <em>functional programming language</em> and
allows dynamic creation and manipulation of functions and language
objects, and has additional features reflecting this fact.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Global-environment" accesskey="1">Global environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Lexical-environment" accesskey="2">Lexical environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Stacks" accesskey="3">Stacks</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Search-path" accesskey="4">Search path</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Global-environment"></a>
<div class="header">
<p>
Next: <a href="#Lexical-environment" accesskey="n" rel="next">Lexical environment</a>, Previous: <a href="#Scope-of-variables" accesskey="p" rel="prev">Scope of variables</a>, Up: <a href="#Scope-of-variables" accesskey="u" rel="up">Scope of variables</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Global-environment-1"></a>
<h4 class="subsection">3.5.1 Global environment</h4>
<p>The global
<a name="index-environment-5"></a>
environment is the root of the user workspace. An
<a name="index-assignment-4"></a>
assignment operation from the command line will cause the relevant
object to belong to the global environment. Its enclosing environment
is the next environment on the search path, and so on back to the
empty environment that is the enclosure of the base environment.
</p>
<hr>
<a name="Lexical-environment"></a>
<div class="header">
<p>
Next: <a href="#Stacks" accesskey="n" rel="next">Stacks</a>, Previous: <a href="#Global-environment" accesskey="p" rel="prev">Global environment</a>, Up: <a href="#Scope-of-variables" accesskey="u" rel="up">Scope of variables</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Lexical-environment-1"></a>
<h4 class="subsection">3.5.2 Lexical environment</h4>
<p>Every call to a
<a name="index-function-8"></a>
function creates a
<a name="index-frame"></a>
<a name="index-environment-6"></a>
<em>frame</em> which contains the local
variables created in the function, and is evaluated in an environment,
which in combination creates a new environment.
</p>
<p>Notice the terminology: A frame is a set of variables, an environment is
a nesting of frames (or equivalently: the innermost frame plus the
enclosing environment).
</p>
<p>Environments may be assigned to variables or be contained in other
objects. However, notice that they are not standard objects&mdash;in
particular, they are not copied on assignment.
</p>
<p>A closure (mode <code>&quot;function&quot;</code>) object will contain the environment
in which it is created as part of its definition (By default. The
environment can be manipulated using <code>environment&lt;-</code>). When the
function is subsequently called, its
<a name="index-environment_002c-evaluation"></a>
evaluation environment is created with the closure&rsquo;s environment as
enclosure. Notice that this is not
necessarily the environment of the caller!
</p>
<p>Thus, when a variable is requested inside a
<a name="index-function-9"></a>
function, it is first sought
in the
<a name="index-environment_002c-evaluation-1"></a>
evaluation environment, then in the enclosure, the enclosure of
the enclosure, etc.; once the global environment or the environment of
a package is reached, the
search continues up the search path
to the environment of the base package. If the variable is not
found there, the search will proceed next to the empty environment, and
will fail.
</p>
<hr>
<a name="Stacks"></a>
<div class="header">
<p>
Next: <a href="#Search-path" accesskey="n" rel="next">Search path</a>, Previous: <a href="#Lexical-environment" accesskey="p" rel="prev">Lexical environment</a>, Up: <a href="#Scope-of-variables" accesskey="u" rel="up">Scope of variables</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-call-stack"></a>
<h4 class="subsection">3.5.3 The call stack</h4>
<p>Every time a
<a name="index-function-10"></a>
function is invoked a new evaluation frame is created. At
any point in time during the computation the currently active
environments are accessible through the <em>call stack</em>. Each time a
function is invoked a special construct called a context is created
internally and is placed on a list of contexts. When a function has
finished evaluating its context is removed from the call stack.
</p>
<p>Making variables defined higher up the call stack available is called
<a name="index-scope-1"></a>
dynamic scope. The binding for a variable is then determined by the most
recent (in time) definition of the variable. This contradicts the
default scoping rules in R, which use the bindings in the
<a name="index-environment-7"></a>
environment
in which the function was defined (lexical scope). Some functions,
particularly those that use and manipulate model formulas, need to
simulate dynamic scope by directly accessing the call stack.
</p>
<p>Access to the
<a name="index-call-stack"></a>
call stack is provided through a family of functions which
have names that start with &lsquo;<samp>sys.</samp>&rsquo;. They are listed briefly below.
</p>
<a name="index-evaluation"></a>
<dl compact="compact">
<dt><code>sys.call</code></dt>
<dd><p>Get the call for the specified context.
</p></dd>
<dt><code>sys.frame</code></dt>
<dd><p>Get the evaluation frame for the specified context.
</p></dd>
<dt><code>sys.nframe</code></dt>
<dd><p>Get the environment frame for all active contexts.
</p></dd>
<dt><code>sys.function</code></dt>
<dd><p>Get the function being invoked in the specified context.
</p></dd>
<dt><code>sys.parent</code></dt>
<dd><p>Get the parent of the current function invocation.
</p></dd>
<dt><code>sys.calls</code></dt>
<dd><p>Get the calls for all the active contexts.
</p></dd>
<dt><code>sys.frames</code></dt>
<dd><p>Get the evaluation frames for all the active contexts.
</p></dd>
<dt><code>sys.parents</code></dt>
<dd><p>Get the numeric labels for all active contexts.
</p></dd>
<dt><code>sys.on.exit</code></dt>
<dd><p>Set a function to be executed when the specified context is exited.
</p></dd>
<dt><code>sys.status</code></dt>
<dd><p>Calls <code>sys.frames</code>, <code>sys.parents</code> and <code>sys.calls</code>.
</p></dd>
<dt><code>parent.frame</code></dt>
<dd><p>Get the evaluation frame for the specified parent context.
</p></dd>
</dl>
<hr>
<a name="Search-path"></a>
<div class="header">
<p>
Previous: <a href="#Stacks" accesskey="p" rel="prev">Stacks</a>, Up: <a href="#Scope-of-variables" accesskey="u" rel="up">Scope of variables</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Search-path-1"></a>
<h4 class="subsection">3.5.4 Search path</h4>
<p>In addition to the evaluation
<a name="index-environment-8"></a>
<a name="index-search-path"></a>
environment structure, R has a search
path of environments which are searched for variables not found
elsewhere. This is used for two things: packages of functions and
attached user data.
</p>
<p>The first element of the search path is the global environment and the
last is the base package. An <code>Autoloads</code> environment is used for
holding proxy objects that may be loaded on demand. Other environments
are inserted in the path using <code>attach</code> or <code>library</code>.
</p>
<a name="index-namespace"></a>
<p>Packages which have a <em>namespace</em> have a different search path.
When a search for an R object is started from an object in such a
package, the package itself is searched first, then its imports, then
the base namespace and finally the global environment and the rest of the
regular search path. The effect is that references to other objects in
the same package will be resolved to the package, and objects cannot be
masked by objects of the same name in the global environment or in other
packages.
</p>
<hr>
<a name="Functions"></a>
<div class="header">
<p>
Next: <a href="#Object_002doriented-programming" accesskey="n" rel="next">Object-oriented programming</a>, Previous: <a href="#Evaluation-of-expressions" accesskey="p" rel="prev">Evaluation of expressions</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Functions-1"></a>
<h2 class="chapter">4 Functions</h2>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Writing-functions" accesskey="1">Writing functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Functions-as-objects" accesskey="2">Functions as objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Evaluation" accesskey="3">Evaluation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Writing-functions"></a>
<div class="header">
<p>
Next: <a href="#Functions-as-objects" accesskey="n" rel="next">Functions as objects</a>, Previous: <a href="#Functions" accesskey="p" rel="prev">Functions</a>, Up: <a href="#Functions" accesskey="u" rel="up">Functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Writing-functions-1"></a>
<h3 class="section">4.1 Writing functions</h3>
<p>While R can be very useful as a data analysis tool most users very
quickly find themselves wanting to write their own
<a name="index-function-11"></a>
functions. This is
one of the real advantages of R. Users can program it and they can,
if they want to, change the system level functions to functions that
they find more appropriate.
</p>
<p>R also provides facilities that make it easy to document any
functions that you have created. See <a href="./R-exts.html#Writing-R-documentation">Writing R documentation</a> in <cite>Writing R Extensions</cite>.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Syntax-and-examples" accesskey="1">Syntax and examples</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Arguments" accesskey="2">Arguments</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Syntax-and-examples"></a>
<div class="header">
<p>
Next: <a href="#Arguments" accesskey="n" rel="next">Arguments</a>, Previous: <a href="#Writing-functions" accesskey="p" rel="prev">Writing functions</a>, Up: <a href="#Writing-functions" accesskey="u" rel="up">Writing functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Syntax-and-examples-1"></a>
<h4 class="subsection">4.1.1 Syntax and examples</h4>
<p>The syntax for writing a
<a name="index-function-12"></a>
function is
</p>
<div class="example">
<pre class="example">function ( <var>arglist</var> ) <var>body</var>
</pre></div>
<p>The first component of the function declaration is the keyword
<code>function</code> which indicates to R that you want to create a
function.
</p>
<p>An
<a name="index-argument-1"></a>
argument list is a comma separated list of formal arguments. A
formal argument can be a symbol, a statement of the form
&lsquo;<samp><var>symbol</var> = <var>expression</var></samp>&rsquo;, or the special formal argument
&lsquo;<samp>...</samp>&rsquo;.
</p>
<p>The <em>body</em> can be any valid R expression. Generally, the body
is a group of expressions contained in curly braces (&lsquo;<samp>{</samp>&rsquo; and
&lsquo;<samp>}</samp>&rsquo;).
</p>
<p>Generally
<a name="index-function-13"></a>
functions are assigned to symbols but they don&rsquo;t need to be.
The value returned by the call to <code>function</code> is a function. If
this is not given a name it is referred to as an
<a name="index-function_002c-anonymous"></a>
anonymous
function. Anonymous functions are most frequently used as arguments to
other functions such as the <code>apply</code> family or <code>outer</code>.
</p>
<p>Here is a simple function: <code>echo &lt;- function(x) print(x)</code>. So
<code>echo</code> is a function that takes a single argument and when
<code>echo</code> is invoked it prints its argument.
</p>
<hr>
<a name="Arguments"></a>
<div class="header">
<p>
Previous: <a href="#Syntax-and-examples" accesskey="p" rel="prev">Syntax and examples</a>, Up: <a href="#Writing-functions" accesskey="u" rel="up">Writing functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Arguments-1"></a>
<h4 class="subsection">4.1.2 Arguments</h4>
<p>The formal arguments to the function define the variables whose values
will be supplied at the time the function is invoked. The names of
these arguments can be used within the function body where they obtain
the value supplied at the time of function invocation.
</p>
<a name="index-argument_002c-default-values"></a>
<p>Default values for arguments can be specified using the special form
&lsquo;<samp><var>name</var> = <var>expression</var></samp>&rsquo;. In this case, if the user does
not specify a value for the argument when the function is invoked the
expression will be associated with the corresponding symbol. When a
value is needed the <var>expression</var> is
<a name="index-evaluation_002c-expression-2"></a>
evaluated in the evaluation
frame of the function.
</p>
<p>Default behaviours can also be specified by using the function
<code>missing</code>. When <code>missing</code> is called with the
<a name="index-name-5"></a>
name of a formal
argument it returns <code>TRUE</code> if the formal argument was not matched
with any actual argument and has not been subsequently modified in the
body of the function. An argument that is <code>missing</code> will thus
have its default value, if any. The <code>missing</code> function does not
force evaluation of the argument.
</p>
<p>The special type of argument &lsquo;<samp>...</samp>&rsquo; can contain any number of
supplied arguments. It is used for a variety of purposes. It allows
you to write a
<a name="index-function-14"></a>
function that takes an arbitrary number of arguments. It
can be used to absorb some arguments into an intermediate function which
can then be extracted by functions called subsequently.
</p>
<hr>
<a name="Functions-as-objects"></a>
<div class="header">
<p>
Next: <a href="#Evaluation" accesskey="n" rel="next">Evaluation</a>, Previous: <a href="#Writing-functions" accesskey="p" rel="prev">Writing functions</a>, Up: <a href="#Functions" accesskey="u" rel="up">Functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Functions-as-objects-1"></a>
<h3 class="section">4.2 Functions as objects</h3>
<p>Functions are first class objects in R. They can be used anywhere
that an R object is required. In particular they can be passed as
arguments to functions and returned as values from functions. See
<a href="#Function-objects">Function objects</a> for the details.
</p>
<hr>
<a name="Evaluation"></a>
<div class="header">
<p>
Previous: <a href="#Functions-as-objects" accesskey="p" rel="prev">Functions as objects</a>, Up: <a href="#Functions" accesskey="u" rel="up">Functions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Evaluation-1"></a>
<h3 class="section">4.3 Evaluation</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Evaluation-environment" accesskey="1">Evaluation environment</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Argument-matching" accesskey="2">Argument matching</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Argument-evaluation" accesskey="3">Argument evaluation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Scope" accesskey="4">Scope</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Evaluation-environment"></a>
<div class="header">
<p>
Next: <a href="#Argument-matching" accesskey="n" rel="next">Argument matching</a>, Previous: <a href="#Evaluation" accesskey="p" rel="prev">Evaluation</a>, Up: <a href="#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Evaluation-environment-1"></a>
<h4 class="subsection">4.3.1 Evaluation environment</h4>
<p>When a
<a name="index-function-15"></a>
function is called or invoked a new
<a name="index-evaluation-1"></a>
evaluation frame is created.
In this frame the formal arguments are matched with the supplied
arguments according to the rules given in <a href="#Argument-matching">Argument matching</a>. The
statements in the body of the function are evaluated sequentially in
this
<a name="index-environment-9"></a>
environment frame.
</p>
<p>The enclosing frame of the evaluation frame is the environment frame
associated with the function being invoked. This may be different from
S. While many functions have <code>.GlobalEnv</code> as their environment
this does not have to be true and functions defined in packages with
namespaces (normally) have the package namespace as their environment.
</p>
<hr>
<a name="Argument-matching"></a>
<div class="header">
<p>
Next: <a href="#Argument-evaluation" accesskey="n" rel="next">Argument evaluation</a>, Previous: <a href="#Evaluation-environment" accesskey="p" rel="prev">Evaluation environment</a>, Up: <a href="#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Argument-matching-1"></a>
<h4 class="subsection">4.3.2 Argument matching</h4>
<p>This subsection applies to closures but not to primitive functions. The
latter typically ignore tags and do positional matching, but their help
pages should be consulted for exceptions, which include <code>log</code>,
<code>round</code>, <code>signif</code>, <code>rep</code> and <code>seq.int</code>.
</p>
<p>The first thing that occurs in a
<a name="index-function-16"></a>
function evaluation is the matching of
formal to the actual or supplied arguments.
This is done by a three-pass process:
</p>
<ol>
<li> <strong>Exact matching on tags</strong>.
<a name="index-name-6"></a>
For each named supplied argument the list of formal arguments is
searched for an item whose name matches exactly. It is an error to have
the same formal argument match several actuals or vice versa.
</li><li> <strong>Partial matching on tags</strong>.
Each remaining named supplied argument is compared to the remaining formal
arguments using partial matching. If the name of the supplied argument
matches exactly with the first part of a formal argument then the two
arguments are considered to be matched. It is an error to have multiple
partial matches. Notice that if <code>f &lt;- function(fumble,
fooey) fbody</code>, then <code>f(f = 1, fo = 2)</code> is illegal, even though the
2nd actual argument only matches <code>fooey</code>. <code>f(f = 1, fooey =
2)</code> <em>is</em> legal though since the second argument matches exactly and
is removed from consideration for partial matching. If the formal
arguments contain &lsquo;<samp>...</samp>&rsquo; then partial matching is only applied to
arguments that precede it.
</li><li> <strong>Positional matching</strong>.
Any unmatched formal arguments are bound to <em>unnamed</em> supplied
arguments, in order. If there is a &lsquo;<samp>...</samp>&rsquo; argument, it will take up
the remaining arguments, tagged or not.
</li></ol>
<p>If any arguments remain unmatched an error is declared.
</p>
<p>Argument matching is augmented by the functions <code>match.arg</code>,
<code>match.call</code> and <code>match.fun</code>.
<a name="index-match_002earg"></a>
<a name="index-match_002ecall"></a>
<a name="index-match_002efun"></a>
Access to the partial matching algorithm used by R is via
<code>pmatch</code>.
</p>
<hr>
<a name="Argument-evaluation"></a>
<div class="header">
<p>
Next: <a href="#Scope" accesskey="n" rel="next">Scope</a>, Previous: <a href="#Argument-matching" accesskey="p" rel="prev">Argument matching</a>, Up: <a href="#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Argument-evaluation-1"></a>
<h4 class="subsection">4.3.3 Argument evaluation</h4>
<p>One of the most important things to know about the
<a name="index-evaluation_002c-argument"></a>
evaluation of
arguments to a
<a name="index-function-17"></a>
function is that supplied arguments and default arguments
are treated differently. The supplied arguments to a function are
evaluated in the evaluation frame of the calling function. The default
arguments to a function are evaluated in the evaluation frame of the
function.
</p>
<p>The semantics of invoking a function in R argument are
<em>call-by-value</em>. In general, supplied arguments behave as if they
are local variables initialized with the value supplied and the
<a name="index-name-7"></a>
name of
the corresponding formal argument. Changing the value of a supplied
argument within a function will not affect the value of the variable in
the calling frame.
</p>
<p>R has a form of lazy evaluation of function arguments. Arguments are
not evaluated until needed. It is important to realize that in some
cases the argument will never be evaluated. Thus, it is bad style to
use arguments to functions to cause side-effects. While in <strong>C</strong> it is
common to use the form, <code>foo(x = y)</code> to invoke <code>foo</code> with the
value of <code>y</code> and simultaneously to assign the value of <code>y</code> to
<code>x</code> this same style should not be used in R. There is no
guarantee that the argument will ever be evaluated and hence the
<a name="index-assignment-5"></a>
assignment may not take place.
</p>
<p>It is also worth noting that the effect of <code>foo(x &lt;- y)</code> if the
argument is evaluated is to change the value of <code>x</code> in the calling
<a name="index-environment-10"></a>
environment and not in the
<a name="index-environment_002c-evaluation-2"></a>
evaluation environment of <code>foo</code>.
</p>
<p>It is possible to access the actual (not default) expressions used as
arguments inside the function. The mechanism is implemented via
promises. When a
<a name="index-function-18"></a>
function is being evaluated the actual expression used as an argument is
stored in the promise together with a pointer to the environment the
function was called from. When (if) the argument is evaluated the
stored expression is evaluated in the environment that the function was
called from. Since only a pointer to the environment is used any
changes made to that environment will be in effect during this
evaluation. The resulting value is then also stored in a separate spot
in the promise. Subsequent evaluations retrieve this stored value (a
second evaluation is not carried out). Access to the unevaluated
expression is also available using <code>substitute</code>.
</p>
<p>When a
<a name="index-function-19"></a>
function is called, each formal argument is assigned a promise in the
local environment of the call with the expression slot containing the
actual argument (if it exists) and the environment slot containing the
environment of the caller. If no actual argument for a formal argument
is given in the call and there is a default expression, it is similarly
assigned to the expression slot of the formal argument, but with the
<a name="index-environment-11"></a>
environment set
to the local environment.
</p>
<p>The process of filling the value slot of a promise by
<a name="index-evaluation-2"></a>
evaluating the
contents of the expression slot in the promise&rsquo;s environment is called
<em>forcing</em> the promise. A promise will only be forced once, the
value slot content being used directly later on.
</p>
<p>A promise is forced when its value is needed. This usually happens
inside internal
<a name="index-function-20"></a>
<a name="index-function_002c-internal"></a>
functions, but a promise can also be forced by direct evaluation of the
promise itself. This is occasionally useful when a default expression
depends on the value of another formal argument or other variable in the
local environment. This is seen in the following example where the lone
<code>label</code> ensures that the label is based on the value of <code>x</code>
before it is changed in the next line.
</p>
<div class="example">
<pre class="example">function(x, label = deparse(x)) {
label
x &lt;- x + 1
print(label)
}
</pre></div>
<p>The expression slot of a promise can itself involve other promises.
This happens whenever an unevaluated argument is passed as an argument
to another function. When forcing a promise, other promises in its
expression will also be forced recursively as they are evaluated.
</p>
<hr>
<a name="Scope"></a>
<div class="header">
<p>
Previous: <a href="#Argument-evaluation" accesskey="p" rel="prev">Argument evaluation</a>, Up: <a href="#Evaluation" accesskey="u" rel="up">Evaluation</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Scope-1"></a>
<h4 class="subsection">4.3.4 Scope</h4>
<a name="index-scope-2"></a>
<p>Scope or the scoping rules are simply the set of rules used by the
<a name="index-evaluation_002c-symbol-2"></a>
evaluator to find a value for a
<a name="index-symbol-3"></a>
symbol. Every computer language has a
set of such rules. In R the rules are fairly simple but there do
exist mechanisms for subverting the usual, or default rules.
</p>
<p>R adheres to a set of rules that are called <em>lexical scope</em>.
This means the variable
<a name="index-binding"></a>
bindings in effect at the time the expression
was created are used to provide values for any unbound symbols in the
expression.
</p>
<p>Most of the interesting properties of
<a name="index-scope-3"></a>
scope are involved with evaluating
<a name="index-function-21"></a>
functions and we concentrate on this issue. A symbol can be either
<a name="index-binding-1"></a>
bound or unbound. All of the formal arguments to a function provide
bound symbols in the body of the function. Any other symbols in the
body of the function are either local variables or unbound variables. A
local variable is one that is defined within the function. Because R
has no formal definition of variables, they are simply used as needed,
it can be difficult to determine whether a variable is local or not.
Local variables must first be defined, this is typically done by having
them on the left-hand side of an
<a name="index-assignment-6"></a>
assignment.
</p>
<p>During the evaluation process if an unbound symbol is detected then R
attempts to find a value for it. The scoping rules determine how this
process proceeds. In R the
<a name="index-environment-12"></a>
environment of the function is searched
first, then its enclosure and so on until the global environment is reached.
</p>
<p>The global environment heads a search list of environments that are searched
sequentially for a matching symbol. The value of the first match is then used.
</p>
<p>When this set of rules is combined with the fact that
<a name="index-function-22"></a>
functions can be
returned as values from other functions then some rather nice, but at
first glance peculiar, properties obtain.
</p>
<p>A simple example:
</p>
<div class="example">
<pre class="example">f &lt;- function() {
y &lt;- 10
g &lt;- function(x) x + y
return(g)
}
h &lt;- f()
h(3)
</pre></div>
<a name="index-evaluation-3"></a>
<p>A rather interesting question is what happens when <code>h</code> is
evaluated. To describe this we need a bit more notation. Within a
<a name="index-function-23"></a>
function body variables can be bound, local or unbound. The bound
variables are those that match the formal arguments to the function.
The local variables are those that were created or defined within the
function body. The unbound variables are those that are neither local
nor bound. When a function body is evaluated there is no problem
determining values for local variables or for bound variables. Scoping
rules determine how the language will find values for the unbound
variables.
</p>
<p>When <code>h(3)</code> is evaluated we see that its body is that of
<code>g</code>. Within that body <code>x</code> is bound to the formal argument
and <code>y</code> is unbound. In a language with
<a name="index-scope-4"></a>
lexical scope <code>x</code> will be associated with the value 3 and
<code>y</code> with the value 10 local to <code>f</code> so <code>h(3)</code> should return the value 13.
In R this is indeed what happens.
</p>
<p>In S, because of the different scoping rules one will get an error
indicating that <code>y</code> is not found, unless there is a variable
<code>y</code> in your workspace in which case its value will be used.
</p>
<hr>
<a name="Object_002doriented-programming"></a>
<div class="header">
<p>
Next: <a href="#Computing-on-the-language" accesskey="n" rel="next">Computing on the language</a>, Previous: <a href="#Functions" accesskey="p" rel="prev">Functions</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Object_002doriented-programming-1"></a>
<h2 class="chapter">5 Object-oriented programming</h2>
<a name="index-object_002doriented"></a>
<p>Object-oriented programming is a style of programming that has become
popular in recent years. Much of the popularity comes from the fact
that it makes it easier to write and maintain complicated systems. It
does this through several different mechanisms.
</p>
<p>Central to any object-oriented language are the concepts of class and of
methods. A <em>class</em> is a definition of an object. Typically a
class contains several <em>slots</em> that are used to hold class-specific
information. An object in the language must be an instance of some
class. Programming is based on objects or instances of classes.
</p>
<p>Computations are carried out via <em>methods</em>. Methods are basically
<a name="index-function-24"></a>
functions that are specialized to carry out specific calculations on
objects, usually of a specific class. This is what makes the language
object oriented. In R, <em>generic functions</em> are used to
determine the appropriate method. The generic function is responsible
for determining the class of its argument(s) and uses that information
to select the appropriate method.
</p>
<p>Another feature of most object-oriented languages is the concept of
inheritance. In most programming problems there are usually many
objects that are related to one another. The programming is
considerably simplified if some components can be reused.
</p>
<p>If a class inherits from another class then generally it gets all the
slots in the parent class and can extend it by adding new slots. On
method dispatching (via the generic functions) if a method for the class
does not exist then a method for the parent is sought.
</p>
<p>In this chapter we discuss how this general strategy has been
implemented in R and discuss some of the limitations within the
current design. One of the advantages that most object systems impart
is greater consistency. This is achieved via the rules that are checked
by the compiler or interpreter. Unfortunately because of the way that
the object system is incorporated into R this advantage does not
obtain. Users are cautioned to use the object system in a
straightforward manner. While it is possible to perform some rather
interesting feats these tend to lead to obfuscated code and may depend
on implementation details that will not be carried forward.
</p>
<p>The greatest use of object oriented programming in R is through
<code>print</code> methods, <code>summary</code> methods and <code>plot</code> methods.
These methods allow us to have one generic
<a name="index-function_002c-generic"></a>
function call, <code>plot</code>
say, that dispatches on the type of its argument and calls a plotting
function that is specific to the data supplied.
</p>
<p>In order to make the concepts clear we will consider the implementation
of a small system designed to teach students about probability. In this
system the objects are probability functions and the methods we will
consider are methods for finding moments and for plotting.
Probabilities can always be represented in terms of the cumulative
distribution function but can often be represented in other ways. For
example as a density, when it exists or as a moment generating function
when it exists.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Definition" accesskey="1">Definition</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Inheritance" accesskey="2">Inheritance</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Method-dispatching" accesskey="3">Method dispatching</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#UseMethod" accesskey="4">UseMethod</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#NextMethod" accesskey="5">NextMethod</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Group-methods" accesskey="6">Group methods</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Writing-methods" accesskey="7">Writing methods</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Definition"></a>
<div class="header">
<p>
Next: <a href="#Inheritance" accesskey="n" rel="next">Inheritance</a>, Previous: <a href="#Object_002doriented-programming" accesskey="p" rel="prev">Object-oriented programming</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Definition-1"></a>
<h3 class="section">5.1 Definition</h3>
<p>Rather than having a full-fledged
<a name="index-object_002doriented-1"></a>
object-oriented system R has a
class system and a mechanism for dispatching based on the class of an
object. The dispatch mechanism for interpreted code relies on four
special objects that are stored in the evaluation frame. These special
objects are <code>.Generic</code>, <code>.Class</code>, <code>.Method</code> and
<code>.Group</code>. There is a separate dispatch mechanism used for internal
functions and types that will be discussed elsewhere.
</p>
<p>The class system is facilitated through the <code>class</code> attribute.
This attribute is a character vector of class names. So to create an
object of class <code>&quot;foo&quot;</code> one simply attaches a class attribute with
the string &lsquo;<samp>&quot;foo&quot;</samp>&rsquo; in it. Thus, virtually anything can be turned
in to an object of class <code>&quot;foo&quot;</code>.
</p>
<p>The object system makes use of
<a name="index-function_002c-generic-1"></a>
<em>generic functions</em> via two
dispatching functions, <code>UseMethod</code> and <code>NextMethod</code>. The
typical use of the object system is to begin by calling a generic
function. This is typically a very simple function and consists of a
single line of code. The system function <code>mean</code> is just such a
function,
</p>
<div class="example">
<pre class="example">&gt; mean
function (x, ...)
UseMethod(&quot;mean&quot;)
</pre></div>
<p>When <code>mean</code> is called it can have any number of arguments but its
first argument is special and the class of that first argument is used
to determine which method should be called. The variable <code>.Class</code>
is set to the class attribute of <code>x</code>, <code>.Generic</code> is set to the
string <code>&quot;mean&quot;</code> and a search is made for the correct method to
invoke. The class attributes of any other arguments to <code>mean</code> are
ignored.
</p>
<p>Suppose that <code>x</code> had a class attribute that contained <code>&quot;foo&quot;</code>
and <code>&quot;bar&quot;</code>, in that order. Then R would first search for a
function called <code>mean.foo</code> and if it did not find one it would then
search for a function <code>mean.bar</code> and if that search was also
unsuccessful then a final search for <code>mean.default</code> would be made.
If the last search is unsuccessful R reports an error. It is a good
idea to always write a default method. Note that the functions
<code>mean.foo</code> etc. are referred to, in this context, as methods.
</p>
<p><code>NextMethod</code> provides another mechanism for dispatching. A
<a name="index-function-25"></a>
function may have a call to <code>NextMethod</code> anywhere in it. The
determination of which method should then be invoked is based primarily
on the current values of <code>.Class</code> and <code>.Generic</code>. This is
somewhat problematic since the method is really an ordinary function and
users may call it directly. If they do so then there will be no values
for <code>.Generic</code> or <code>.Class</code>.
</p>
<p>If a method is invoked directly and it contains a call to
<code>NextMethod</code> then the first argument to <code>NextMethod</code> is used
to determine the
<a name="index-function_002c-generic-2"></a>
generic function. An error is signalled if this
argument has not been supplied; it is therefore a good idea to always
supply this argument.
</p>
<p>In the case that a method is invoked directly the class attribute of the
first argument to the method is used as the value of <code>.Class</code>.
</p>
<p>Methods themselves employ <code>NextMethod</code> to provide a form of
inheritance. Commonly a specific method performs a few operations to
set up the data and then it calls the next appropriate method through a
call to <code>NextMethod</code>.
</p>
<p>Consider the following simple example. A point in two-dimensional
Euclidean space can be specified by its Cartesian (x-y) or polar
(r-theta) coordinates. Hence, to store information about the location
of the point, we could define two classes, <code>&quot;xypoint&quot;</code> and
<code>&quot;rthetapoint&quot;</code>. All the &lsquo;xypoint&rsquo; data structures are lists with
an x-component and a y-component. All &lsquo;rthetapoint&rsquo; objects are lists
with an r-component and a theta-component.
</p>
<p>Now, suppose we want to get the x-position from either type of object.
This can easily be achieved through
<a name="index-function_002c-generic-3"></a>
generic functions. We define the
generic function <code>xpos</code> as follows.
</p>
<div class="example">
<pre class="example">xpos &lt;- function(x, ...)
UseMethod(&quot;xpos&quot;)
</pre></div>
<p>Now we can define methods:
</p>
<div class="example">
<pre class="example">xpos.xypoint &lt;- function(x) x$x
xpos.rthetapoint &lt;- function(x) x$r * cos(x$theta)
</pre></div>
<p>The user simply calls the function <code>xpos</code> with either
representation as the argument. The internal dispatching method finds
the class of the object and calls the appropriate methods.
</p>
<p>It is pretty easy to add other representations. One need not write a
new generic function only the methods. This makes it easy to add to
existing systems since the user is only responsible for dealing with the
new representation and not with any of the existing representations.
</p>
<p>The bulk of the uses of this methodology are to provide specialized
printing for objects of different types; there are about 40 methods for
<code>print</code>.
</p>
<hr>
<a name="Inheritance"></a>
<div class="header">
<p>
Next: <a href="#Method-dispatching" accesskey="n" rel="next">Method dispatching</a>, Previous: <a href="#Definition" accesskey="p" rel="prev">Definition</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Inheritance-1"></a>
<h3 class="section">5.2 Inheritance</h3>
<a name="index-evaluation-4"></a>
<p>The class attribute of an object can have several elements. When a
<a name="index-function_002c-generic-4"></a>
generic function is called the first inheritance is mainly handled
through <code>NextMethod</code>. <code>NextMethod</code> determines the method
currently being evaluated, finds the next class from th
</p>
<p>FIXME: something is missing here
</p>
<hr>
<a name="Method-dispatching"></a>
<div class="header">
<p>
Next: <a href="#UseMethod" accesskey="n" rel="next">UseMethod</a>, Previous: <a href="#Inheritance" accesskey="p" rel="prev">Inheritance</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Method-dispatching-1"></a>
<h3 class="section">5.3 Method dispatching</h3>
<a name="index-function_002c-generic-5"></a>
<p>Generic functions should consist of a single statement. They should
usually be of the form <code>foo &lt;- function(x, ...) UseMethod(&quot;foo&quot;,
x)</code>. When <code>UseMethod</code> is called, it determines the appropriate
method and then that method is invoked with the same arguments, in
the same order as the call to the generic, as if the call had been made
directly to the method.
</p>
<p>In order to determine the correct method the class attribute of the
first argument to the generic is obtained and used to find the correct
method. The
<a name="index-name-8"></a>
name of the generic function is combined with the first element of the
class attribute into the form, <code><var>generic</var>.<var>class</var></code> and a
function with that name is sought. If the function is found then it is
used. If no such function is found then the second element of the class
attribute is used, and so on until all the elements of the class
attribute have been exhausted. If no method has been found at that
point then the method <code><var>generic</var>.<var>default</var></code> is used. If
the first argument to the generic function has no class attribute then
<code><var>generic</var>.<var>default</var></code> is used. Since the introduction of
namespaces the methods may not be accessible by their names
(i.e. <code>get(&quot;<var>generic</var>.<var>class</var>&quot;)</code> may fail), but they will
be accessible by <code>getS3method(&quot;<var>generic</var>&quot;,&quot;<var>class</var>&quot;)</code>.
</p>
<a name="index-object-4"></a>
<p>Any object can have a <code>class</code> attribute. This attribute can have
any number of elements. Each of these is a string that defines a class.
When a generic function is invoked the class of its first argument is
examined.
</p>
<hr>
<a name="UseMethod"></a>
<div class="header">
<p>
Next: <a href="#NextMethod" accesskey="n" rel="next">NextMethod</a>, Previous: <a href="#Method-dispatching" accesskey="p" rel="prev">Method dispatching</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="UseMethod-1"></a>
<h3 class="section">5.4 UseMethod</h3>
<a name="index-UseMethod"></a>
<p><code>UseMethod</code> is a special function and it behaves differently from
other function calls. The syntax of a call to it is
<code>UseMethod(<var>generic</var>, <var>object</var>)</code>, where <var>generic</var> is
the name of the generic function, <var>object</var> is the object used to
determine which method should be chosen. <code>UseMethod</code> can only be
called from the body of a function.
</p>
<a name="index-evaluation-5"></a>
<p><code>UseMethod</code> changes the evaluation model in two ways. First, when
it is invoked it determines the next method (function) to be called. It
then invokes that function using the current evaluation
<a name="index-environment-13"></a>
environment; this process will be described shortly. The second way in
which <code>UseMethod</code> changes the evaluation environment is that it
does not return control to the calling function. This means, that any
statements after a call to <code>UseMethod</code> are guaranteed not to be
executed.
</p>
<p>When <code>UseMethod</code> is invoked the generic function is the specified
value in the call to <code>UseMethod</code>. The object to dispatch on is
either the supplied second argument or the first argument to the current
function. The class of the argument is determined and the first element
of it is combined with the name of the generic to determine the
appropriate method. So, if the generic had name <code>foo</code> and the
class of the object is <code>&quot;bar&quot;</code>, then R will search for a method
named <code>foo.bar</code>. If no such method exists then the inheritance
mechanism described above is used to locate an appropriate method.
</p>
<p>Once a method has been determined R invokes it in a special way.
Rather than creating a new evaluation
<a name="index-environment-14"></a>
environment R uses the
environment of the current function call (the call to the generic). Any
<a name="index-assignment-7"></a>
assignments or evaluations that were made before the call to
<code>UseMethod</code> will be in effect. The arguments that were used in the
call to the generic are rematched to the formal arguments of the
selected method.
</p>
<p>When the method is invoked it is called with arguments that are the same
in number and have the same names as in the call to the generic. They
are matched to the arguments of the method according to the standard
R rules for argument matching. However the object, i.e. the first
argument has been evaluated.
</p>
<p>The call to <code>UseMethod</code> has the effect of placing some special
objects in the evaluation frame. They are <code>.Class</code>,
<code>.Generic</code> and <code>.Method</code>. These special objects are used to
by R to handle the method dispatch and inheritance. <code>.Class</code> is
the class of the object, <code>.Generic</code> is the name of the generic
function and <code>.Method</code> is the name of the method currently being
invoked. If the method was invoked through one of the internal
interfaces then there may also be an object called <code>.Group</code>. This
will be described in Section <a href="#Group-methods">Group methods</a>. After the initial
call to <code>UseMethod</code> these special variables, not the object itself,
control the selection of subsequent methods.
</p>
<p>The body of the method is then evaluated in the standard fashion. In
particular variable look-up in the body follows the rules for the
method. So if the method has an associated environment then that is
used. In effect we have replaced the call to the generic by a call to
the method. Any local
<a name="index-assignment-8"></a>
assignments in the frame of the generic will be
carried forward into the call to the method. Use of this <em>feature</em>
is discouraged. It is important to realize that control will never
return to the generic and hence any expressions after a call to
<code>UseMethod</code> will never be executed.
</p>
<p>Any arguments to the generic that were evaluated prior to the call to
<code>UseMethod</code> remain evaluated.
</p>
<p>If the first argument to <code>UseMethod</code> is not supplied it is assumed
to be the name of the current function. If two arguments are supplied
to <code>UseMethod</code> then the first is the name of the method and the
second is assumed to be the object that will be dispatched on. It is
evaluated so that the required method can be determined. In this case
the first argument in the call to the generic is not evaluated and is
discarded. There is no way to change the other arguments in the call to
the method; these remain as they were in the call to the generic. This
is in contrast to <code>NextMethod</code> where the arguments in the call to
the next method can be altered.
</p>
<hr>
<a name="NextMethod"></a>
<div class="header">
<p>
Next: <a href="#Group-methods" accesskey="n" rel="next">Group methods</a>, Previous: <a href="#UseMethod" accesskey="p" rel="prev">UseMethod</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="NextMethod-1"></a>
<h3 class="section">5.5 NextMethod</h3>
<a name="index-NextMethod"></a>
<p><code>NextMethod</code> is used to provide a simple inheritance mechanism.
</p>
<p>Methods invoked as a result of a call to <code>NextMethod</code> behave as if
they had been invoked from the previous method. The arguments to the
inherited method are in the same order and have the same names as the
call to the current method. This means that they are the same as for
the call to the generic. However, the expressions for the arguments are
the names of the corresponding formal arguments of the current method.
Thus the arguments will have values that correspond to their value at
the time NextMethod was invoked.
</p>
<p>Unevaluated arguments remain unevaluated. Missing arguments remain
missing.
</p>
<p>The syntax for a call to <code>NextMethod</code> is <code>NextMethod(generic,
object, ...)</code>. If the <code>generic</code> is not supplied the value of
<code>.Generic</code> is used. If the <code>object</code> is not supplied the first
argument in the call to the current method is used. Values in the
&lsquo;<samp>...</samp>&rsquo; argument are used to modify the arguments of the next method.
</p>
<p>It is important to realize that the choice of the next method depends on
the current values of <code>.Generic</code> and <code>.Class</code> and not on the
object. So changing the object in a call to <code>NextMethod</code> affects
the arguments received by the next method but does not affect the choice
of the next method.
</p>
<p>Methods can be called directly. If they are then there will be no
<code>.Generic</code>, <code>.Class</code> or <code>.Method</code>. In this case the
<code>generic</code> argument of <code>NextMethod</code> must be specified. The
value of <code>.Class</code> is taken to be the class attribute of the object
which is the first argument to the current function. The value of
<code>.Method</code> is the name of the current function. These choices for
default values ensure that the behaviour of a method doesn&rsquo;t change
depending on whether it is called directly or via a call to a generic.
</p>
<p>An issue for discussion is the behaviour of the &lsquo;<samp>...</samp>&rsquo; argument to
<code>NextMethod</code>. The White Book describes the behaviour as follows:
</p>
<a name="index-name-9"></a>
<p>- named arguments replace the corresponding arguments in the call to
the current method. Unnamed arguments go at the start of the argument
list.
</p>
<p>What I would like to do is:
</p>
<p>-first do the argument matching for NextMethod;
-if the object or generic are changed fine
-first if a named list element matches an argument (named or not) the
list value replaces the argument value.
- the first unnamed list element
</p>
<p>Values for lookup:
Class: comes first from .Class, second from the first argument to the
method and last from the object specified in the call to NextMethod
</p>
<p>Generic: comes first from .Generic, if nothing then from the first
argument to the method and if it&rsquo;s still missing from the call to
NextMethod
</p>
<p>Method: this should just be the current function name.
</p>
<hr>
<a name="Group-methods"></a>
<div class="header">
<p>
Next: <a href="#Writing-methods" accesskey="n" rel="next">Writing methods</a>, Previous: <a href="#NextMethod" accesskey="p" rel="prev">NextMethod</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Group-methods-1"></a>
<h3 class="section">5.6 Group methods</h3>
<p>For several types of
<a name="index-function_002c-internal-1"></a>
internal functions R provides a dispatching
mechanism for operators. This means that operators such as <code>==</code> or
<code>&lt;</code> can have their behaviour modified for members of special
classes. The functions and operators have been grouped into three
categories and group methods can be written for each of these
categories. There is currently no mechanism to add groups. It is
possible to write methods specific to any function within a group.
</p>
<p>The following table lists the functions for the different Groups.
</p>
<dl compact="compact">
<dt>&lsquo;<samp>Math</samp>&rsquo;</dt>
<dd><p>abs, acos, acosh, asin, asinh, atan, atanh, ceiling, cos, cosh, cospi, cumsum,
exp, floor, gamma, lgamma, log, log10, round, signif, sin, sinh, sinpi,
tan, tanh, tanpi, trunc
</p>
</dd>
<dt>&lsquo;<samp>Summary</samp>&rsquo;</dt>
<dd><p>all, any, max, min, prod, range, sum
</p>
</dd>
<dt>&lsquo;<samp>Ops</samp>&rsquo;</dt>
<dd><p><code>+</code>, <code>-</code>, <code>*</code>, <code>/</code>, <code>^</code>, <code>&lt;</code> , <code>&gt;</code>,
<code>&lt;=</code>, <code>&gt;=</code>, <code>!=</code>, <code>==</code>, <code>%%</code>, <code>%/%</code>,
<code>&amp;</code>, <code>|</code>, <code>!</code>
</p></dd>
</dl>
<p>For operators in the Ops group a special method is invoked if the two
operands taken together suggest a single method. Specifically, if both
operands correspond to the same method or if one operand corresponds to
a method that takes precedence over that of the other operand. If they
do not suggest a single method then the default method is used. Either
a group method or a class method dominates if the other operand has no
corresponding method. A class method dominates a group method.
</p>
<p>When the group is Ops the special variable <code>.Method</code> is a string
vector with two elements. The elements of <code>.Method</code> are set to the
name of the method if the corresponding argument is a member of the
class that was used to determine the method. Otherwise the
corresponding element of <code>.Method</code> is set to the zero length
string, <code>&quot;&quot;</code>.
</p>
<hr>
<a name="Writing-methods"></a>
<div class="header">
<p>
Previous: <a href="#Group-methods" accesskey="p" rel="prev">Group methods</a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Writing-methods-1"></a>
<h3 class="section">5.7 Writing methods</h3>
<p>Users can easily write their own methods and generic functions. A
<a name="index-function_002c-generic-6"></a>
generic function is simply a function with a call to <code>UseMethod</code>.
A method is simply a function that has been invoked via method dispatch.
This can be as a result of a call to either <code>UseMethod</code> or
<code>NextMethod</code>.
</p>
<p>It is worth remembering that methods can be called directly. That means
that they can be entered without a call to <code>UseMethod</code> having been
made and hence the special variables <code>.Generic</code>, <code>.Class</code> and
<code>.Method</code> will not have been instantiated. In that case the
default rules detailed above will be used to determine these.
</p>
<p>The most common use of
<a name="index-function_002c-generic-7"></a>
generic functions is to provide <code>print</code> and
<code>summary</code> methods for statistical objects, generally the output of
some model fitting process. To do this, each model attaches a class
attribute to its output and then provides a special method that takes
that output and provides a nice readable version of it. The user then
needs only remember that <code>print</code> or <code>summary</code> will provide
nice output for the results of any analysis.
</p>
<hr>
<a name="Computing-on-the-language"></a>
<div class="header">
<p>
Next: <a href="#System-and-foreign-language-interfaces" accesskey="n" rel="next">System and foreign language interfaces</a>, Previous: <a href="#Object_002doriented-programming" accesskey="p" rel="prev">Object-oriented programming</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Computing-on-the-language-1"></a>
<h2 class="chapter">6 Computing on the language</h2>
<p>R belongs to a class of programming languages in which subroutines
have the ability to modify or construct other subroutines and evaluate
the result as an integral part of the language itself. This is similar
to Lisp and Scheme and other languages of the &ldquo;functional programming&rdquo;
variety, but in contrast to FORTRAN and the ALGOL family. The Lisp
family takes this feature to the extreme by the &ldquo;everything is a list&rdquo;
paradigm in which there is no distinction between programs and data.
</p>
<p>R presents a friendlier interface to programming than Lisp does, at
least to someone used to mathematical formulas and C-like control
structures, but the engine is really very Lisp-like. R allows direct
access to
<a name="index-parsing-3"></a>
parsed expressions and functions and allows you to alter and
subsequently execute them, or create entirely new functions from
scratch.
</p>
<p>There is a number of standard applications of this facility, such as
calculation of analytical derivatives of expressions, or the generation
of polynomial functions from a vector of coefficients. However, there
are also uses that are much more fundamental to the workings of the
interpreted part of R. Some of these are essential to the reuse of
functions as components in other functions, as the (admittedly not very
pretty) calls to <code>model.frame</code> that are constructed in several
modeling and plotting routines. Other uses simply allow elegant
interfaces to useful functionality. As an example, consider the
<code>curve</code> function, which allows you to draw the graph of a function
given as an expression like <code>sin(x)</code> or the facilities for plotting
mathematical expressions.
</p>
<p>In this chapter, we give an introduction to the set of facilities that
are available for computing on the language.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Direct-manipulation-of-language-objects" accesskey="1">Direct manipulation of language objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Substitutions" accesskey="2">Substitutions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#More-on-evaluation" accesskey="3">More on evaluation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Evaluation-of-expression-objects" accesskey="4">Evaluation of expression objects</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Manipulation-of-function-calls" accesskey="5">Manipulation of function calls</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Manipulation-of-functions" accesskey="6">Manipulation of functions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Direct-manipulation-of-language-objects"></a>
<div class="header">
<p>
Next: <a href="#Substitutions" accesskey="n" rel="next">Substitutions</a>, Previous: <a href="#Computing-on-the-language" accesskey="p" rel="prev">Computing on the language</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Direct-manipulation-of-language-objects-1"></a>
<h3 class="section">6.1 Direct manipulation of language objects</h3>
<p>There are three kinds of language objects that are available for
modification, calls, expressions, and functions. At this point, we
shall concentrate on the call objects. These are sometimes referred to
as &ldquo;unevaluated expressions&rdquo;, although this terminology is somewhat
confusing. The most direct method of obtaining a call object is to use
<code>quote</code> with an expression argument, e.g.,
</p>
<div class="example">
<pre class="example">&gt; e1 &lt;- quote(2 + 2)
&gt; e2 &lt;- quote(plot(x, y))
</pre></div>
<p>The arguments are not evaluated, the result is simply the parsed
argument. The objects <code>e1</code> and <code>e2</code> may be evaluated later
using <code>eval</code>, or simply manipulated as data. It is perhaps most
immediately obvious why the <code>e2</code> object has mode <code>&quot;call&quot;</code>,
since it involves a call to the <code>plot</code> function with some
arguments. However, <code>e1</code> actually has exactly the same structure
as a call to the binary operator <code>+</code> with two arguments, a fact
that gets clearly displayed by the following
</p>
<div class="example">
<pre class="example">&gt; quote(&quot;+&quot;(2, 2))
2 + 2
</pre></div>
<p>The components of a call object are accessed using a list-like syntax,
and may in fact be converted to and from lists using <code>as.list</code> and
<code>as.call</code>
</p>
<div class="example">
<pre class="example">&gt; e2[[1]]
plot
&gt; e2[[2]]
x
&gt; e2[[3]]
y
</pre></div>
<p>When keyword argument matching is used, the keywords can be used as list
tags:
</p>
<div class="example">
<pre class="example">&gt; e3 &lt;- quote(plot(x = age, y = weight))
&gt; e3$x
age
&gt; e3$y
weight
</pre></div>
<p>All the components of the call object have mode <code>&quot;name&quot;</code> in the
preceding examples. This is true for identifiers in calls, but the
components of a call can also be constants&mdash;which can be of any type,
although the first component had better be a function if the call is to
be evaluated successfully&mdash;or other call objects, corresponding to
subexpressions. Objects of mode
<a name="index-name-10"></a>
name can be constructed from character
strings using <code>as.name</code>, so one might modify the <code>e2</code> object
as follows
</p>
<div class="example">
<pre class="example">&gt; e2[[1]] &lt;- as.name(&quot;+&quot;)
&gt; e2
x + y
</pre></div>
<p>To illustrate the fact that subexpressions are simply components that
are themselves calls, consider
</p>
<div class="example">
<pre class="example">&gt; e1[[2]] &lt;- e2
&gt; e1
x + y + 2
</pre></div>
<p>All grouping parentheses in input are preserved in parsed expressions.
They are represented as a function call with one argument, so that
<code>4 - (2 - 2)</code> becomes <code>&quot;-&quot;(4, &quot;(&quot; (&quot;-&quot;(2, 2)))</code> in prefix
notation. In evaluations, the &lsquo;<samp>(</samp>&rsquo; operator just returns its
argument.
</p>
<p>This is a bit unfortunate, but it is not easy to write a
<a name="index-parsing-4"></a>
parser/deparser
combination that both preserves user input, stores it in minimal form
and ensures that parsing a deparsed expression gives the same expression
back.
</p>
<p>As it happens, R&rsquo;s parser is not perfectly invertible, nor is its
deparser, as the following examples show
</p>
<div class="example">
<pre class="example">&gt; str(quote(c(1,2)))
language c(1, 2)
&gt; str(c(1,2))
num [1:2] 1 2
&gt; deparse(quote(c(1,2)))
[1] &quot;c(1, 2)&quot;
&gt; deparse(c(1,2))
[1] &quot;c(1, 2)&quot;
&gt; quote(&quot;-&quot;(2, 2))
2 - 2
&gt; quote(2 - 2)
2 - 2
</pre></div>
<p>Deparsed expressions should, however, evaluate to an equivalent value
to the original expression (up to rounding error).
</p>
<p>...internal storage of flow control constructs...note Splus
incompatibility...
</p>
<hr>
<a name="Substitutions"></a>
<div class="header">
<p>
Next: <a href="#More-on-evaluation" accesskey="n" rel="next">More on evaluation</a>, Previous: <a href="#Direct-manipulation-of-language-objects" accesskey="p" rel="prev">Direct manipulation of language objects</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Substitutions-1"></a>
<h3 class="section">6.2 Substitutions</h3>
<p>It is in fact not often that one wants to modify the innards of an
expression like in the previous section. More frequently, one wants to
simply get at an expression in order to deparse it and use it for
labeling plots, for instance. An example of this is seen at the
beginning of <code>plot.default</code>:
<a name="index-substitute"></a>
</p>
<div class="example">
<pre class="example">xlabel &lt;- if (!missing(x))
deparse(substitute(x))
</pre></div>
<p>This causes the variable or expression given as the <code>x</code> argument to
<code>plot</code> to be used for labeling the x-axis later on.
</p>
<p>The function used to achieve this is <code>substitute</code> which takes the
expression <code>x</code> and substitutes the expression that was passed
through the formal argument <code>x</code>. Notice that for this to happen,
<code>x</code> must carry information about the expression that creates its
value. This is related to the
<a name="index-evaluation_002c-lazy-1"></a>
lazy evaluation scheme of R
(see <a href="#Promise-objects">Promise objects</a>). A formal argument is really a
<em>promise</em>, an object with three slots, one for the expression that
defines it, one for the environment in which to evaluate that expression,
and one for the value of that expression once evaluated. <code>substitute</code>
will recognize a promise variable and substitute the value of its
expression slot. If <code>substitute</code> is invoked inside a function, the
local variables of the function are also subject to substitution.
</p>
<p>The argument to <code>substitute</code> does not have to be a simple
identifier, it can be an expression involving several variables and
substitution will occur for each of these. Also, <code>substitute</code> has
an additional argument which can be an environment or a list in which
the variables are looked up. For example:
</p>
<div class="example">
<pre class="example">&gt; substitute(a + b, list(a = 1, b = quote(x)))
1 + x
</pre></div>
<p>Notice that quoting was necessary to substitute the <code>x</code>. This kind
of construction comes in handy in connection with the facilities for
putting math expression in graphs, as the following case shows
</p>
<div class="example">
<pre class="example">&gt; plot(0)
&gt; for (i in 1:4)
+ text(1, 0.2 * i,
+ substitute(x[ix] == y, list(ix = i, y = pnorm(i))))
</pre></div>
<p>It is important to realize that the substitutions are purely lexical;
there is no checking that the resulting call objects make sense if they
are evaluated. <code>substitute(x &lt;- x + 1, list(x = 2))</code> will happily
return <code>2 &lt;- 2 + 1</code>. However, some parts of R make up their own
rules for what makes sense and what does not and might actually have a
use for such ill-formed expressions. For example, using the &ldquo;math in
graphs&rdquo; feature often involves constructions that are syntactically
correct, but which would be meaningless to evaluate, like
&lsquo;<samp>{}&gt;=40*&quot; years&quot;</samp>&rsquo;.
</p>
<p>Substitute will not evaluate its first argument. This leads to the
puzzle of how to do substitutions on an object that is contained in a
variable. The solution is to use <code>substitute</code> once more, like this
</p>
<div class="example">
<pre class="example">&gt; expr &lt;- quote(x + y)
&gt; substitute(substitute(e, list(x = 3)), list(e = expr))
substitute(x + y, list(x = 3))
&gt; eval(substitute(substitute(e, list(x = 3)), list(e = expr)))
3 + y
</pre></div>
<p>The exact rules for substitutions are as follows: Each
<a name="index-symbol-4"></a>
symbol in the
<a name="index-parsing-5"></a>
parse tree for the first is matched against the second argument, which
can be a tagged list or an environment frame. If it is a simple local
object, its value is inserted, <em>except</em> if matching against the
global environment. If it is a promise (usually a function argument),
the promise expression is substituted. If the symbol is not matched, it
is left untouched. The special exception for substituting at the top
level is admittedly peculiar. It has been inherited from S and the
rationale is most likely that there is no control over which variables
might be bound at that level so that it would be better to just make
substitute act as <code>quote</code>.
</p>
<p>The rule of promise substitution is slightly different from that of
S if the local variable is modified before <code>substitute</code> is
used. R will then use the new value of the variable, whereas S
will unconditionally use the argument expression&mdash;unless it was a
constant, which has the curious consequence that <code>f((1))</code> may be
very different from <code>f(1)</code> in S. The R rule is considerably
cleaner, although it does have consequences in connection with
<a name="index-evaluation_002c-lazy-2"></a>
lazy
evaluation that comes as a surprise to some. Consider
</p>
<div class="example">
<pre class="example">logplot &lt;- function(y, ylab = deparse(substitute(y))) {
y &lt;- log(y)
plot(y, ylab = ylab)
}
</pre></div>
<p>This looks straightforward, but one will discover that the y label
becomes an ugly <code>c(...)</code> expression. It happens because the rules
of lazy evaluation cause the evaluation of the <code>ylab</code> expression
to happen <em>after</em> <code>y</code> has been modified. The solution is to
force <code>ylab</code> to be evaluated first, i.e.,
</p>
<div class="example">
<pre class="example">logplot &lt;- function(y, ylab = deparse(substitute(y))) {
ylab
y &lt;- log(y)
plot(y, ylab = ylab)
}
</pre></div>
<p>Notice that one should not use <code>eval(ylab)</code> in this situation. If
<code>ylab</code> is a language or expression object, then that would cause
the object to be evaluated as well, which would not at all be desirable
if a math expression like <code>quote(log[e](y))</code> was being passed.
</p>
<p>A variant on <code>substitute</code> is <code>bquote</code>, which is used to replace some subexpressions with their values. The example from above
</p><div class="example">
<pre class="example">&gt; plot(0)
&gt; for (i in 1:4)
+ text(1, 0.2 * i,
+ substitute(x[ix] == y, list(ix = i, y = pnorm(i))))
</pre></div>
<p>could be written more compactly as
</p><div class="example">
<pre class="example">plot(0)
for(i in 1:4)
text(1, 0.2*i, bquote( x[.(i)] == .(pnorm(i)) ))
</pre></div>
<p>The expression is quoted except for the contents of <code>.()</code>
subexpressions, which are replaced with their values. There is an
optional argument to compute the values in a different
environment. The syntax for <code>bquote</code> is borrowed from the LISP
backquote macro.
</p>
<hr>
<a name="More-on-evaluation"></a>
<div class="header">
<p>
Next: <a href="#Evaluation-of-expression-objects" accesskey="n" rel="next">Evaluation of expression objects</a>, Previous: <a href="#Substitutions" accesskey="p" rel="prev">Substitutions</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="More-on-evaluation-1"></a>
<h3 class="section">6.3 More on evaluation</h3>
<a name="index-evaluation-6"></a>
<p>The <code>eval</code> function was introduced earlier in this chapter as a
means of evaluating call objects. However, this is not the full story.
It is also possible to specify the
<a name="index-environment-15"></a>
environment in which the evaluation
is to take place. By default this is the evaluation frame from which
<code>eval</code> is called, but quite frequently it needs to be set to
something else.
<a name="index-eval"></a>
</p>
<p>Very often, the relevant evaluation frame is that of the parent of the
current frame (cf. ???). In particular, when the object to evaluate
is the result of a <code>substitute</code> operation of the function
arguments, it will contain variables that make sense to the caller only
(notice that there is no reason to expect that the variables of the
caller are in the
<a name="index-scope-5"></a>
lexical scope of the callee). Since evaluation in the
parent frame occurs frequently, an <code>eval.parent</code> function exists as
a shorthand for <code>eval(expr, sys.frame(sys.parent()))</code>.
</p>
<p>Another case that occurs frequently is evaluation in a list or a data
frame. For instance, this happens in connection with the
<code>model.frame</code> function when a <code>data</code> argument is given.
Generally, the terms of the model formula need to be evaluated in
<code>data</code>, but they may occasionally also contain references to items
in the caller of <code>model.frame</code>. This is sometimes useful in
connection with simulation studies. So for this purpose one needs not
only to evaluate an expression in a list, but also to specify an
enclosure into which the search continues if the variable is not in the
list. Hence, the call has the form
</p>
<div class="example">
<pre class="example">eval(expr, data, sys.frame(sys.parent()))
</pre></div>
<p>Notice that evaluation in a given environment may actually change that
environment, most obviously in cases involving the
<a name="index-assignment-9"></a>
assignment operator,
such as
</p>
<div class="example">
<pre class="example">eval(quote(total &lt;- 0), environment(robert$balance)) # <span class="roman">rob Rob</span>
</pre></div>
<p>This is also true when evaluating in lists, but the original list does
not change because one is really working on a copy.
</p>
<hr>
<a name="Evaluation-of-expression-objects"></a>
<div class="header">
<p>
Next: <a href="#Manipulation-of-function-calls" accesskey="n" rel="next">Manipulation of function calls</a>, Previous: <a href="#More-on-evaluation" accesskey="p" rel="prev">More on evaluation</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Evaluation-of-expression-objects-1"></a>
<h3 class="section">6.4 Evaluation of expression objects</h3>
<p>Objects of mode <code>&quot;expression&quot;</code> are defined in <a href="#Expression-objects">Expression objects</a>. They are very similar to lists of call objects.
</p>
<div class="example">
<pre class="example">&gt; ex &lt;- expression(2 + 2, 3 + 4)
&gt; ex[[1]]
2 + 2
&gt; ex[[2]]
3 + 4
&gt; eval(ex)
[1] 7
</pre></div>
<p>Notice that evaluating an expression object evaluates each call in turn,
but the final value is that of the last call. In this respect it
behaves almost identically to the compound language object
<code>quote({2 + 2; 3 + 4})</code>. However, there is a subtle difference:
Call objects are indistinguishable from subexpressions in a parse tree.
This means that they are automatically evaluated in the same way a
subexpression would be. Expression objects can be recognized during
evaluation and in a sense retain their quotedness. The evaluator will
not evaluate an expression object recursively, only when it is passed
directly to <code>eval</code> function as above. The difference can be seen
like this:
</p>
<div class="example">
<pre class="example">&gt; eval(substitute(mode(x), list(x = quote(2 + 2))))
[1] &quot;numeric&quot;
&gt; eval(substitute(mode(x), list(x = expression(2 + 2))))
[1] &quot;expression&quot;
</pre></div>
<p>The deparser represents an expression object by the call
that creates it. This is similar to the way it handles numerical
vectors and several other objects that do not have a specific external
representation. However, it does lead to the following bit of
confusion:
</p>
<div class="example">
<pre class="example">&gt; e &lt;- quote(expression(2 + 2))
&gt; e
expression(2 + 2)
&gt; mode(e)
[1] &quot;call&quot;
&gt; ee &lt;- expression(2 + 2)
&gt; ee
expression(2 + 2)
&gt; mode(ee)
[1] &quot;expression&quot;
</pre></div>
<p>I.e., <code>e</code> and <code>ee</code> look identical when printed, but one is a
call that generates an expression object and the other is the object
itself.
</p>
<hr>
<a name="Manipulation-of-function-calls"></a>
<div class="header">
<p>
Next: <a href="#Manipulation-of-functions" accesskey="n" rel="next">Manipulation of functions</a>, Previous: <a href="#Evaluation-of-expression-objects" accesskey="p" rel="prev">Evaluation of expression objects</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Manipulation-of-function-calls-1"></a>
<h3 class="section">6.5 Manipulation of function calls</h3>
<p>It is possible for a
<a name="index-function-26"></a>
function to find out how it has been called by
looking at the result of <code>sys.call</code> as in the following example of
a function that simply returns its own call:
</p>
<div class="example">
<pre class="example">&gt; f &lt;- function(x, y, ...) sys.call()
&gt; f(y = 1, 2, z = 3, 4)
f(y = 1, 2, z = 3, 4)
</pre></div>
<p>However, this is not really useful except for debugging because it
requires the function to keep track of argument matching in order to
interpret the call. For instance, it must be able to see that the 2nd
actual argument gets matched to the first formal one (<code>x</code> in the
above example).
</p>
<p>More often one requires the call with all actual arguments bound to the
corresponding formals. To this end, the function <code>match.call</code> is
used. Here&rsquo;s a variant of the preceding example, a function that
returns its own call with arguments matched
</p>
<div class="example">
<pre class="example">&gt; f &lt;- function(x, y, ...) match.call()
&gt; f(y = 1, 2, z = 3, 4)
f(x = 2, y = 1, z = 3, 4)
</pre></div>
<p>Notice that the second argument now gets matched to <code>x</code> and appears
in the corresponding position in the result.
</p>
<p>The primary use of this technique is to call another function with the
same arguments, possibly deleting some and adding others. A typical
application is seen at the start of the <code>lm</code> function:
</p>
<div class="example">
<pre class="example"> mf &lt;- cl &lt;- match.call()
mf$singular.ok &lt;- mf$model &lt;- mf$method &lt;- NULL
mf$x &lt;- mf$y &lt;- mf$qr &lt;- mf$contrasts &lt;- NULL
mf$drop.unused.levels &lt;- TRUE
mf[[1]] &lt;- as.name(&quot;model.frame&quot;)
mf &lt;- eval(mf, sys.frame(sys.parent()))
</pre></div>
<p>Notice that the resulting call is
<a name="index-evaluation-7"></a>
evaluated in the parent frame, in
which one can be certain that the involved expressions make sense. The
call can be treated as a list object where the first element is the name
of the function and the remaining elements are the actual argument
expressions, with the corresponding formal argument names as tags.
Thus, the technique to eliminate undesired arguments is to assign
<code>NULL</code>, as seen in lines 2 and 3, and to add an argument one uses
tagged list
<a name="index-assignment-10"></a>
assignment (here to pass <code>drop.unused.levels = TRUE</code>)
as in line 4. To change the name of the function called, assign to the
first element of the list and make sure that the value is a name, either
using the <code>as.name(&quot;model.frame&quot;)</code> construction here or
<code>quote(model.frame)</code>.
</p>
<p>The <code>match.call</code> function has an <code>expand.dots</code> argument which
is a switch which if set to <code>FALSE</code> lets all &lsquo;<samp>...</samp>&rsquo; arguments
be collected as a single argument with the tag &lsquo;<samp>...</samp>&rsquo;.
<a name="index-match_002ecall-1"></a>
</p>
<div class="example">
<pre class="example">&gt; f &lt;- function(x, y, ...) match.call(expand.dots = FALSE)
&gt; f(y = 1, 2, z = 3, 4)
f(x = 2, y = 1, ... = list(z = 3, 4))
</pre></div>
<p>The &lsquo;<samp>...</samp>&rsquo; argument is a list (a pairlist to be precise), not a call
to <code>list</code> like it is in S:
</p>
<div class="example">
<pre class="example">&gt; e1 &lt;- f(y = 1, 2, z = 3, 4)$...
&gt; e1
$z
[1] 3
[[2]]
[1] 4
</pre></div>
<p>One reason for using this form of <code>match.call</code> is simply to get rid
of any &lsquo;<samp>...</samp>&rsquo; arguments in order not to be passing unspecified
arguments on to functions that may not know them. Here&rsquo;s an example
paraphrased from <code>plot.formula</code>:
</p>
<div class="example">
<pre class="example">m &lt;- match.call(expand.dots = FALSE)
m$... &lt;- NULL
m[[1]] &lt;- &quot;model.frame&quot;
</pre></div>
<p>A more elaborate application is in <code>update.default</code> where a set of
optional extra arguments can add to, replace, or cancel those of the
original call:
</p>
<div class="example">
<pre class="example">extras &lt;- match.call(expand.dots = FALSE)$...
if (length(extras) &gt; 0) {
existing &lt;- !is.na(match(names(extras), names(call)))
for (a in names(extras)[existing]) call[[a]] &lt;- extras[[a]]
if (any(!existing)) {
call &lt;- c(as.list(call), extras[!existing])
call &lt;- as.call(call)
}
}
</pre></div>
<p>Notice that care is taken to modify existing arguments individually in
case <code>extras[[a]] == NULL</code>. Concatenation does not work on call
objects without the coercion as shown; this is arguably a bug.
</p>
<p>Two further functions exist for the construction of function calls,
namely <code>call</code> and <code>do.call</code>.
</p>
<p>The function <code>call</code> allows creation of a call object from the
function name and the list of arguments
</p>
<div class="example">
<pre class="example">&gt; x &lt;- 10.5
&gt; call(&quot;round&quot;, x)
round(10.5)
</pre></div>
<p>As seen, the value of <code>x</code> rather than the
<a name="index-symbol-5"></a>
symbol is inserted in the
call, so it is distinctly different from <code>round(x)</code>. The form is
used rather rarely, but is occasionally useful where the name of a
function is available as a character variable.
</p>
<p>The function <code>do.call</code> is related, but evaluates the call immediately
and takes the arguments from an object of mode <code>&quot;list&quot;</code> containing
all the arguments. A natural use of this is when one wants to apply a
function like <code>cbind</code> to all elements of a list or data frame.
<a name="index-do_002ecall"></a>
</p>
<div class="example">
<pre class="example">is.na.data.frame &lt;- function (x) {
y &lt;- do.call(&quot;cbind&quot;, lapply(x, &quot;is.na&quot;))
rownames(y) &lt;- row.names(x)
y
}
</pre></div>
<p>Other uses include variations over constructions like <code>do.call(&quot;f&quot;,
list(...))</code>. However, one should be aware that this involves evaluation
of the arguments before the actual function call, which may defeat
aspects of lazy evaluation and argument substitution in the function
itself. A similar remark applies to the <code>call</code> function.
</p>
<hr>
<a name="Manipulation-of-functions"></a>
<div class="header">
<p>
Previous: <a href="#Manipulation-of-function-calls" accesskey="p" rel="prev">Manipulation of function calls</a>, Up: <a href="#Computing-on-the-language" accesskey="u" rel="up">Computing on the language</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Manipulation-of-functions-1"></a>
<h3 class="section">6.6 Manipulation of functions</h3>
<p>It is often useful to be able to manipulate the components of a
<a name="index-function-27"></a>
function
or closure. R provides a set of interface functions for this
purpose.
</p>
<dl compact="compact">
<dt><code>body</code>
<a name="index-body-1"></a>
</dt>
<dd><p>Returns the expression that is the body of the function.
</p></dd>
<dt><code>formals</code>
<a name="index-formals-1"></a>
</dt>
<dd><p>Returns a list of the formal arguments to the function. This is a
<code>pairlist</code>.
</p></dd>
<dt><code>environment</code>
<a name="index-environment-21"></a>
</dt>
<dd><a name="index-environment-16"></a>
<p>Returns the environment associated with the function.
</p></dd>
<dt><code>body&lt;-</code>
<a name="index-body_003c_002d"></a>
</dt>
<dd><p>This sets the body of the function to the supplied expression.
</p></dd>
<dt><code>formals&lt;-</code>
<a name="index-formals_003c_002d"></a>
</dt>
<dd><p>Sets the formal arguments of the function to the supplied list.
</p></dd>
<dt><code>environment&lt;-</code>
<a name="index-environment_003c_002d"></a>
</dt>
<dd><p>Sets the environment of the function to the specified environment.
</p></dd>
</dl>
<p>It is also possible to alter the bindings of different variables in the
environment of the function, using code along the lines of <code>evalq(x
&lt;- 5, environment(f))</code>.
</p>
<p>It is also possible to convert a
<a name="index-function-28"></a>
function to a list using
<code>as.list</code>. The result is the concatenation of the list of formal
arguments with the function body. Conversely such a list can be
converted to a function using <code>as.function</code>. This functionality is
mainly included for S compatibility. Notice that environment
information is lost when <code>as.list</code> is used, whereas
<code>as.function</code> has an argument that allows the environment to be
set.
</p>
<hr>
<a name="System-and-foreign-language-interfaces"></a>
<div class="header">
<p>
Next: <a href="#Exception-handling" accesskey="n" rel="next">Exception handling</a>, Previous: <a href="#Computing-on-the-language" accesskey="p" rel="prev">Computing on the language</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="System-and-foreign-language-interfaces-1"></a>
<h2 class="chapter">7 System and foreign language interfaces</h2>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Operating-system-access" accesskey="1">Operating system access</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Foreign-language-interfaces" accesskey="2">Foreign language interfaces</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#g_t_002eInternal-and-_002ePrimitive" accesskey="3">.Internal and .Primitive</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Operating-system-access"></a>
<div class="header">
<p>
Next: <a href="#Foreign-language-interfaces" accesskey="n" rel="next">Foreign language interfaces</a>, Previous: <a href="#System-and-foreign-language-interfaces" accesskey="p" rel="prev">System and foreign language interfaces</a>, Up: <a href="#System-and-foreign-language-interfaces" accesskey="u" rel="up">System and foreign language interfaces</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Operating-system-access-1"></a>
<h3 class="section">7.1 Operating system access</h3>
<p>Access to the operating system shell is via the R function
<code>system</code>.
<a name="index-system"></a>
The details will differ by platform (see the on-line help), and about
all that can safely be assumed is that the first argument will be a
string <code>command</code> that will be passed for execution (not necessarily
by a shell) and the second argument will be <code>internal</code> which if
true will collect the output of the command into an R character
vector.
</p>
<p>The functions <code>system.time</code>
<a name="index-system_002etime"></a>
and <code>proc.time</code>
<a name="index-proc_002etime"></a>
are available for timing (although the information available may be
limited on non-Unix-like platforms).
</p>
<p>Information from the operating system
<a name="index-environment-17"></a>
environment can be accessed and manipulated with
</p><blockquote>
<table summary="">
<tr><td width="30%"><code>Sys.getenv</code></td><td width="70%">OS environment variables
<a name="index-Sys_002egetenv"></a></td></tr>
<tr><td width="30%"><code>Sys.putenv</code>
<a name="index-Sys_002eputenv"></a></td></tr>
<tr><td width="30%"><code>Sys.getlocale</code></td><td width="70%">System locale
<a name="index-Sys_002egetlocale"></a></td></tr>
<tr><td width="30%"><code>Sys.putlocale</code>
<a name="index-Sys_002eputlocale"></a></td></tr>
<tr><td width="30%"><code>Sys.localeconv</code>
<a name="index-Sys_002elocaleconv"></a></td></tr>
<tr><td width="30%"><code>Sys.time</code></td><td width="70%">Current time
<a name="index-Sys_002etime"></a></td></tr>
<tr><td width="30%"><code>Sys.timezone</code></td><td width="70%">Time zone
<a name="index-Sys_002etimezone"></a></td></tr>
</table>
</blockquote>
<p>A uniform set of file access functions is provided on all platforms:
</p><blockquote>
<table summary="">
<tr><td width="30%"><code>file.access</code></td><td width="70%">Ascertain File Accessibility
<a name="index-file_002eaccess"></a></td></tr>
<tr><td width="30%"><code>file.append</code></td><td width="70%">Concatenate files
<a name="index-file_002eappend"></a></td></tr>
<tr><td width="30%"><code>file.choose</code></td><td width="70%">Prompt user for file name
<a name="index-file_002echoose"></a></td></tr>
<tr><td width="30%"><code>file.copy</code></td><td width="70%">Copy files
<a name="index-file_002ecopy"></a></td></tr>
<tr><td width="30%"><code>file.create</code></td><td width="70%">Create or truncate a files
<a name="index-file_002ecreate"></a></td></tr>
<tr><td width="30%"><code>file.exists</code></td><td width="70%">Test for existence
<a name="index-file_002eexists"></a></td></tr>
<tr><td width="30%"><code>file.info</code></td><td width="70%">Miscellaneous file information
<a name="index-file_002einfo"></a></td></tr>
<tr><td width="30%"><code>file.remove</code></td><td width="70%">remove files
<a name="index-file_002eremove"></a></td></tr>
<tr><td width="30%"><code>file.rename</code></td><td width="70%">rename files
<a name="index-file_002erename"></a></td></tr>
<tr><td width="30%"><code>file.show</code></td><td width="70%">Display a text file
<a name="index-file_002eshow"></a></td></tr>
<tr><td width="30%"><code>unlink</code></td><td width="70%">Remove files or directories.
<a name="index-unlink"></a></td></tr>
</table>
</blockquote>
<p>There are also functions for manipulating file names and paths in a
platform-independent way.
</p><blockquote>
<table summary="">
<tr><td width="30%"><code>basename</code></td><td width="70%">File name without directory
<a name="index-basename"></a></td></tr>
<tr><td width="30%"><code>dirname</code></td><td width="70%">Directory name
<a name="index-dirname"></a></td></tr>
<tr><td width="30%"><code>file.path</code></td><td width="70%">Construct path to file
<a name="index-file_002epath"></a></td></tr>
<tr><td width="30%"><code>path.expand</code></td><td width="70%">Expand <code>~</code> in Unix path
<a name="index-path_002eexpand"></a></td></tr>
</table>
</blockquote>
<hr>
<a name="Foreign-language-interfaces"></a>
<div class="header">
<p>
Next: <a href="#g_t_002eInternal-and-_002ePrimitive" accesskey="n" rel="next">.Internal and .Primitive</a>, Previous: <a href="#Operating-system-access" accesskey="p" rel="prev">Operating system access</a>, Up: <a href="#System-and-foreign-language-interfaces" accesskey="u" rel="up">System and foreign language interfaces</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Foreign-language-interfaces-1"></a>
<h3 class="section">7.2 Foreign language interfaces</h3>
<a name="index-_002eC"></a>
<a name="index-_002eFortran"></a>
<a name="index-_002eCall"></a>
<a name="index-_002eExternal"></a>
<p>See <a href="./R-exts.html#System-and-foreign-language-interfaces">System and foreign language interfaces</a> in <cite>Writing R
Extensions</cite> for the details of adding functionality to R via compiled
code.
</p>
<p>Functions <code>.C</code> and <code>.Fortran</code> provide a standard interface to
compiled code that has been linked into R, either at build time or
via <code>dyn.load</code>. They are primarily intended for compiled <strong>C</strong> and
FORTRAN code respectively, but the <code>.C</code> function can be used with
other languages which can generate C interfaces, for example C++.
</p>
<p>Functions <code>.Call</code> and <code>.External</code> provide interfaces which allow
compiled code (primarily compiled <strong>C</strong> code) to manipulate R objects.
</p>
<hr>
<a name="g_t_002eInternal-and-_002ePrimitive"></a>
<div class="header">
<p>
Previous: <a href="#Foreign-language-interfaces" accesskey="p" rel="prev">Foreign language interfaces</a>, Up: <a href="#System-and-foreign-language-interfaces" accesskey="u" rel="up">System and foreign language interfaces</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="g_t_002eInternal-and-_002ePrimitive-1"></a>
<h3 class="section">7.3 .Internal and .Primitive</h3>
<a name="index-_002eInternal-1"></a>
<a name="index-_002ePrimitive-1"></a>
<p>The <code>.Internal</code> and <code>.Primitive</code> interfaces are used to call
<strong>C</strong> code compiled into R at build time.
See <a href="./R-ints.html#g_t_002eInternal-vs-_002ePrimitive">.Internal vs .Primitive</a> in <cite>R Internals</cite>.
</p>
<hr>
<a name="Exception-handling"></a>
<div class="header">
<p>
Next: <a href="#Debugging" accesskey="n" rel="next">Debugging</a>, Previous: <a href="#System-and-foreign-language-interfaces" accesskey="p" rel="prev">System and foreign language interfaces</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Exception-handling-1"></a>
<h2 class="chapter">8 Exception handling</h2>
<p>The exception handling facilities in R are provided through two
mechanisms. Functions such as <code>stop</code> or <code>warning</code> can be
called directly or options such as <code>&quot;warn&quot;</code> can be used to control
the handling of problems.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#stop" accesskey="1">stop</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#warning" accesskey="2">warning</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#on_002eexit" accesskey="3">on.exit</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Error-options" accesskey="4">Error options</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="stop"></a>
<div class="header">
<p>
Next: <a href="#warning" accesskey="n" rel="next">warning</a>, Previous: <a href="#Exception-handling" accesskey="p" rel="prev">Exception handling</a>, Up: <a href="#Exception-handling" accesskey="u" rel="up">Exception handling</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="stop-1"></a>
<h3 class="section">8.1 stop</h3>
<a name="index-stop"></a>
<p>A call to <code>stop</code> halts the evaluation of the current expression,
prints the message argument and returns execution to top-level.
</p>
<hr>
<a name="warning"></a>
<div class="header">
<p>
Next: <a href="#on_002eexit" accesskey="n" rel="next">on.exit</a>, Previous: <a href="#stop" accesskey="p" rel="prev">stop</a>, Up: <a href="#Exception-handling" accesskey="u" rel="up">Exception handling</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="warning-1"></a>
<h3 class="section">8.2 warning</h3>
<a name="index-warning"></a>
<a name="index-warnings"></a>
<p>The function <code>warning</code> takes a single argument that is a character
string. The behaviour of a call to <code>warning</code> depends on the value
of the option <code>&quot;warn&quot;</code>. If <code>&quot;warn&quot;</code> is negative warnings are
ignored. If it is zero, they are stored and printed after the top-level
function has completed. If it is one, they are printed as they occur
and if it is 2 (or larger) warnings are turned into errors.
</p>
<p>If <code>&quot;warn&quot;</code> is zero (the default), a variable <code>last.warning</code>
is created and the messages associated with each call to <code>warning</code>
are stored, sequentially, in this vector. If there are fewer than 10
warnings they are printed after the function has finished evaluating.
If there are more than 10 then a message indicating how many warnings
occurred is printed. In either case <code>last.warning</code> contains the
vector of messages, and <code>warnings</code> provides a way to access and
print it.
</p>
<hr>
<a name="on_002eexit"></a>
<div class="header">
<p>
Next: <a href="#Error-options" accesskey="n" rel="next">Error options</a>, Previous: <a href="#warning" accesskey="p" rel="prev">warning</a>, Up: <a href="#Exception-handling" accesskey="u" rel="up">Exception handling</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="on_002eexit-1"></a>
<h3 class="section">8.3 on.exit</h3>
<a name="index-on_002eexit"></a>
<p>A function can insert a call to <code>on.exit</code> at any point in the body
of a function. The effect of a call to <code>on.exit</code> is to store the
value of the body so that it will be executed when the function exits.
This allows the function to change some system parameters and to ensure
that they are reset to appropriate values when the function is finished.
The <code>on.exit</code> is guaranteed to be executed when the function exits
either directly or as the result of a warning.
</p>
<p>An error in the evaluation of the <code>on.exit</code> code causes an
immediate jump to top-level without further processing of the
<code>on.exit</code> code.
</p>
<p><code>on.exit</code> takes a single argument which is an expression to be
evaluated when the function is exited.
</p>
<hr>
<a name="Error-options"></a>
<div class="header">
<p>
Previous: <a href="#on_002eexit" accesskey="p" rel="prev">on.exit</a>, Up: <a href="#Exception-handling" accesskey="u" rel="up">Exception handling</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Error-options-1"></a>
<h3 class="section">8.4 Error options</h3>
<p>There are a number of <code>options</code> variables that can be used to
control how R handles errors and warnings. They are listed in the
table below.
</p>
<dl compact="compact">
<dt>&lsquo;<samp>warn</samp>&rsquo;</dt>
<dd><p>Controls the printing of warnings.
</p></dd>
<dt>&lsquo;<samp>warning.expression</samp>&rsquo;</dt>
<dd><p>Sets an expression that is to be evaluated when a warning occurs. The
normal printing of warnings is suppressed if this option is set.
</p></dd>
<dt>&lsquo;<samp>error</samp>&rsquo;</dt>
<dd><p>Installs an expression that will be evaluated when an error occurs.
The normal printing of error messages and warning messages precedes the
evaluation of the expression.
</p></dd>
</dl>
<p>Expressions installed by <code>options(&quot;error&quot;)</code> are evaluated before
calls to <code>on.exit</code> are carried out.
</p>
<p>One can use <code>options(error = expression(q(&quot;yes&quot;)))</code> to get R to
quit when an error has been signalled. In this case an error will cause
R to shut down and the global environment will be saved.
</p>
<hr>
<a name="Debugging"></a>
<div class="header">
<p>
Next: <a href="#Parser" accesskey="n" rel="next">Parser</a>, Previous: <a href="#Exception-handling" accesskey="p" rel="prev">Exception handling</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Debugging-1"></a>
<h2 class="chapter">9 Debugging</h2>
<p>Debugging code has always been a bit of an art. R provides several
tools that help users find problems in their code. These tools halt
execution at particular points in the code and the current state of the
computation can be inspected.
</p>
<p>Most debugging takes place either through calls to <code>browser</code> or
<code>debug</code>. Both of these functions rely on the same internal
mechanism and both provide the user with a special prompt. Any command
can be typed at the prompt. The evaluation
<a name="index-environment-18"></a>
environment for the command
is the currently active environment. This allows you to examine the
current state of any variables etc.
</p>
<p>There are five special commands that R interprets differently. They
are,
</p>
<dl compact="compact">
<dt>&lsquo;<samp><span class="key">RET</span></samp>&rsquo;</dt>
<dd><p>Go to the next statement if the function is being debugged. Continue
execution if the browser was invoked.
</p></dd>
<dt>&lsquo;<samp>c</samp>&rsquo;</dt>
<dt>&lsquo;<samp>cont</samp>&rsquo;</dt>
<dd><p>Continue the execution.
</p></dd>
<dt>&lsquo;<samp>n</samp>&rsquo;</dt>
<dd><p>Execute the next statement in the function. This works from the browser
as well.
</p></dd>
<dt>&lsquo;<samp>where</samp>&rsquo;</dt>
<dd><p>Show the call stack
</p></dd>
<dt>&lsquo;<samp>Q</samp>&rsquo;</dt>
<dd><p>Halt execution and jump to the top-level immediately.
</p></dd>
</dl>
<a name="index-name-11"></a>
<p>If there is a local variable with the same name as one of the special
commands listed above then its value can be accessed by using
<code>get</code>. A call to <code>get</code> with the name in quotes will retrieve
the value in the current
<a name="index-environment-19"></a>
environment.
</p>
<p>The debugger provides access only to interpreted expressions. If a
function calls a foreign language (such as <strong>C</strong>) then no access to the
statements in that language is provided. Execution will halt on the
next statement that is evaluated in R. A symbolic debugger such as
<code>gdb</code> can be used to debug compiled code.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#browser" accesskey="1">browser</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#debug_002fundebug" accesskey="2">debug/undebug</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#trace_002funtrace" accesskey="3">trace/untrace</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#traceback" accesskey="4">traceback</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="browser"></a>
<div class="header">
<p>
Next: <a href="#debug_002fundebug" accesskey="n" rel="next">debug/undebug</a>, Previous: <a href="#Debugging" accesskey="p" rel="prev">Debugging</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="browser-1"></a>
<h3 class="section">9.1 browser</h3>
<a name="index-browser"></a>
<p>A call to the function <code>browser</code> causes R to halt execution at
that point and to provide the user with a special prompt. Arguments to
<code>browser</code> are ignored.
</p>
<div class="example">
<pre class="example">&gt; foo &lt;- function(s) {
+ c &lt;- 3
+ browser()
+ }
&gt; foo(4)
Called from: foo(4)
Browse[1]&gt; s
[1] 4
Browse[1]&gt; get(&quot;c&quot;)
[1] 3
Browse[1]&gt;
</pre></div>
<hr>
<a name="debug_002fundebug"></a>
<div class="header">
<p>
Next: <a href="#trace_002funtrace" accesskey="n" rel="next">trace/untrace</a>, Previous: <a href="#browser" accesskey="p" rel="prev">browser</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="debug_002fundebug-1"></a>
<h3 class="section">9.2 debug/undebug</h3>
<a name="index-debug"></a>
<a name="index-undebug"></a>
<p>The debugger can be invoked on any function by using the command
<code>debug(<var>fun</var>)</code>. Subsequently, each time that function is
evaluated the debugger is invoked. The debugger allows you to control
the evaluation of the statements in the body of the function. Before
each statement is executed the statement is printed out and a special
prompt provided. Any command can be given, those in the table above
have special meaning.
</p>
<p>Debugging is turned off by a call to <code>undebug</code> with the function as
an argument.
</p>
<div class="example">
<pre class="example">&gt; debug(mean.default)
&gt; mean(1:10)
debugging in: mean.default(1:10)
debug: {
if (na.rm)
x &lt;- x[!is.na(x)]
trim &lt;- trim[1]
n &lt;- length(c(x, recursive = TRUE))
if (trim &gt; 0) {
if (trim &gt;= 0.5)
return(median(x, na.rm = FALSE))
lo &lt;- floor(n * trim) + 1
hi &lt;- n + 1 - lo
x &lt;- sort(x, partial = unique(c(lo, hi)))[lo:hi]
n &lt;- hi - lo + 1
}
sum(x)/n
}
Browse[1]&gt;
debug: if (na.rm) x &lt;- x[!is.na(x)]
Browse[1]&gt;
debug: trim &lt;- trim[1]
Browse[1]&gt;
debug: n &lt;- length(c(x, recursive = TRUE))
Browse[1]&gt; c
exiting from: mean.default(1:10)
[1] 5.5
</pre></div>
<hr>
<a name="trace_002funtrace"></a>
<div class="header">
<p>
Next: <a href="#traceback" accesskey="n" rel="next">traceback</a>, Previous: <a href="#debug_002fundebug" accesskey="p" rel="prev">debug/undebug</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="trace_002funtrace-1"></a>
<h3 class="section">9.3 trace/untrace</h3>
<a name="index-trace"></a>
<a name="index-untrace"></a>
<p>Another way of monitoring the behaviour of R is through the
<code>trace</code> mechanism. <code>trace</code> is called with a single argument
that is the name of the function you want to trace. The name does not
need to be quoted but for some functions you will need to quote the name
in order to avoid a syntax error.
</p>
<p>When <code>trace</code> has been invoked on a function then every time that
function is evaluated the call to it is printed out. This mechanism is
removed by calling <code>untrace</code> with the function as an argument.
</p>
<div class="example">
<pre class="example">&gt; trace(&quot;[&lt;-&quot;)
&gt; x &lt;- 1:10
&gt; x[3] &lt;- 4
trace: &quot;[&lt;-&quot;(*tmp*, 3, value = 4)
</pre></div>
<hr>
<a name="traceback"></a>
<div class="header">
<p>
Previous: <a href="#trace_002funtrace" accesskey="p" rel="prev">trace/untrace</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="traceback-1"></a>
<h3 class="section">9.4 traceback</h3>
<a name="index-traceback"></a>
<p>When an error has caused a jump to top-level a special variable called
<code>.Traceback</code> is placed into the base environment.
<code>.Traceback</code> is a character vector with one entry for each function
call that was active at the time the error occurred. An examination of
<code>.Traceback</code> can be carried out by a call to <code>traceback</code>.
</p>
<hr>
<a name="Parser"></a>
<div class="header">
<p>
Next: <a href="#Function-and-Variable-Index" accesskey="n" rel="next">Function and Variable Index</a>, Previous: <a href="#Debugging" accesskey="p" rel="prev">Debugging</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Parser-1"></a>
<h2 class="chapter">10 Parser</h2>
<a name="index-parsing-6"></a>
<p>The parser is what converts the textual representation of R code into
an internal form which may then be passed to the R evaluator which
causes the specified instructions to be carried out. The internal form
is itself an R object and can be saved and otherwise manipulated
within the R system.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#The-parsing-process" accesskey="1">The parsing process</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Comments" accesskey="2">Comments</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Tokens" accesskey="3">Tokens</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Expressions" accesskey="4">Expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Directives" accesskey="5">Directives</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="The-parsing-process"></a>
<div class="header">
<p>
Next: <a href="#Comments" accesskey="n" rel="next">Comments</a>, Previous: <a href="#Parser" accesskey="p" rel="prev">Parser</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="The-parsing-process-1"></a>
<h3 class="section">10.1 The parsing process</h3>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Modes-of-parsing" accesskey="1">Modes of parsing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Internal-representation" accesskey="2">Internal representation</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Deparsing" accesskey="3">Deparsing</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Modes-of-parsing"></a>
<div class="header">
<p>
Next: <a href="#Internal-representation" accesskey="n" rel="next">Internal representation</a>, Previous: <a href="#The-parsing-process" accesskey="p" rel="prev">The parsing process</a>, Up: <a href="#The-parsing-process" accesskey="u" rel="up">The parsing process</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Modes-of-parsing-1"></a>
<h4 class="subsection">10.1.1 Modes of parsing</h4>
<p>Parsing in R occurs in three different variants:
</p>
<ul>
<li> The read-eval-print loop
</li><li> Parsing of text files
</li><li> Parsing of character strings
</li></ul>
<p>The read-eval-print loop forms the basic command line interface to R.
Textual input is read until a complete R expression is available.
Expressions may be split over several input lines. The primary prompt
(by default &lsquo;<samp>&gt; </samp>&rsquo;) indicates that the parser is ready for a new
expression, and a continuation prompt (by default &lsquo;<samp>+ </samp>&rsquo;) indicates
that the parser expects the remainder of an incomplete expression. The
expression is converted to internal form during input and the parsed
expression is passed to the evaluator and the result is printed (unless
specifically made invisible). If the parser finds itself in a state
which is incompatible with the language syntax, a &ldquo;Syntax Error&rdquo; is
flagged and the parser resets itself and resumes input at the beginning
of the next input line.
</p>
<p>Text files can be parsed using the <code>parse</code> function. In
particular, this is done during execution of the <code>source</code>
function, which allows commands to be stored in an external file and
executed as if they had been typed at the keyboard. Note, though, that
the entire file is parsed and syntax checked before any evaluation takes
place.
</p>
<p>Character strings, or vectors thereof, can be parsed using the
<code>text=</code> argument to <code>parse</code>. The strings are treated exactly
as if they were the lines of an input file.
</p>
<hr>
<a name="Internal-representation"></a>
<div class="header">
<p>
Next: <a href="#Deparsing" accesskey="n" rel="next">Deparsing</a>, Previous: <a href="#Modes-of-parsing" accesskey="p" rel="prev">Modes of parsing</a>, Up: <a href="#The-parsing-process" accesskey="u" rel="up">The parsing process</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Internal-representation-1"></a>
<h4 class="subsection">10.1.2 Internal representation</h4>
<a name="index-parsing-7"></a>
<p>Parsed expressions are stored in an R object containing the parse
tree. A fuller description of such objects can be found in
<a href="#Language-objects">Language objects</a> and <a href="#Expression-objects">Expression objects</a>. Briefly, every
elementary R expression is stored in
<a name="index-function-29"></a>
function call form, as a list
with the first element containing the function name and the remainder
containing the arguments, which may in turn be further R expressions.
The list elements can be named, corresponding to tagged matching of
formal and actual arguments. Note that <em>all</em> R syntax elements
are treated in this way, e.g. the assignment <code>x &lt;- 1</code> is encoded
as <code>&quot;&lt;-&quot;(x, 1)</code>.
</p>
<hr>
<a name="Deparsing"></a>
<div class="header">
<p>
Previous: <a href="#Internal-representation" accesskey="p" rel="prev">Internal representation</a>, Up: <a href="#The-parsing-process" accesskey="u" rel="up">The parsing process</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Deparsing-1"></a>
<h4 class="subsection">10.1.3 Deparsing</h4>
<p>Any R object can be converted to an R expression using
<code>deparse</code>. This is frequently used in connection with output of
results, e.g. for labeling plots. Notice that only objects of mode
<code>&quot;expression&quot;</code> can be expected to be unchanged by reparsing the
output of deparsing. For instance, the numeric vector <code>1:5</code> will
deparse as <code>&quot;c(1, 2, 3, 4, 5)&quot;</code>, which will reparse as a call to
the function <code>c</code>. As far as possible, evaluating the deparsed and
reparsed expression gives the same result as evaluating the original,
but there are a couple of awkward exceptions, mostly involving
expressions that weren&rsquo;t generated from a textual representation in the
first place.
</p>
<hr>
<a name="Comments"></a>
<div class="header">
<p>
Next: <a href="#Tokens" accesskey="n" rel="next">Tokens</a>, Previous: <a href="#The-parsing-process" accesskey="p" rel="prev">The parsing process</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Comments-1"></a>
<h3 class="section">10.2 Comments</h3>
<a name="index-comments"></a>
<p>Comments in R are ignored by the parser. Any text from a
<a name="index-_0023"></a>
<code>#</code> character
to the end of the line is taken to be a comment, unless
the <code>#</code> character is inside a quoted string. For example,
</p>
<div class="example">
<pre class="example">&gt; x &lt;- 1 # This is a comment...
&gt; y &lt;- &quot; #... but this is not.&quot;
</pre></div>
<hr>
<a name="Tokens"></a>
<div class="header">
<p>
Next: <a href="#Expressions" accesskey="n" rel="next">Expressions</a>, Previous: <a href="#Comments" accesskey="p" rel="prev">Comments</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Tokens-1"></a>
<h3 class="section">10.3 Tokens</h3>
<p>Tokens are the elementary building blocks of a programming language.
They are recognised during <em>lexical analysis</em> which (conceptually,
at least) takes place prior to the syntactic analysis performed by the
parser itself.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Literal-constants" accesskey="1">Literal constants</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Identifiers" accesskey="2">Identifiers</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Reserved-words" accesskey="3">Reserved words</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Special-operators" accesskey="4">Special operators</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Separators" accesskey="5">Separators</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Operator-tokens" accesskey="6">Operator tokens</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Grouping" accesskey="7">Grouping</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Indexing-tokens" accesskey="8">Indexing tokens</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Literal-constants"></a>
<div class="header">
<p>
Next: <a href="#Identifiers" accesskey="n" rel="next">Identifiers</a>, Previous: <a href="#Tokens" accesskey="p" rel="prev">Tokens</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Constants-2"></a>
<h4 class="subsection">10.3.1 Constants</h4>
<p>There are five types of constants: integer, logical, numeric, complex and string.
</p>
<p>In addition, there are four special constants, <code>NULL</code>, <code>NA</code>,
<code>Inf</code>, and <code>NaN</code>.
</p>
<p><code>NULL</code> is used to indicate the empty object. <code>NA</code> is used for
absent (&ldquo;Not Available&rdquo;) data values. <code>Inf</code> denotes infinity and
<code>NaN</code> is not-a-number in the <acronym>IEEE</acronym> floating point calculus
(results of the operations respectively <em>1/0</em> and <em>0/0</em>, for
instance).
</p>
<p>Logical constants are either <code>TRUE</code> or <code>FALSE</code>.
</p>
<p>Numeric constants follow a similar syntax to that of the <strong>C</strong> language.
They consist of an integer part consisting of zero or more digits,
followed optionally by &lsquo;<samp>.</samp>&rsquo; and a fractional part of zero or more
digits optionally followed by an exponent part consisting of an &lsquo;<samp>E</samp>&rsquo;
or an &lsquo;<samp>e</samp>&rsquo;, an optional sign and a string of one or more digits.
Either the fractional or the decimal part can be empty, but not both at
once.
</p>
<div class="example">
<pre class="example"><span class="roman">Valid numeric constants:</span> 1 10 0.1 .2 1e-7 1.2e+7
</pre></div>
<p>Numeric constants can also be hexadecimal, starting with &lsquo;<samp>0x</samp>&rsquo; or
&lsquo;<samp>0x</samp>&rsquo; followed by zero or more digits, &lsquo;<samp>a-f</samp>&rsquo; or &lsquo;<samp>A-F</samp>&rsquo;.
Hexadecimal floating point constants are supported using C99 syntax, e.g.
&lsquo;<samp>0x1.1p1</samp>&rsquo;.
</p>
<p>There is now a separate class of integer constants. They are created
by using the qualifier <code>L</code> at the end of the number. For
example, <code>123L</code> gives an integer value rather than a numeric
value. The suffix <code>L</code> can be used to qualify any non-complex
number with the intent of creating an integer. So it can be used with
numbers given by hexadecimal or scientific notation. However, if the
value is not a valid integer, a warning is emitted and the numeric
value created. The following shows examples of valid integer
constants, values which will generate a warning and give numeric
constants and syntax errors.
</p>
<div class="example">
<pre class="example"><span class="roman">Valid integer constants:</span> 1L, 0x10L, 1000000L, 1e6L
<span class="roman">Valid numeric constants:</span> 1.1L, 1e-3L, 0x1.1p-2
<span class="roman">Syntax error:</span> 12iL 0x1.1
</pre></div>
<p>A warning is emitted for decimal values that contain an unnecessary
decimal point, e.g. <code>1.L</code>. It is an error to have a decimal
point in a hexadecimal constant without the binary exponent.
</p>
<p>Note also that a preceding sign (<code>+</code> or <code>-</code>) is treated as a
unary operator, not as part of the constant.
</p>
<p>Up-to-date information on the currently accepted formats can be found by
<code>?NumericConstants</code>.
</p>
<p>Complex constants have the form of a decimal numeric constant followed
by &lsquo;<samp>i</samp>&rsquo;. Notice that only purely imaginary numbers are actual
constants, other complex numbers are parsed a unary or binary operations
on numeric and imaginary numbers.
</p>
<div class="example">
<pre class="example"><span class="roman">Valid complex constants:</span> 2i 4.1i 1e-2i
</pre></div>
<p>String constants are delimited by a pair of single (&lsquo;<samp>'</samp>&rsquo;) or double
(&lsquo;<samp>&quot;</samp>&rsquo;) quotes and can contain all other printable characters.
Quotes and other special characters within strings are specified using
<em>escape sequences</em>:
</p>
<dl compact="compact">
<dt><code>\'</code></dt>
<dd><p>single quote
</p></dd>
<dt><code>\&quot;</code></dt>
<dd><p>double quote
</p></dd>
<dt><code>\n</code></dt>
<dd><p>newline
</p></dd>
<dt><code>\r</code></dt>
<dd><p>carriage return
</p></dd>
<dt><code>\t</code></dt>
<dd><p>tab character
</p></dd>
<dt><code>\b</code></dt>
<dd><p>backspace
</p></dd>
<dt><code>\a</code></dt>
<dd><p>bell
</p></dd>
<dt><code>\f</code></dt>
<dd><p>form feed
</p></dd>
<dt><code>\v</code></dt>
<dd><p>vertical tab
</p></dd>
<dt><code>\\</code></dt>
<dd><p>backslash itself
</p></dd>
<dt><code>\<var>nnn</var></code></dt>
<dd><p>character with given octal code &ndash; sequences of one, two or three digits
in the range <code>0 ... 7</code> are accepted.
</p></dd>
<dt><code>\x<var>nn</var></code></dt>
<dd><p>character with given hex code &ndash; sequences of one or two hex digits
(with entries <code>0 ... 9 A ... F a ... f</code>).
</p></dd>
<dt><code>\u<var>nnnn</var> \u{<var>nnnn</var>}</code></dt>
<dd><p>(where multibyte locales are supported, otherwise an error).
Unicode character with given hex code &ndash; sequences of up to four hex
digits. The character needs to be valid in the current locale.
</p></dd>
<dt><code>\U<var>nnnnnnnn</var> \U{<var>nnnnnnnn</var>}</code></dt>
<dd><p>(where multibyte locales are supported and not on Windows, otherwise an
error). Unicode character with given hex code &ndash; sequences of up to
eight hex digits.
</p></dd>
</dl>
<p>A single quote may also be embedded directly in a double-quote delimited
string and vice versa.
</p>
<p>As from R 2.8.0, a &lsquo;nul&rsquo; (<code>\0</code>) is not allowed in a character
string, so using <code>\0</code> in a string constant terminates the constant
(usually with a warning): further characters up to the closing quote are
scanned but ignored.
</p>
<hr>
<a name="Identifiers"></a>
<div class="header">
<p>
Next: <a href="#Reserved-words" accesskey="n" rel="next">Reserved words</a>, Previous: <a href="#Literal-constants" accesskey="p" rel="prev">Literal constants</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Identifiers-1"></a>
<h4 class="subsection">10.3.2 Identifiers</h4>
<a name="index-identifier"></a>
<p>Identifiers consist of a sequence of letters, digits, the period
(&lsquo;<samp>.</samp>&rsquo;) and the underscore. They must not start with a digit or
an underscore, or with a period followed by a digit.
</p>
<p>The definition of a letter depends on the current locale: the precise
set of characters allowed is given by the C expression <code>(isalnum(c)
|| c == . || c == _)</code> and will include accented letters in many
Western European locales.
</p>
<p>Notice that identifiers starting with a period are not by default listed
by the <code>ls</code> function and that &lsquo;<samp>...</samp>&rsquo; and &lsquo;<samp>..1</samp>&rsquo;,
&lsquo;<samp>..2</samp>&rsquo;, etc. are special.
</p>
<p>Notice also that objects can have names that are not identifiers. These
are generally accessed via <code>get</code> and <code>assign</code>, although they
can also be represented by text strings in some limited circumstances
when there is no ambiguity (e.g. <code>&quot;x&quot; &lt;- 1</code>). As <code>get</code> and
<code>assign</code> are not restricted to names that are identifiers they do
not recognise subscripting operators or replacement functions. The
following pairs are <em>not</em> equivalent
<a name="index-get"></a>
<a name="index-assign"></a>
</p>
<blockquote>
<table summary="">
<tr><td><code>x$a&lt;-1</code></td><td><code>assign(&quot;x$a&quot;,1)</code></td></tr>
<tr><td><code>x[[1]]</code></td><td><code>get(&quot;x[[1]]&quot;)</code></td></tr>
<tr><td><code>names(x)&lt;-nm</code></td><td><code>assign(&quot;names(x)&quot;,nm)</code></td></tr>
</table>
</blockquote>
<hr>
<a name="Reserved-words"></a>
<div class="header">
<p>
Next: <a href="#Special-operators" accesskey="n" rel="next">Special operators</a>, Previous: <a href="#Identifiers" accesskey="p" rel="prev">Identifiers</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Reserved-words-1"></a>
<h4 class="subsection">10.3.3 Reserved words</h4>
<p>The following identifiers have a special meaning and cannot be used
for object names
</p>
<div class="example">
<pre class="example">if else repeat while function for in next break
TRUE FALSE NULL Inf NaN
NA NA_integer_ NA_real_ NA_complex_ NA_character_
... ..1 ..2 <span class="roman">etc.</span>
</pre></div>
<hr>
<a name="Special-operators"></a>
<div class="header">
<p>
Next: <a href="#Separators" accesskey="n" rel="next">Separators</a>, Previous: <a href="#Reserved-words" accesskey="p" rel="prev">Reserved words</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Special-operators-1"></a>
<h4 class="subsection">10.3.4 Special operators</h4>
<p>R allows user-defined infix operators. These have the form of a
string of characters delimited by the &lsquo;<samp>%</samp>&rsquo; character. The string
can contain any printable character except &lsquo;<samp>%</samp>&rsquo;. The escape sequences
for strings do not apply here.
</p>
<p>Note that the following operators are predefined
</p>
<div class="example">
<pre class="example">%% %*% %/% %in% %o% %x%
</pre></div>
<hr>
<a name="Separators"></a>
<div class="header">
<p>
Next: <a href="#Operator-tokens" accesskey="n" rel="next">Operator tokens</a>, Previous: <a href="#Special-operators" accesskey="p" rel="prev">Special operators</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Separators-1"></a>
<h4 class="subsection">10.3.5 Separators</h4>
<p>Although not strictly tokens, stretches of whitespace characters
(spaces, tabs and formfeeds, on Windows and UTF-8 locales other Unicode
whitespace characters<a name="DOCF4" href="#FOOT4"><sup>4</sup></a>) serve to delimit tokens in case of
ambiguity, (compare <code>x&lt;-5</code> and <code>x &lt; -5</code>).
</p>
<p>Newlines have a function which is a combination of token separator and
expression terminator. If an expression can terminate at the end of
the line the parser will assume it does so, otherwise the newline is
treated as whitespace. Semicolons (&lsquo;<samp>;</samp>&rsquo;) may be used to separate
elementary
<a name="index-expression-2"></a>
expressions on the same line.
</p>
<p>Special rules apply to the <code>else</code> keyword: inside a compound
expression, a newline before <code>else</code> is discarded, whereas at the
outermost level, the newline terminates the <code>if</code> construction and a
subsequent <code>else</code> causes a syntax error. This somewhat anomalous
behaviour occurs because R should be usable in interactive mode and
then it must decide whether the input expression is complete,
incomplete, or invalid as soon as the user presses <tt class="key">RET</tt>.
</p>
<p>The comma (&lsquo;<samp>,</samp>&rsquo;) is used to separate function arguments and multiple
indices.
</p>
<hr>
<a name="Operator-tokens"></a>
<div class="header">
<p>
Next: <a href="#Grouping" accesskey="n" rel="next">Grouping</a>, Previous: <a href="#Separators" accesskey="p" rel="prev">Separators</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Operator-tokens-1"></a>
<h4 class="subsection">10.3.6 Operator tokens</h4>
<p>R uses the following operator tokens
</p>
<blockquote>
<table summary="">
<tr><td width="30%"><code>+ - * / %% ^</code></td><td width="60%">arithmetic</td></tr>
<tr><td width="30%"><code>&gt; &gt;= &lt; &lt;= == !=</code></td><td width="60%">relational</td></tr>
<tr><td width="30%"><code>! &amp; |</code></td><td width="60%">logical</td></tr>
<tr><td width="30%"><code>~</code></td><td width="60%">model formulae</td></tr>
<tr><td width="30%"><code>-&gt; &lt;-</code></td><td width="60%">assignment</td></tr>
<tr><td width="30%"><code>$</code></td><td width="60%">list indexing</td></tr>
<tr><td width="30%"><code>:</code></td><td width="60%">sequence</td></tr>
</table>
</blockquote>
<p>(Several of the operators have different meaning inside model formulas)
</p>
<hr>
<a name="Grouping"></a>
<div class="header">
<p>
Next: <a href="#Indexing-tokens" accesskey="n" rel="next">Indexing tokens</a>, Previous: <a href="#Operator-tokens" accesskey="p" rel="prev">Operator tokens</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Grouping-1"></a>
<h4 class="subsection">10.3.7 Grouping</h4>
<p>Ordinary parentheses&mdash;&lsquo;<samp>(</samp>&rsquo; and &lsquo;<samp>)</samp>&rsquo;&mdash;are used for explicit
grouping within expressions and to delimit the argument lists for
function definitions and function calls.
</p>
<p>Braces&mdash;&lsquo;<samp>{</samp>&rsquo; and &lsquo;<samp>}</samp>&rsquo;&mdash;delimit blocks of expressions in
function definitions, conditional expressions, and iterative constructs.
</p>
<hr>
<a name="Indexing-tokens"></a>
<div class="header">
<p>
Previous: <a href="#Grouping" accesskey="p" rel="prev">Grouping</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Indexing-tokens-1"></a>
<h4 class="subsection">10.3.8 Indexing tokens</h4>
<p>Indexing of arrays and vectors is performed using the single and double
brackets, &lsquo;<samp>[]</samp>&rsquo; and &lsquo;<samp>[[]]</samp>&rsquo;. Also, indexing tagged lists
may be done using the &lsquo;<samp>$</samp>&rsquo; operator.
</p>
<hr>
<a name="Expressions"></a>
<div class="header">
<p>
Next: <a href="#Directives" accesskey="n" rel="next">Directives</a>, Previous: <a href="#Tokens" accesskey="p" rel="prev">Tokens</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Expressions-1"></a>
<h3 class="section">10.4 Expressions</h3>
<p>An R program consists of a sequence of R expressions. An
expression can be a simple expression consisting of only a constant or
an identifier, or it can be a compound expression constructed from other
parts (which may themselves be expressions).
</p>
<p>The following sections detail the various syntactical constructs that
are available.
</p>
<table summary="" class="menu" border="0" cellspacing="0">
<tr><td align="left" valign="top">&bull; <a href="#Function-calls-_0028expressions_0029" accesskey="1">Function calls (expressions)</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Infix-and-prefix-operators" accesskey="2">Infix and prefix operators</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Index-constructions" accesskey="3">Index constructions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Compound-expressions" accesskey="4">Compound expressions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Flow-control-elements" accesskey="5">Flow control elements</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
<tr><td align="left" valign="top">&bull; <a href="#Function-definitions" accesskey="6">Function definitions</a>:</td><td>&nbsp;&nbsp;</td><td align="left" valign="top">
</td></tr>
</table>
<hr>
<a name="Function-calls-_0028expressions_0029"></a>
<div class="header">
<p>
Next: <a href="#Infix-and-prefix-operators" accesskey="n" rel="next">Infix and prefix operators</a>, Previous: <a href="#Expressions" accesskey="p" rel="prev">Expressions</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-calls-2"></a>
<h4 class="subsection">10.4.1 Function calls</h4>
<a name="index-function-30"></a>
<p>A function call takes the form of a function reference followed by a
comma-separated list of arguments within a set of parentheses.
</p>
<div class="example">
<pre class="example"><var>function_reference</var> ( <var>arg1</var>, <var>arg2</var>, ...... , <var>argn</var> )
</pre></div>
<p>The function reference can be either
</p><ul>
<li> an identifier (the name of the function)
</li><li> a text string (ditto, but handy if the function has a name which is not
a valid identifier)
</li><li> an expression (which should evaluate to a function object)
</li></ul>
<p>Each argument can be tagged (<code><var>tag</var>=<var>expr</var></code>), or just be a
simple expression. It can also be empty or it can be one of the special
tokens &lsquo;<samp>...</samp>&rsquo;, &lsquo;<samp>..2</samp>&rsquo;, etc.
</p>
<p>A tag can be an identifier or a text string.
</p>
<p>Examples:
</p>
<div class="example">
<pre class="example">f(x)
g(tag = value, , 5)
&quot;odd name&quot;(&quot;strange tag&quot; = 5, y)
(function(x) x^2)(5)
</pre></div>
<hr>
<a name="Infix-and-prefix-operators"></a>
<div class="header">
<p>
Next: <a href="#Index-constructions" accesskey="n" rel="next">Index constructions</a>, Previous: <a href="#Function-calls-_0028expressions_0029" accesskey="p" rel="prev">Function calls (expressions)</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Infix-and-prefix-operators-1"></a>
<h4 class="subsection">10.4.2 Infix and prefix operators</h4>
<p>The order of precedence (highest first) of the operators is
</p>
<div class="example">
<pre class="example">::
$ @
^
- + <span class="roman">(unary)</span>
:
%<var>xyz</var>%
* /
+ - <span class="roman">(binary)</span>
&gt; &gt;= &lt; &lt;= == !=
!
&amp; &amp;&amp;
| ||
~ <span class="roman">(unary and binary)</span>
-&gt; -&gt;&gt;
= <span class="roman">(as assignment)</span>
&lt;- &lt;&lt;-
</pre></div>
<p>Note that <code>:</code> precedes binary +/-, but not <code>^</code>. Hence,
<code>1:3-1</code> is <em>0 1 2</em>, but <code>1:2^3</code> is <code>1:8</code>.
</p>
<p>The exponentiation operator &lsquo;<samp>^</samp>&rsquo; and the
<a name="index-assignment-11"></a>
left assignment plus minus operators
&lsquo;<samp>&lt;- - = &lt;&lt;-</samp>&rsquo; group right to left, all other operators group left to
right. That is, <code>2 ^ 2 ^ 3</code> is <em>2 ^ 8</em>, not <em>4 ^ 3</em>,
whereas <code>1 - 1 - 1</code> is <em>-1</em>, not 1.
</p>
<p>Notice that the operators <code>%%</code> and <code>%/%</code> for integer
remainder and divide have higher precedence than multiply and divide.
</p>
<p>Although it is not strictly an operator, it also needs mentioning that
the &lsquo;<samp>=</samp>&rsquo; sign is used for tagging arguments in
function calls and
for assigning default values in function definitions.
</p>
<p>The &lsquo;<samp>$</samp>&rsquo; sign is in some sense an operator, but does not allow
arbitrary right hand sides and is discussed under <a href="#Index-constructions">Index constructions</a>. It has higher precedence than any of the other
operators.
</p>
<p>The parsed form of a unary or binary operation is completely equivalent
to a function call with the operator as the function name and the
operands as the function arguments.
</p>
<p>Parentheses are recorded as equivalent to a unary operator, with name
<code>&quot;(&quot;</code>, even in cases where the parentheses could be inferred from
operator precedence (e.g., <code>a * (b + c)</code>).
</p>
<p>Notice that the
<a name="index-assignment-12"></a>
assignment symbols are operators just like the arithmetic, relational,
and logical ones. Any expression is allowed also on the target side of
an assignment, as far as the parser is concerned (<code>2 + 2 &lt;- 5</code> is a
valid expression as far as the parser is concerned. The evaluator will
object, though). Similar comments apply to the model formula operator.
</p>
<hr>
<a name="Index-constructions"></a>
<div class="header">
<p>
Next: <a href="#Compound-expressions" accesskey="n" rel="next">Compound expressions</a>, Previous: <a href="#Infix-and-prefix-operators" accesskey="p" rel="prev">Infix and prefix operators</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Index-constructions-1"></a>
<h4 class="subsection">10.4.3 Index constructions</h4>
<p>R has three indexing constructs, two of which are syntactically
similar although with somewhat different semantics:
</p>
<div class="example">
<pre class="example"><var>object</var> [ <var>arg1</var>, ...... , <var>argn</var> ]
<var>object</var> [[ <var>arg1</var>, ...... , <var>argn</var> ]]
</pre></div>
<a name="index-_005b-1"></a>
<a name="index-_005b_005b-1"></a>
<p>The <var>object</var> can formally be any valid expression, but it is
understood to denote or evaluate to a subsettable object. The arguments
generally evaluate to numerical or character indices, but other kinds of
arguments are possible (notably <code>drop = FALSE</code>).
</p>
<p>Internally, these index constructs are stored as function calls with
function name <code>&quot;[&quot;</code> respectively <code>&quot;[[&quot;</code>.
</p>
<p>The third index construction is
</p>
<div class="example">
<pre class="example"><var>object</var> $ <var>tag</var>
</pre></div>
<a name="index-_0024-1"></a>
<p>Here, <var>object</var> is as above, whereas <var>tag</var> is an identifier or a
text string. Internally, it is stored as a function call with name
<code>&quot;$&quot;</code>
</p>
<hr>
<a name="Compound-expressions"></a>
<div class="header">
<p>
Next: <a href="#Flow-control-elements" accesskey="n" rel="next">Flow control elements</a>, Previous: <a href="#Index-constructions" accesskey="p" rel="prev">Index constructions</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Compound-expressions-1"></a>
<h4 class="subsection">10.4.4 Compound expressions</h4>
<p>A compound expression is of the form
</p>
<div class="example">
<pre class="example">{ <var>expr1</var> ; <var>expr2</var> ; ...... ; <var>exprn</var> }
</pre></div>
<p>The semicolons may be replaced by newlines. Internally, this is stored
as a function call with <code>&quot;{&quot;</code> as the function name and the
expressions as arguments.
</p>
<hr>
<a name="Flow-control-elements"></a>
<div class="header">
<p>
Next: <a href="#Function-definitions" accesskey="n" rel="next">Function definitions</a>, Previous: <a href="#Compound-expressions" accesskey="p" rel="prev">Compound expressions</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Flow-control-elements-1"></a>
<h4 class="subsection">10.4.5 Flow control elements</h4>
<p>R contains the following control structures as special syntactic
constructs
</p>
<div class="example">
<pre class="example">if ( <var>cond</var> ) <var>expr</var>
if ( <var>cond</var> ) <var>expr1</var> else <var>expr2</var>
while ( <var>cond</var> ) <var>expr</var>
repeat <var>expr</var>
for ( <var>var</var> in <var>list</var> ) <var>expr</var>
</pre></div>
<p>The expressions in these constructs will typically be compound
expressions.
</p>
<p>Within the loop constructs (<code>while</code>, <code>repeat</code>, <code>for</code>),
one may use <code>break</code> (to terminate the loop) and <code>next</code> (to
skip to the next iteration).
</p>
<p>Internally, the constructs are stored as function calls:
</p>
<div class="example">
<pre class="example">&quot;if&quot;(<var>cond</var>, <var>expr</var>)
&quot;if&quot;(<var>cond</var>, <var>expr1</var>, <var>expr2</var>)
&quot;while&quot;(<var>cond</var>, <var>expr</var>)
&quot;repeat&quot;(<var>expr</var>)
&quot;for&quot;(<var>var</var>, <var>list</var>, <var>expr</var>)
&quot;break&quot;()
&quot;next&quot;()
</pre></div>
<hr>
<a name="Function-definitions"></a>
<div class="header">
<p>
Previous: <a href="#Flow-control-elements" accesskey="p" rel="prev">Flow control elements</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-definitions-1"></a>
<h4 class="subsection">10.4.6 Function definitions</h4>
<p>A
<a name="index-function-31"></a>
function definition is of the form
</p>
<div class="example">
<pre class="example">function ( <var>arglist</var> ) <var>body</var>
</pre></div>
<p>The function body is an expression, often a compound expression. The
<var>arglist</var> is a comma-separated list of items each of which can be an
identifier, or of the form &lsquo;<samp><var>identifier</var> = <var>default</var></samp>&rsquo;, or
the special token &lsquo;<samp>...</samp>&rsquo;. The <var>default</var> can be any valid
expression.
</p>
<p>Notice that function arguments unlike list tags, etc., cannot have
&ldquo;strange names&rdquo; given as text strings.
</p>
<p>Internally, a function definition is stored as a function call with
function name <code>function</code> and two arguments, the <var>arglist</var> and
the <var>body</var>. The <var>arglist</var> is stored as a tagged pairlist where
the tags are the argument names and the values are the default
expressions.
</p>
<hr>
<a name="Directives"></a>
<div class="header">
<p>
Previous: <a href="#Expressions" accesskey="p" rel="prev">Expressions</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Directives-1"></a>
<h3 class="section">10.5 Directives</h3>
<a name="index-_0023line"></a>
<p>The parser currently only supports one directive, <code>#line</code>.
This is similar to the C-preprocessor directive of the same name. The
syntax is
</p>
<div class="example">
<pre class="example"><var>#line</var> <var>nn</var> [ <code>&quot;filename&quot;</code> ]
</pre></div>
<p>where <var>nn</var> is an integer line number, and the optional <var>filename</var>
(in required double quotes) names the source file.
</p>
<p>Unlike the C directive, <code>#line</code> must appear as the first five characters
on a line. As in C, <var>nn</var> and <code>&quot;filename&quot;</code> entries may be separated
from it by whitespace. And unlike C, any following text on the line will be
treated as a comment and ignored.
</p>
<p>This directive tells the parser that the following line should be assumed to
be line <var>nn</var> of file <var>filename</var>. (If the filename is not given,
it is assumed to be the same as for the previous directive.) This is not
typically used by users, but may be used by preprocessors so that
diagnostic messages refer to the original file.
</p>
<hr>
<a name="Function-and-Variable-Index"></a>
<div class="header">
<p>
Next: <a href="#Concept-Index" accesskey="n" rel="next">Concept Index</a>, Previous: <a href="#Parser" accesskey="p" rel="prev">Parser</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Function-and-Variable-Index-1"></a>
<h2 class="unnumbered">Function and Variable Index</h2>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-1"><b>#</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-2"><b>$</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-3"><b>.</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-4"><b>[</b></a>
&nbsp;
<br>
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-A"><b>A</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-B"><b>B</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-D"><b>D</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-E"><b>E</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-F"><b>F</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-G"><b>G</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-I"><b>I</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-M"><b>M</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-N"><b>N</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-O"><b>O</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-P"><b>P</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-Q"><b>Q</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-R"><b>R</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-S"><b>S</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-T"><b>T</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-U"><b>U</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-W"><b>W</b></a>
&nbsp;
</td></tr></table>
<table summary="" class="index-vr" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_symbol-1">#</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0023"><code>#</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Comments">Comments</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_symbol-2">$</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0024"><code>$</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing">Indexing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0024-1"><code>$</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-constructions">Index constructions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_symbol-3">.</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eC"><code>.C</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eCall"><code>.Call</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eExternal"><code>.External</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eFortran"><code>.Fortran</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eInternal-1"><code>.Internal</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#g_t_002eInternal-and-_002ePrimitive">.Internal and .Primitive</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002ePrimitive-1"><code>.Primitive</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#g_t_002eInternal-and-_002ePrimitive">.Internal and .Primitive</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_symbol-4">[</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_005b"><code>[</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing">Indexing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_005b-1"><code>[</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-constructions">Index constructions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_005b_005b"><code>[[</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing">Indexing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_005b_005b-1"><code>[[</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Index-constructions">Index constructions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-A">A</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002ecall"><code>as.call</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002echaracter"><code>as.character</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002efunction"><code>as.function</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002elist"><code>as.list</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-as_002ename"><code>as.name</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assign"><code>assign</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Identifiers">Identifiers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attr"><code>attr</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attr_003c_002d"><code>attr&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attributes-1"><code>attributes</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attributes_003c_002d"><code>attributes&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-B">B</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-baseenv"><code>baseenv</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Environment-objects">Environment objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-basename"><code>basename</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-body"><code>body</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-body-1"><code>body</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-body_003c_002d"><code>body&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-break"><code>break</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Looping">Looping</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-browser"><code>browser</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#browser">browser</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-D">D</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-debug"><code>debug</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#debug_002fundebug">debug/undebug</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-dirname"><code>dirname</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-do_002ecall"><code>do.call</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-E">E</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-emptyenv"><code>emptyenv</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Environment-objects">Environment objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-20"><code>environment</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-21"><code>environment</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment_003c_002d"><code>environment&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-eval"><code>eval</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#More-on-evaluation">More on evaluation</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-F">F</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002eaccess"><code>file.access</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002eappend"><code>file.append</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002echoose"><code>file.choose</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002ecopy"><code>file.copy</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002ecreate"><code>file.create</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002eexists"><code>file.exists</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002einfo"><code>file.info</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002epath"><code>file.path</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002eremove"><code>file.remove</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002erename"><code>file.rename</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-file_002eshow"><code>file.show</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-for"><code>for</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#for">for</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-formals"><code>formals</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-formals-1"><code>formals</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-formals_003c_002d"><code>formals&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-G">G</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-get"><code>get</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Identifiers">Identifiers</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-I">I</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-is_002ena"><code>is.na</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-is_002enan"><code>is.nan</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-M">M</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-match_002earg"><code>match.arg</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-matching">Argument matching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-match_002ecall"><code>match.call</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-matching">Argument matching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-match_002ecall-1"><code>match.call</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-match_002efun"><code>match.fun</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-matching">Argument matching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-missing"><code>missing</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-mode-3"><code>mode</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-N">N</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-NA"><code>NA</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-NA-1"><code>NA</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-names"><code>names</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Names">Names</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-names_003c_002d"><code>names&lt;-</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Names">Names</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-NaN"><code>NaN</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-new_002eenv"><code>new.env</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Environment-objects">Environment objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-next"><code>next</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Looping">Looping</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-NextMethod"><code>NextMethod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NextMethod">NextMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-NULL"><code>NULL</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#NULL-object">NULL object</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-O">O</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-on_002eexit"><code>on.exit</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#on_002eexit">on.exit</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-P">P</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-pairlist"><code>pairlist</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Pairlist-objects">Pairlist objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-path_002eexpand"><code>path.expand</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-proc_002etime"><code>proc.time</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-Q">Q</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-quote"><code>quote</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-R">R</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-repeat"><code>repeat</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#repeat">repeat</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-S">S</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-stop"><code>stop</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#stop">stop</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-storage_002emode"><code>storage.mode</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-substitute"><code>substitute</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Substitutions">Substitutions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-switch"><code>switch</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#switch">switch</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002egetenv"><code>Sys.getenv</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002egetlocale"><code>Sys.getlocale</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002elocaleconv"><code>Sys.localeconv</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002eputenv"><code>Sys.putenv</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002eputlocale"><code>Sys.putlocale</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002etime"><code>Sys.time</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-Sys_002etimezone"><code>Sys.timezone</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-system"><code>system</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-system_002etime"><code>system.time</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-T">T</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-trace"><code>trace</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#trace_002funtrace">trace/untrace</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-traceback"><code>traceback</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#traceback">traceback</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-typeof"><code>typeof</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-U">U</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-undebug"><code>undebug</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#debug_002fundebug">debug/undebug</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-unlink"><code>unlink</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-untrace"><code>untrace</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#trace_002funtrace">trace/untrace</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-UseMethod"><code>UseMethod</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Function-and-Variable-Index_vr_letter-W">W</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-warning"><code>warning</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#warning">warning</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-warnings"><code>warnings</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#warning">warning</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-while"><code>while</code></a>:</td><td>&nbsp;</td><td valign="top"><a href="#while">while</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-1"><b>#</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-2"><b>$</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-3"><b>.</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_symbol-4"><b>[</b></a>
&nbsp;
<br>
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-A"><b>A</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-B"><b>B</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-D"><b>D</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-E"><b>E</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-F"><b>F</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-G"><b>G</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-I"><b>I</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-M"><b>M</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-N"><b>N</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-O"><b>O</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-P"><b>P</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-Q"><b>Q</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-R"><b>R</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-S"><b>S</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-T"><b>T</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-U"><b>U</b></a>
&nbsp;
<a class="summary-letter" href="#Function-and-Variable-Index_vr_letter-W"><b>W</b></a>
&nbsp;
</td></tr></table>
<hr>
<a name="Concept-Index"></a>
<div class="header">
<p>
Next: <a href="#References" accesskey="n" rel="next">References</a>, Previous: <a href="#Function-and-Variable-Index" accesskey="p" rel="prev">Function and Variable Index</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="Concept-Index-1"></a>
<h2 class="unnumbered">Concept Index</h2>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-Index_cp_symbol-1"><b>#</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_symbol-2"><b>.</b></a>
&nbsp;
<br>
<a class="summary-letter" href="#Concept-Index_cp_letter-A"><b>A</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-B"><b>B</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-C"><b>C</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-E"><b>E</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-F"><b>F</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-I"><b>I</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-M"><b>M</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-N"><b>N</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-O"><b>O</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-P"><b>P</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-S"><b>S</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-T"><b>T</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-V"><b>V</b></a>
&nbsp;
</td></tr></table>
<table summary="" class="index-cp" border="0">
<tr><td></td><th align="left">Index Entry</th><td>&nbsp;</td><th align="left"> Section</th></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_symbol-1">#</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_0023line">#line</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Directives">Directives</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_symbol-2">.</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002eInternal">.Internal</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-_002ePrimitive">.Primitive</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-A">A</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-argument">argument</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-argument-1">argument</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-argument_002c-default-values">argument, default values</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arguments">Arguments</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-1">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls">Function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-2">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operators">Operators</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-3">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Subset-assignment">Subset assignment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-4">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Global-environment">Global environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-5">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-6">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-7">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-8">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-9">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#More-on-evaluation">More on evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-10">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-11">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Infix-and-prefix-operators">Infix and prefix operators</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-assignment-12">assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Infix-and-prefix-operators">Infix and prefix operators</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-atomic">atomic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-objects">Vector objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-attributes">attributes</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-B">B</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-binding">binding</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-binding-1">binding</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-C">C</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-call">call</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-call-stack">call stack</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Stacks">Stacks</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coercion">coercion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coercion-1">coercion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coercion-2">coercion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Any_002dtype">Any-type</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coercion-3">coercion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Classes">Classes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-coercion-4">coercion</a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-comments">comments</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Comments">Comments</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-complex-assignment">complex assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Subset-assignment">Subset assignment</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-E">E</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-1">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-2">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Promise-objects">Promise objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-3">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Environment-objects">Environment objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-4">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Control-structures">Control structures</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-5">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Global-environment">Global environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-6">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-7">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Stacks">Stacks</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-8">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Search-path">Search path</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-9">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Evaluation-environment">Evaluation environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-10">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-11">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-12">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-13">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-14">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-15">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#More-on-evaluation">More on evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-16">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-17">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operating-system-access">Operating system access</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-18">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Debugging">Debugging</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment-19">environment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Debugging">Debugging</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment_002c-evaluation">environment, evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment_002c-evaluation-1">environment, evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-environment_002c-evaluation-2">environment, evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Stacks">Stacks</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-1">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Evaluation-environment">Evaluation environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-2">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-3">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-4">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Inheritance">Inheritance</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-5">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#UseMethod">UseMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-6">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#More-on-evaluation">More on evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation-7">evaluation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-argument">evaluation, argument</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-expression">evaluation, expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Expression-objects">Expression objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-expression-1">evaluation, expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Promise-objects">Promise objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-expression-2">evaluation, expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arguments">Arguments</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-lazy">evaluation, lazy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-lazy-1">evaluation, lazy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Substitutions">Substitutions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-lazy-2">evaluation, lazy</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Substitutions">Substitutions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-statement">evaluation, statement</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Control-structures">Control structures</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-symbol">evaluation, symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-symbol-1">evaluation, symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-lookup">Symbol lookup</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-evaluation_002c-symbol-2">evaluation, symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-expression">expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Introduction">Introduction</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-expression-1">expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-expression-2">expression</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Separators">Separators</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-expression-object">expression object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Expression-objects">Expression objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-expression-object-1">expression object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Expression-objects">Expression objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-F">F</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-frame">frame</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-1">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-2">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-objects">Function objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-3">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-4">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-5">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Promise-objects">Promise objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-6">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Dot_002ddot_002ddot">Dot-dot-dot</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-7">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls">Function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-8">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-9">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Lexical-environment">Lexical environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-10">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Stacks">Stacks</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-11">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Writing-functions">Writing functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-12">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-13">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-14">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arguments">Arguments</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-15">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Evaluation-environment">Evaluation environment</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-16">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-matching">Argument matching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-17">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-18">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-19">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-20">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-21">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-22">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-23">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-24">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-25">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Definition">Definition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-26">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-27">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-28">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-29">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Internal-representation">Internal representation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-30">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls-_0028expressions_0029">Function calls (expressions)</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-31">function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-definitions">Function definitions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-argument">function argument</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Promise-objects">Promise objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-argument-1">function argument</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Dot_002ddot_002ddot">Dot-dot-dot</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-arguments">function arguments</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls">Function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function-invocation">function invocation</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls">Function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-accessor">function, accessor</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-anonymous">function, anonymous</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-assignment">function, assignment</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Function-calls">Function calls</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-1">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Definition">Definition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-2">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Definition">Definition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-3">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Definition">Definition</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-4">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Inheritance">Inheritance</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-5">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Method-dispatching">Method dispatching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-6">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Writing-methods">Writing methods</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-generic-7">function, generic</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Writing-methods">Writing methods</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-internal">function, internal</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-internal-1">function, internal</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Group-methods">Group methods</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-function_002c-modeling">function, modeling</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-I">I</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-identifier">identifier</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Identifiers">Identifiers</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-objects">Vector objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index-1">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#List-objects">List objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index-2">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing">Indexing</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index-3">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index-4">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing-matrices-and-arrays">Indexing matrices and arrays</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-index-5">index</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing-matrices-and-arrays">Indexing matrices and arrays</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-M">M</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-mode">mode</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-mode-1">mode</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-objects">Vector objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-mode-2">mode</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-modeling-function">modeling function</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Factors">Factors</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-N">N</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-name">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-1">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-2">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-lookup">Symbol lookup</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-3">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Propagation-of-names">Propagation of names</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-4">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope-of-variables">Scope of variables</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-5">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Arguments">Arguments</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-6">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-matching">Argument matching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-7">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Argument-evaluation">Argument evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-8">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Method-dispatching">Method dispatching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-9">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#NextMethod">NextMethod</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-10">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Direct-manipulation-of-language-objects">Direct manipulation of language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-name-11">name</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Debugging">Debugging</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-namespace">namespace</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Search-path">Search path</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-O">O</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-object">object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object-1">object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object-2">object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object-3">object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Attributes">Attributes</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object-4">object</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Method-dispatching">Method dispatching</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object_002doriented">object-oriented</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-object_002doriented-1">object-oriented</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Definition">Definition</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-P">P</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-1">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-2">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Evaluation-of-expressions">Evaluation of expressions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-3">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Computing-on-the-language">Computing on the language</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-4">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Direct-manipulation-of-language-objects">Direct manipulation of language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-5">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Substitutions">Substitutions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-6">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Parser">Parser</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-parsing-7">parsing</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Internal-representation">Internal representation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-partial-matching">partial matching</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-promise">promise</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Promise-objects">Promise objects</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-S">S</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope-of-variables">Scope of variables</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope-1">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Stacks">Stacks</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope-2">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope-3">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope-4">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-scope-5">scope</a>:</td><td>&nbsp;</td><td valign="top"><a href="#More-on-evaluation">More on evaluation</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-search-path">search path</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Search-path">Search path</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-statement">statement</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Language-objects">Language objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol-1">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-objects">Symbol objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol-2">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-lookup">Symbol lookup</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol-3">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Scope">Scope</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol-4">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Substitutions">Substitutions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-symbol-5">symbol</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-T">T</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-token">token</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Expression-objects">Expression objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type-1">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type-2">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Basic-types">Basic types</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type-3">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-objects">Vector objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type-4">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Names">Names</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-type-5">type</a>:</td><td>&nbsp;</td><td valign="top"><a href="#NA-handling">NA handling</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
<tr><th><a name="Concept-Index_cp_letter-V">V</a></th><td></td><td></td></tr>
<tr><td></td><td valign="top"><a href="#index-value">value</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Symbol-lookup">Symbol lookup</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-variable">variable</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Objects">Objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-vector">vector</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Vector-objects">Vector objects</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-vector-1">vector</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Dimensions">Dimensions</a></td></tr>
<tr><td></td><td valign="top"><a href="#index-vector-2">vector</a>:</td><td>&nbsp;</td><td valign="top"><a href="#Operators">Operators</a></td></tr>
<tr><td colspan="4"> <hr></td></tr>
</table>
<table summary=""><tr><th valign="top">Jump to: &nbsp; </th><td><a class="summary-letter" href="#Concept-Index_cp_symbol-1"><b>#</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_symbol-2"><b>.</b></a>
&nbsp;
<br>
<a class="summary-letter" href="#Concept-Index_cp_letter-A"><b>A</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-B"><b>B</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-C"><b>C</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-E"><b>E</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-F"><b>F</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-I"><b>I</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-M"><b>M</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-N"><b>N</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-O"><b>O</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-P"><b>P</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-S"><b>S</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-T"><b>T</b></a>
&nbsp;
<a class="summary-letter" href="#Concept-Index_cp_letter-V"><b>V</b></a>
&nbsp;
</td></tr></table>
<hr>
<a name="References"></a>
<div class="header">
<p>
Previous: <a href="#Concept-Index" accesskey="p" rel="prev">Concept Index</a>, Up: <a href="#Top" accesskey="u" rel="up">Top</a> &nbsp; [<a href="#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="#Function-and-Variable-Index" title="Index" rel="index">Index</a>]</p>
</div>
<a name="References-1"></a>
<h2 class="appendix">Appendix A References</h2>
<p>Richard A. Becker, John M. Chambers and Allan R. Wilks (1988),
<em>The New S Language.</em> Chapman &amp; Hall, New York.
This book is often called the &ldquo;<em>Blue Book</em>&rdquo;.
</p>
<div class="footnote">
<hr>
<h4 class="footnotes-heading">Footnotes</h4>
<h3><a name="FOOT1" href="#DOCF1">(1)</a></h3>
<p>actually two, but this draft
manual predates the <strong>methods</strong> package.</p>
<h3><a name="FOOT2" href="#DOCF2">(2)</a></h3>
<p>Evaluation always takes place in an
<a name="index-environment-4"></a>
environment.
See <a href="#Scope-of-variables">Scope of variables</a> for more details.</p>
<h3><a name="FOOT3" href="#DOCF3">(3)</a></h3>
<p>Looping is the repeated evaluation of a statement or
block of statements.</p>
<h3><a name="FOOT4" href="#DOCF4">(4)</a></h3>
<p>such as <code>U+A0</code>, non-breaking space,
and <code>U+3000</code>, ideographic space.</p>
</div>
<hr>
</body>
</html>