0byt3m1n1
Path:
C:
/
Program Files
/
R
/
R-4.4.1
/
doc
/
manual
/
[
Home
]
File: R-lang.html
<!DOCTYPE html> <html> <!-- Created by GNU Texinfo 7.1, https://www.gnu.org/software/texinfo/ --> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <!-- This manual is for R, version 4.4.1 (2024-06-14). Copyright © 2000-2024 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. --> <title>R Language Definition</title> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <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 name="viewport" content="width=device-width,initial-scale=1"> <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"> <link href="#Introduction" rel="next" title="Introduction"> <style type="text/css"> <!-- a.copiable-link {visibility: hidden; text-decoration: none; line-height: 0em} a.summary-letter-printindex {text-decoration: none} div.example {margin-left: 3.2em} kbd.key {font-style: normal} span.r {font-family: initial; font-weight: normal; font-style: normal} span:hover a.copiable-link {visibility: visible} td.printindex-index-entry {vertical-align: top} td.printindex-index-section {vertical-align: top; padding-left: 1em} th.entries-header-printindex {text-align:left} th.sections-header-printindex {text-align:left; padding-left: 1em} ul.mark-bullet {list-style-type: disc} ul.toc-numbered-mark {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; } /* hide top navigation bar in Texinfo >= 6.8 output */ #Top > .header, #Top > .nav-panel { display: none } --> </style> </head> <body lang="en"> <div class="top-level-extent" id="Top"> <div class="nav-panel"> <p> Next: <a href="#Introduction" accesskey="n" rel="next">Introduction</a> [<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> <h1 class="top" id="R-Language-Definition"><span>R Language Definition<a class="copiable-link" href="#R-Language-Definition"> ¶</a></span></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 4.4.1 (2024-06-14). </p> <p>Copyright © 2000–2024 R Core Team </p> <blockquote class="quotation"> <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> <div class="element-contents" id="SEC_Contents"> <h2 class="contents-heading">Table of Contents</h2> <div class="contents"> <ul class="toc-numbered-mark"> <li><a id="toc-Introduction-1" href="#Introduction">1 Introduction</a></li> <li><a id="toc-Objects-1" href="#Objects">2 Objects</a> <ul class="toc-numbered-mark"> <li><a id="toc-Basic-types-1" href="#Basic-types">2.1 Basic types</a> <ul class="toc-numbered-mark"> <li><a id="toc-Vectors" href="#Vector-objects">2.1.1 Vectors</a></li> <li><a id="toc-Lists" href="#List-objects">2.1.2 Lists</a></li> <li><a id="toc-Language-objects-1" href="#Language-objects">2.1.3 Language objects</a> <ul class="toc-numbered-mark"> <li><a id="toc-Symbol-objects-1" href="#Symbol-objects">2.1.3.1 Symbol objects</a></li> </ul></li> <li><a id="toc-Expression-objects-1" href="#Expression-objects">2.1.4 Expression objects</a></li> <li><a id="toc-Function-objects-1" href="#Function-objects">2.1.5 Function objects</a></li> <li><a id="toc-NULL" href="#NULL-object">2.1.6 NULL</a></li> <li><a id="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 id="toc-Promise-objects-1" href="#Promise-objects">2.1.8 Promise objects</a></li> <li><a id="toc-Dot_002ddot_002ddot-1" href="#Dot_002ddot_002ddot">2.1.9 Dot-dot-dot</a></li> <li><a id="toc-Environments" href="#Environment-objects">2.1.10 Environments</a></li> <li><a id="toc-Pairlist-objects-1" href="#Pairlist-objects">2.1.11 Pairlist objects</a></li> <li><a id="toc-The-_0060_0060Any_0027_0027-type" href="#Any_002dtype">2.1.12 The “Any” type</a></li> </ul></li> <li><a id="toc-Attributes-1" href="#Attributes">2.2 Attributes</a> <ul class="toc-numbered-mark"> <li><a id="toc-Names-1" href="#Names">2.2.1 Names</a></li> <li><a id="toc-Dimensions-1" href="#Dimensions">2.2.2 Dimensions</a></li> <li><a id="toc-Dimnames-1" href="#Dimnames">2.2.3 Dimnames</a></li> <li><a id="toc-Classes-1" href="#Classes">2.2.4 Classes</a></li> <li><a id="toc-Time-series-attributes-1" href="#Time-series-attributes">2.2.5 Time series attributes</a></li> <li><a id="toc-Copying-of-attributes-1" href="#Copying-of-attributes">2.2.6 Copying of attributes</a></li> </ul></li> <li><a id="toc-Special-compound-objects-1" href="#Special-compound-objects">2.3 Special compound objects</a> <ul class="toc-numbered-mark"> <li><a id="toc-Factors-1" href="#Factors">2.3.1 Factors</a></li> <li><a id="toc-Data-frame-objects-1" href="#Data-frame-objects">2.3.2 Data frame objects</a></li> </ul></li> </ul></li> <li><a id="toc-Evaluation-of-expressions-1" href="#Evaluation-of-expressions">3 Evaluation of expressions</a> <ul class="toc-numbered-mark"> <li><a id="toc-Simple-evaluation-1" href="#Simple-evaluation">3.1 Simple evaluation</a> <ul class="toc-numbered-mark"> <li><a id="toc-Constants-1" href="#Constants">3.1.1 Constants</a></li> <li><a id="toc-Symbol-lookup-1" href="#Symbol-lookup">3.1.2 Symbol lookup</a></li> <li><a id="toc-Function-calls-1" href="#Function-calls">3.1.3 Function calls</a></li> <li><a id="toc-Operators-1" href="#Operators">3.1.4 Operators</a></li> </ul></li> <li><a id="toc-Control-structures-1" href="#Control-structures">3.2 Control structures</a> <ul class="toc-numbered-mark"> <li><a id="toc-if-1" href="#if">3.2.1 if</a></li> <li><a id="toc-Looping-1" href="#Looping">3.2.2 Looping</a></li> <li><a id="toc-repeat-1" href="#repeat">3.2.3 repeat</a></li> <li><a id="toc-while-1" href="#while">3.2.4 while</a></li> <li><a id="toc-for-1" href="#for">3.2.5 for</a></li> <li><a id="toc-switch-1" href="#switch">3.2.6 switch</a></li> </ul></li> <li><a id="toc-Elementary-arithmetic-operations-1" href="#Elementary-arithmetic-operations">3.3 Elementary arithmetic operations</a> <ul class="toc-numbered-mark"> <li><a id="toc-Recycling-rules-1" href="#Recycling-rules">3.3.1 Recycling rules</a></li> <li><a id="toc-Propagation-of-names-1" href="#Propagation-of-names">3.3.2 Propagation of names</a></li> <li><a id="toc-Dimensional-attributes-1" href="#Dimensional-attributes">3.3.3 Dimensional attributes</a></li> <li><a id="toc-NA-handling-1" href="#NA-handling">3.3.4 NA handling</a></li> </ul></li> <li><a id="toc-Indexing-1" href="#Indexing">3.4 Indexing</a> <ul class="toc-numbered-mark"> <li><a id="toc-Indexing-by-vectors-1" href="#Indexing-by-vectors">3.4.1 Indexing by vectors</a></li> <li><a id="toc-Indexing-matrices-and-arrays-1" href="#Indexing-matrices-and-arrays">3.4.2 Indexing matrices and arrays</a></li> <li><a id="toc-Indexing-other-structures-1" href="#Indexing-other-structures">3.4.3 Indexing other structures</a></li> <li><a id="toc-Subset-assignment-1" href="#Subset-assignment">3.4.4 Subset assignment</a></li> </ul></li> <li><a id="toc-Scope-of-variables-1" href="#Scope-of-variables">3.5 Scope of variables</a> <ul class="toc-numbered-mark"> <li><a id="toc-Global-environment-1" href="#Global-environment">3.5.1 Global environment</a></li> <li><a id="toc-Lexical-environment-1" href="#Lexical-environment">3.5.2 Lexical environment</a></li> <li><a id="toc-The-call-stack" href="#Stacks">3.5.3 The call stack</a></li> <li><a id="toc-Search-path-1" href="#Search-path">3.5.4 Search path</a></li> </ul></li> </ul></li> <li><a id="toc-Functions-1" href="#Functions">4 Functions</a> <ul class="toc-numbered-mark"> <li><a id="toc-Writing-functions-1" href="#Writing-functions">4.1 Writing functions</a> <ul class="toc-numbered-mark"> <li><a id="toc-Syntax-and-examples-1" href="#Syntax-and-examples">4.1.1 Syntax and examples</a></li> <li><a id="toc-Arguments-1" href="#Arguments">4.1.2 Arguments</a></li> </ul></li> <li><a id="toc-Functions-as-objects-1" href="#Functions-as-objects">4.2 Functions as objects</a></li> <li><a id="toc-Evaluation-1" href="#Evaluation">4.3 Evaluation</a> <ul class="toc-numbered-mark"> <li><a id="toc-Evaluation-environment-1" href="#Evaluation-environment">4.3.1 Evaluation environment</a></li> <li><a id="toc-Argument-matching-1" href="#Argument-matching">4.3.2 Argument matching</a></li> <li><a id="toc-Argument-evaluation-1" href="#Argument-evaluation">4.3.3 Argument evaluation</a></li> <li><a id="toc-Scope-1" href="#Scope">4.3.4 Scope</a></li> </ul></li> </ul></li> <li><a id="toc-Object_002doriented-programming-1" href="#Object_002doriented-programming">5 Object-oriented programming</a> <ul class="toc-numbered-mark"> <li><a id="toc-Definition-1" href="#Definition">5.1 Definition</a></li> <li><a id="toc-Inheritance-1" href="#Inheritance">5.2 Inheritance</a></li> <li><a id="toc-Method-dispatching-1" href="#Method-dispatching">5.3 Method dispatching</a></li> <li><a id="toc-UseMethod-1" href="#UseMethod">5.4 <code class="code">UseMethod</code></a></li> <li><a id="toc-NextMethod-1" href="#NextMethod">5.5 <code class="code">NextMethod</code></a></li> <li><a id="toc-Group-methods-1" href="#Group-methods">5.6 Group methods</a></li> <li><a id="toc-Writing-methods-1" href="#Writing-methods">5.7 Writing methods</a></li> </ul></li> <li><a id="toc-Computing-on-the-language-1" href="#Computing-on-the-language">6 Computing on the language</a> <ul class="toc-numbered-mark"> <li><a id="toc-Direct-manipulation-of-language-objects-1" href="#Direct-manipulation-of-language-objects">6.1 Direct manipulation of language objects</a></li> <li><a id="toc-Substitutions-1" href="#Substitutions">6.2 Substitutions</a></li> <li><a id="toc-More-on-evaluation-1" href="#More-on-evaluation">6.3 More on evaluation</a></li> <li><a id="toc-Evaluation-of-expression-objects-1" href="#Evaluation-of-expression-objects">6.4 Evaluation of expression objects</a></li> <li><a id="toc-Manipulation-of-function-calls-1" href="#Manipulation-of-function-calls">6.5 Manipulation of function calls</a></li> <li><a id="toc-Manipulation-of-functions-1" href="#Manipulation-of-functions">6.6 Manipulation of functions</a></li> </ul></li> <li><a id="toc-System-and-foreign-language-interfaces-1" href="#System-and-foreign-language-interfaces">7 System and foreign language interfaces</a> <ul class="toc-numbered-mark"> <li><a id="toc-Operating-system-access-1" href="#Operating-system-access">7.1 Operating system access</a></li> <li><a id="toc-Foreign-language-interfaces-1" href="#Foreign-language-interfaces">7.2 Foreign language interfaces</a></li> <li><a id="toc-_002eInternal-and-_002ePrimitive-1" href="#g_t_002eInternal-and-_002ePrimitive">7.3 .Internal and .Primitive</a></li> </ul></li> <li><a id="toc-Exception-handling-1" href="#Exception-handling">8 Exception handling</a> <ul class="toc-numbered-mark"> <li><a id="toc-stop-1" href="#stop">8.1 stop</a></li> <li><a id="toc-warning-1" href="#warning">8.2 warning</a></li> <li><a id="toc-on_002eexit-1" href="#on_002eexit">8.3 on.exit</a></li> <li><a id="toc-Error-options-1" href="#Error-options">8.4 Error options</a></li> </ul></li> <li><a id="toc-Debugging-1" href="#Debugging">9 Debugging</a> <ul class="toc-numbered-mark"> <li><a id="toc-browser-1" href="#browser">9.1 browser</a></li> <li><a id="toc-debug_002fundebug-1" href="#debug_002fundebug">9.2 <code class="code">debug</code>/<code class="code">undebug</code></a></li> <li><a id="toc-trace_002funtrace-1" href="#trace_002funtrace">9.3 trace/untrace</a></li> <li><a id="toc-traceback-1" href="#traceback">9.4 traceback</a></li> </ul></li> <li><a id="toc-Parser-1" href="#Parser">10 Parser</a> <ul class="toc-numbered-mark"> <li><a id="toc-The-parsing-process-1" href="#The-parsing-process">10.1 The parsing process</a> <ul class="toc-numbered-mark"> <li><a id="toc-Modes-of-parsing-1" href="#Modes-of-parsing">10.1.1 Modes of parsing</a></li> <li><a id="toc-Internal-representation-1" href="#Internal-representation">10.1.2 Internal representation</a></li> <li><a id="toc-Deparsing-1" href="#Deparsing">10.1.3 Deparsing</a></li> </ul></li> <li><a id="toc-Comments-1" href="#Comments">10.2 Comments</a></li> <li><a id="toc-Tokens-1" href="#Tokens">10.3 Tokens</a> <ul class="toc-numbered-mark"> <li><a id="toc-Constants-2" href="#Literal-constants">10.3.1 Constants</a></li> <li><a id="toc-Identifiers-1" href="#Identifiers">10.3.2 Identifiers</a></li> <li><a id="toc-Reserved-words-1" href="#Reserved-words">10.3.3 Reserved words</a></li> <li><a id="toc-Special-operators-1" href="#Special-operators">10.3.4 Special operators</a></li> <li><a id="toc-Separators-1" href="#Separators">10.3.5 Separators</a></li> <li><a id="toc-Operator-tokens-1" href="#Operator-tokens">10.3.6 Operator tokens</a></li> <li><a id="toc-Grouping-1" href="#Grouping">10.3.7 Grouping</a></li> <li><a id="toc-Indexing-tokens-1" href="#Indexing-tokens">10.3.8 Indexing tokens</a></li> </ul></li> <li><a id="toc-Expressions-1" href="#Expressions">10.4 Expressions</a> <ul class="toc-numbered-mark"> <li><a id="toc-Function-calls-2" href="#Function-calls-_0028expressions_0029">10.4.1 Function calls</a></li> <li><a id="toc-Infix-and-prefix-operators-1" href="#Infix-and-prefix-operators">10.4.2 Infix and prefix operators</a></li> <li><a id="toc-Index-constructions-1" href="#Index-constructions">10.4.3 Index constructions</a></li> <li><a id="toc-Compound-expressions-1" href="#Compound-expressions">10.4.4 Compound expressions</a></li> <li><a id="toc-Flow-control-elements-1" href="#Flow-control-elements">10.4.5 Flow control elements</a></li> <li><a id="toc-Function-definitions-1" href="#Function-definitions">10.4.6 Function definitions</a></li> </ul></li> <li><a id="toc-Directives-1" href="#Directives">10.5 Directives</a></li> </ul></li> <li><a id="toc-Function-and-Variable-Index-1" href="#Function-and-Variable-Index" rel="index">Function and Variable Index</a></li> <li><a id="toc-Concept-Index-1" href="#Concept-Index" rel="index">Concept Index</a></li> <li><a id="toc-References-1" href="#References">Appendix A References</a></li> </ul> </div> </div> <hr> <div class="chapter-level-extent" id="Introduction"> <div class="nav-panel"> <p> Next: <a href="#Objects" accesskey="n" rel="next">Objects</a>, Previous: <a href="#Top" accesskey="p" rel="prev">R Language Definition</a>, Up: <a href="#Top" accesskey="u" rel="up">R Language Definition</a> [<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> <h2 class="chapter" id="Introduction-1"><span>1 Introduction<a class="copiable-link" href="#Introduction-1"> ¶</a></span></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 <abbr class="abbr">ACM</abbr> Software Systems Award for S. </p> <p>The language syntax has a superficial similarity with C, but the semantics are of the <abbr class="abbr">FPL</abbr> (functional programming language) variety with stronger affinities with Lisp and <abbr class="acronym">APL</abbr>. In particular, it allows “computing on the language”, 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 class="index-entry-id" id="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 class="emph">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’ reference manual. Other manuals provide complementary information: in particular <a data-manual="R-intro" href="https://cran.r-project.org/doc/manuals/R-intro.html#Preface">Preface</a> in <cite class="cite">An Introduction to R</cite> provides an introduction to R and <a data-manual="R-exts" href="https://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-language-interfaces">System and foreign language interfaces</a> in <cite class="cite">Writing R Extensions</cite> details how to extend R using compiled code. </p> <hr> </div> <div class="chapter-level-extent" id="Objects"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Objects-1"><span>2 Objects<a class="copiable-link" href="#Objects-1"> ¶</a></span></h2> <p>In every computer language <a class="index-entry-id" id="index-variable"></a> variables provide a means of accessing the data stored in memory. R does not provide direct access to the computer’s memory but rather provides a number of specialized data structures we will refer to as <a class="index-entry-id" id="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 class="code">typeof</code> <a class="index-entry-id" id="index-typeof"></a> <a class="index-entry-id" id="index-type"></a> returns the <em class="dfn">type</em> of an R object. Note that in the C code underlying R, all objects are pointers to a structure with typedef <code class="code">SEXPREC</code>; the different R data types are represented in C by <code class="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 class="code">typeof</code> and what they are. </p> <blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="20%"><code class="code">"NULL"</code></td><td width="70%">NULL</td></tr> <tr><td width="20%"><code class="code">"symbol"</code></td><td width="70%">a variable name</td></tr> <tr><td width="20%"><code class="code">"pairlist"</code></td><td width="70%">a pairlist object (mainly internal)</td></tr> <tr><td width="20%"><code class="code">"closure"</code></td><td width="70%">a function</td></tr> <tr><td width="20%"><code class="code">"environment"</code></td><td width="70%">an environment <a class="index-entry-id" id="index-evaluation_002c-lazy"></a></td></tr> <tr><td width="20%"><code class="code">"promise"</code></td><td width="70%">an object used to implement lazy evaluation</td></tr> <tr><td width="20%"><code class="code">"language"</code></td><td width="70%">an R language construct</td></tr> <tr><td width="20%"><code class="code">"special"</code></td><td width="70%">an internal function that does not evaluate its arguments</td></tr> <tr><td width="20%"><code class="code">"builtin"</code></td><td width="70%">an internal function that evaluates its arguments <a class="anchor" id="typeof_002dchar"></a></td></tr> <tr><td width="20%"><code class="code">"char"</code></td><td width="70%">a ‘scalar’ string object (internal only) ***</td></tr> <tr><td width="20%"><code class="code">"logical"</code></td><td width="70%">a vector containing logical values</td></tr> <tr><td width="20%"><code class="code">"integer"</code></td><td width="70%">a vector containing integer values</td></tr> <tr><td width="20%"><code class="code">"double"</code></td><td width="70%">a vector containing real values</td></tr> <tr><td width="20%"><code class="code">"complex"</code></td><td width="70%">a vector containing complex values</td></tr> <tr><td width="20%"><code class="code">"character"</code></td><td width="70%">a vector containing character values</td></tr> <tr><td width="20%"><code class="code">"..."</code></td><td width="70%">the special variable length argument ***</td></tr> <tr><td width="20%"><code class="code">"any"</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 class="code">"expression"</code></td><td width="70%">an expression object</td></tr> <tr><td width="20%"><code class="code">"list"</code></td><td width="70%">a list</td></tr> <tr><td width="20%"><code class="code">"bytecode"</code></td><td width="70%">byte code (internal only) ***</td></tr> <tr><td width="20%"><code class="code">"externalptr"</code></td><td width="70%">an external pointer object</td></tr> <tr><td width="20%"><code class="code">"weakref"</code></td><td width="70%">a weak reference object</td></tr> <tr><td width="20%"><code class="code">"raw"</code></td><td width="70%">a vector containing bytes</td></tr> <tr><td width="20%"><code class="code">"S4"</code></td><td width="70%">an S4 object which is not a simple object</td></tr> </tbody> </table> </blockquote> <p>Users cannot easily get hold of objects of types marked with a ‘***’. </p> <a class="index-entry-id" id="index-mode-3"></a> <a class="index-entry-id" id="index-mode"></a> <p>Function <code class="code">mode</code> gives information about the <em class="dfn">mode</em> of an object in the sense of Becker, Chambers & Wilks (1988), and is more compatible with other implementations of the S language. <a class="index-entry-id" id="index-storage_002emode"></a> Finally, the function <code class="code">storage.mode</code> returns the <em class="dfn">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 class="code">"numeric"</code>, so their storage modes need to be distinguished.) </p> <div class="example"> <pre class="example-preformatted">> x <- 1:3 > typeof(x) [1] "integer" > mode(x) [1] "numeric" > storage.mode(x) [1] "integer" </pre></div> <p>R <a class="index-entry-id" id="index-object-1"></a> objects are often coerced to different <a class="index-entry-id" id="index-type-1"></a> types during computations. There are also many functions available to perform explicit <a class="index-entry-id" id="index-coercion"></a> coercion. When programming in the R language the type of an object generally doesn’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> <ul class="mini-toc"> <li><a href="#Basic-types" accesskey="1">Basic types</a></li> <li><a href="#Attributes" accesskey="2">Attributes</a></li> <li><a href="#Special-compound-objects" accesskey="3">Special compound objects</a></li> </ul> <hr> <div class="section-level-extent" id="Basic-types"> <div class="nav-panel"> <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> [<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 class="index-entry-id" id="index-type-2"></a> <h3 class="section" id="Basic-types-1"><span>2.1 Basic types<a class="copiable-link" href="#Basic-types-1"> ¶</a></span></h3> <ul class="mini-toc"> <li><a href="#Vector-objects" accesskey="1">Vectors</a></li> <li><a href="#List-objects" accesskey="2">Lists</a></li> <li><a href="#Language-objects" accesskey="3">Language objects</a></li> <li><a href="#Expression-objects" accesskey="4">Expression objects</a></li> <li><a href="#Function-objects" accesskey="5">Function objects</a></li> <li><a href="#NULL-object" accesskey="6">NULL</a></li> <li><a href="#Builtin-objects-and-special-forms" accesskey="7">Builtin objects and special forms</a></li> <li><a href="#Promise-objects" accesskey="8">Promise objects</a></li> <li><a href="#Dot_002ddot_002ddot" accesskey="9">Dot-dot-dot</a></li> <li><a href="#Environment-objects">Environments</a></li> <li><a href="#Pairlist-objects">Pairlist objects</a></li> <li><a href="#Any_002dtype">The “Any” type</a></li> </ul> <hr> <div class="subsection-level-extent" id="Vector-objects"> <div class="nav-panel"> <p> Next: <a href="#List-objects" accesskey="n" rel="next">Lists</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> [<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> <h4 class="subsection" id="Vectors"><span>2.1.1 Vectors<a class="copiable-link" href="#Vectors"> ¶</a></span></h4> <a class="index-entry-id" id="index-vector"></a> <p>Vectors can be thought of as contiguous cells containing data. Cells are accessed through <a class="index-entry-id" id="index-index"></a> indexing operations such as <code class="code">x[5]</code>. More details are given in <a class="ref" href="#Indexing">Indexing</a>. </p> <a class="index-entry-id" id="index-type-3"></a> <a class="index-entry-id" id="index-mode-1"></a> <a class="index-entry-id" id="index-atomic"></a> <p>R has six basic (‘atomic’) vector types: logical, integer, real, complex, character (in C aka ‘string’) and raw. The modes and storage modes for the different vector types are listed in the following table. </p> <blockquote class="quotation"> <table summary="" class="multitable"> <thead><tr><th>typeof</th><th>mode</th><th>storage.mode</th></tr></thead> <tbody><tr><td><code class="code">logical</code></td><td><code class="code">logical</code></td><td><code class="code">logical</code></td></tr> <tr><td><code class="code">integer</code></td><td><code class="code">numeric</code></td><td><code class="code">integer</code></td></tr> <tr><td><code class="code">double</code></td><td><code class="code">numeric</code></td><td><code class="code">double</code></td></tr> <tr><td><code class="code">complex</code></td><td><code class="code">complex</code></td><td><code class="code">complex</code></td></tr> <tr><td><code class="code">character</code></td><td><code class="code">character</code></td><td><code class="code">character</code></td></tr> <tr><td><code class="code">raw</code></td><td><code class="code">raw</code></td><td><code class="code">raw</code></td></tr> </tbody> </table> </blockquote> <p>Single numbers, such as <code class="code">4.2</code>, and strings, such as <code class="code">"four point two"</code> are still vectors, of length 1; there are no more basic types. Vectors with length zero are possible (and useful). </p> <p>A single element of a character vector is often referred to as a <em class="emph">character string</em> or short <em class="emph">string</em>. <a class="footnote" id="DOCF1" href="#FOOT1"><sup>1</sup></a> </p> <hr> </div> <div class="subsection-level-extent" id="List-objects"> <div class="nav-panel"> <p> Next: <a href="#Language-objects" accesskey="n" rel="next">Language objects</a>, Previous: <a href="#Vector-objects" accesskey="p" rel="prev">Vectors</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> [<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> <h4 class="subsection" id="Lists"><span>2.1.2 Lists<a class="copiable-link" href="#Lists"> ¶</a></span></h4> <p>Lists (“generic vectors”) 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 class="index-entry-id" id="index-index-1"></a> indexing operations. These are explained in detail in <a class="ref" href="#Indexing">Indexing</a>. </p> <p>Lists are vectors, and the basic vector types are referred to as <em class="emph">atomic vectors</em> where it is necessary to exclude lists. </p> <hr> </div> <div class="subsection-level-extent" id="Language-objects"> <div class="nav-panel"> <p> Next: <a href="#Expression-objects" accesskey="n" rel="next">Expression objects</a>, Previous: <a href="#List-objects" accesskey="p" rel="prev">Lists</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> [<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> <h4 class="subsection" id="Language-objects-1"><span>2.1.3 Language objects<a class="copiable-link" href="#Language-objects-1"> ¶</a></span></h4> <p>There are three types of objects that constitute the R language. They are <em class="emph">calls</em>, <em class="emph">expressions</em>, and <em class="emph">names</em>. <a class="index-entry-id" id="index-call"></a> <a class="index-entry-id" id="index-expression-1"></a> <a class="index-entry-id" id="index-name"></a> Since R has objects of type <code class="code">"expression"</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 class="emph">statements</em>. <a class="index-entry-id" id="index-statement"></a> </p> <p>These objects have modes <code class="code">"call"</code>, <code class="code">"expression"</code>, and <code class="code">"name"</code>, respectively. </p> <p>They can be created directly from expressions using the <code class="code">quote</code> mechanism and converted to and from lists by the <code class="code">as.list</code> and <code class="code">as.call</code> functions. <a class="index-entry-id" id="index-quote"></a> <a class="index-entry-id" id="index-as_002elist"></a> <a class="index-entry-id" id="index-as_002ecall"></a> Components of the <a class="index-entry-id" id="index-parsing"></a> parse tree can be extracted using the standard indexing operations. </p> <ul class="mini-toc"> <li><a href="#Symbol-objects" accesskey="1">Symbol objects</a></li> </ul> <hr> <div class="subsubsection-level-extent" id="Symbol-objects"> <div class="nav-panel"> <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> [<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> <h4 class="subsubsection" id="Symbol-objects-1"><span>2.1.3.1 Symbol objects<a class="copiable-link" href="#Symbol-objects-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-symbol"></a> <p>Symbols refer to R <a class="index-entry-id" id="index-object-2"></a> objects. The <a class="index-entry-id" id="index-name-1"></a> name of any R object is usually a symbol. Symbols can be created through the functions <code class="code">as.name</code> and <code class="code">quote</code>. </p> <a class="index-entry-id" id="index-symbol-1"></a> <a class="index-entry-id" id="index-mode-2"></a> <p>Symbols have mode <code class="code">"name"</code>, storage mode <code class="code">"symbol"</code>, and type <code class="code">"symbol"</code>. They can be <a class="index-entry-id" id="index-coercion-1"></a> coerced to and from character strings using <code class="code">as.character</code> and <code class="code">as.name</code>. <a class="index-entry-id" id="index-as_002echaracter"></a> <a class="index-entry-id" id="index-as_002ename"></a> <a class="index-entry-id" id="index-parsing-1"></a> They naturally appear as atoms of parsed expressions, try e.g. <code class="code">as.list(quote(x + y))</code>. </p> <hr> </div> </div> <div class="subsection-level-extent" id="Expression-objects"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Expression-objects-1"><span>2.1.4 Expression objects<a class="copiable-link" href="#Expression-objects-1"> ¶</a></span></h4> <p>In R one can have objects of type <code class="code">"expression"</code>. An <em class="emph">expression</em> contains one or more statements. A statement is a syntactically correct collection of <a class="index-entry-id" id="index-token"></a> tokens. <a class="index-entry-id" id="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 class="code">"expression"</code> are only <a class="index-entry-id" id="index-evaluation_002c-expression"></a> evaluated when explicitly passed to <code class="code">eval</code>, whereas other language objects may get evaluated in some unexpected cases. </p> <p>An <a class="index-entry-id" id="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> </div> <div class="subsection-level-extent" id="Function-objects"> <div class="nav-panel"> <p> Next: <a href="#NULL-object" accesskey="n" rel="next">NULL</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> [<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> <h4 class="subsection" id="Function-objects-1"><span>2.1.5 Function objects<a class="copiable-link" href="#Function-objects-1"> ¶</a></span></h4> <a class="index-entry-id" id="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 class="index-entry-id" id="index-environment"></a> environment. The argument list is a comma-separated list of arguments. An <a class="index-entry-id" id="index-argument"></a> argument can be a symbol, or a ‘<samp class="samp"><var class="var">symbol</var> = <var class="var">default</var></samp>’ construct, or the special argument <code class="code">...</code>. 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 <code class="code">...</code> 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’s <a class="index-entry-id" id="index-function-1"></a> <a class="index-entry-id" id="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 class="emph">captured</em> and available to the function. This combination of the code of the function and the bindings in its environment is called a ‘function closure’, a term from functional programming theory. In this document we generally use the term ‘function’, but use ‘closure’ 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 class="code">formals</code>, <code class="code">body</code>, and <code class="code">environment</code> constructs (all three can also be used on the left hand side of <a class="index-entry-id" id="index-assignment"></a> assignments). <a class="index-entry-id" id="index-formals"></a> <a class="index-entry-id" id="index-body"></a> <a class="index-entry-id" id="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 class="emph">evaluation environment</em>) is created, whose enclosure (see <a class="ref" href="#Environment-objects">Environments</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 class="index-entry-id" id="index-function-2"></a> <p>There is also a facility for converting functions to and from list structures using <code class="code">as.list</code> and <code class="code">as.function</code>. <a class="index-entry-id" id="index-as_002efunction"></a> These have been included to provide compatibility with S and their use is discouraged. </p> <hr> </div> <div class="subsection-level-extent" id="NULL-object"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="NULL"><span>2.1.6 NULL<a class="copiable-link" href="#NULL"> ¶</a></span></h4> <p>There is a special object called <code class="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 class="index-entry-id" id="index-NULL"></a> </p> <p>The <code class="code">NULL</code> object has no type and no modifiable properties. There is only one <code class="code">NULL</code> object in R, to which all instances refer. To test for <code class="code">NULL</code> use <code class="code">is.null</code>. You cannot set attributes on <code class="code">NULL</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Builtin-objects-and-special-forms"> <div class="nav-panel"> <p> Next: <a href="#Promise-objects" accesskey="n" rel="next">Promise objects</a>, Previous: <a href="#NULL-object" accesskey="p" rel="prev">NULL</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> [<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> <h4 class="subsection" id="Builtin-objects-and-special-forms-1"><span>2.1.7 Builtin objects and special forms<a class="copiable-link" href="#Builtin-objects-and-special-forms-1"> ¶</a></span></h4> <p>These two kinds of object contain the builtin <a class="index-entry-id" id="index-function-3"></a> <a class="index-entry-id" id="index-_002ePrimitive"></a> <a class="index-entry-id" id="index-_002eInternal"></a> functions of R, i.e., those that are displayed as <code class="code">.Primitive</code> in code listings (as well as those accessed via the <code class="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 class="emph">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 class="code">is.primitive</code> function can distinguish them from interpreted <a class="index-entry-id" id="index-function-4"></a> functions. </p> <hr> </div> <div class="subsection-level-extent" id="Promise-objects"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Promise-objects-1"><span>2.1.8 Promise objects<a class="copiable-link" href="#Promise-objects-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-promise"></a> <p>Promise objects are part of R’s lazy evaluation mechanism. They contain three slots: a value, an expression, and an <a class="index-entry-id" id="index-environment-2"></a> environment. When a <a class="index-entry-id" id="index-function-5"></a> <a class="index-entry-id" id="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 class="emph">value</em> associated with the promise. When the argument is accessed, the stored expression is <a class="index-entry-id" id="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 class="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 class="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> </div> <div class="subsection-level-extent" id="Dot_002ddot_002ddot"> <div class="nav-panel"> <p> Next: <a href="#Environment-objects" accesskey="n" rel="next">Environments</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> [<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> <h4 class="subsection" id="Dot_002ddot_002ddot-1"><span>2.1.9 Dot-dot-dot<a class="copiable-link" href="#Dot_002ddot_002ddot-1"> ¶</a></span></h4> <p>The <code class="code">...</code> object type is stored as a type of pairlist. The components of <code class="code">...</code> can be accessed in the usual pairlist manner from C code, but <code class="code">...</code> is not easily accessed as an object in interpreted code, and even the existence of such an object should typically not be assumed, as that may change in the future. </p> <p>The object can be captured (with promises being forced!) as a list, so for example in <code class="code">table</code> one sees </p> <div class="example"> <pre class="example-preformatted"> args <- list(...) ## .... for (a in args) { ## .... </pre></div> <p>Note that the implementation of <code class="code">...</code> as a pairlist object is <em class="emph">not</em> to be considered part of the R API, and code outside base R should not rely on this current description of <code class="code">...</code>. On the other hand, the above <code class="code">list(...)</code> access, and the other “dot-access” functions <code class="code">...length()</code>, <code class="code">...elt()</code>, <code class="code">...names()</code>, and “reserved words” <code class="code">..1</code>, <code class="code">..2</code>, etc, see also help page <code class="code">?dots</code>, are part of the stable R API. </p> <a class="index-entry-id" id="index-function-6"></a> <a class="index-entry-id" id="index-function-argument-1"></a> <p>If a function has <code class="code">...</code> as a formal argument then any actual arguments that do not match a formal argument are matched with <code class="code">...</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Environment-objects"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Environments"><span>2.1.10 Environments<a class="copiable-link" href="#Environments"> ¶</a></span></h4> <a class="index-entry-id" id="index-environment-3"></a> <p>Environments can be thought of as consisting of two things. A <em class="emph">frame</em>, consisting of a set of symbol-value pairs, and an <em class="emph">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 class="index-entry-id" id="index-emptyenv"></a> environment, available through <code class="code">emptyenv()</code>, which has no parent. It is the direct parent of the environment of the base package <a class="index-entry-id" id="index-baseenv"></a> (available through the <code class="code">baseenv()</code> function). </p> <p>Environments are created implicitly by function calls, as described in <a class="ref" href="#Function-objects">Function objects</a> and <a class="ref" 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 class="code">new.env</code>. <a class="index-entry-id" id="index-new_002eenv"></a> The frame content of an environment can be accessed by use of <code class="code">ls</code>, <code class="code">names</code>, <code class="code">$</code>, <code class="code">[</code>, <code class="code">[[</code>, <code class="code">get</code>, and <code class="code">get0</code>, <a class="index-entry-id" id="index-get"></a> <a class="index-entry-id" id="index-get0"></a> and manipulated by <code class="code">$<-</code>, <code class="code">[[<-</code>, and <code class="code">assign</code> <a class="index-entry-id" id="index-assign"></a> as well as <code class="code">eval</code> and <code class="code">evalq</code>. </p> <p>The <code class="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> </div> <div class="subsection-level-extent" id="Pairlist-objects"> <div class="nav-panel"> <p> Next: <a href="#Any_002dtype" accesskey="n" rel="next">The “Any” type</a>, Previous: <a href="#Environment-objects" accesskey="p" rel="prev">Environments</a>, Up: <a href="#Basic-types" accesskey="u" rel="up">Basic types</a> [<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> <h4 class="subsection" id="Pairlist-objects-1"><span>2.1.11 Pairlist objects<a class="copiable-link" href="#Pairlist-objects-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-pairlist"></a> <p>Pairlist objects are similar to Lisp’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 class="code">formals</code>, and can be created by (e.g.) the <code class="code">pairlist</code> function. A zero-length pairlist is <code class="code">NULL</code>, as would be expected in Lisp but in contrast to a zero-length list. <a class="index-entry-id" id="index-pairlist-1"></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’s IBM computer). </p> <p>Pairlists are handled in the R language in exactly the same way as generic vectors (“lists”). In particular, elements are accessed using the same <code class="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 class="code">.Options</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Any_002dtype"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="The-_0060_0060Any_0027_0027-type"><span>2.1.12 The “Any” type<a class="copiable-link" href="#The-_0060_0060Any_0027_0027-type"> ¶</a></span></h4> <p>It is not really possible for an object to be of “Any” type, but it is nevertheless a valid type value. It gets used in certain (rather rare) circumstances, e.g. <code class="code">as.vector(x, "any")</code>, indicating that type <a class="index-entry-id" id="index-coercion-2"></a> coercion should not be done. </p> <hr> </div> </div> <div class="section-level-extent" id="Attributes"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Attributes-1"><span>2.2 Attributes<a class="copiable-link" href="#Attributes-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-attributes"></a> <a class="index-entry-id" id="index-object-3"></a> <p>All objects except <code class="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 class="code">attributes</code> and set by <code class="code">attributes<-</code>, <a class="index-entry-id" id="index-attributes-1"></a> <a class="index-entry-id" id="index-attributes_003c_002d"></a> individual components are accessed using <code class="code">attr</code> and <code class="code">attr<-</code>. <a class="index-entry-id" id="index-attr"></a> <a class="index-entry-id" id="index-attr_003c_002d"></a> </p> <p>Some attributes have special accessor <a class="index-entry-id" id="index-function_002c-accessor"></a> functions (e.g. <code class="code">levels<-</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 class="code">attr<-</code> and to <code class="code">attributes<-</code> that involve the special attributes and enforces the consistency checks. </p> <p>Matrices and arrays are simply vectors with the attribute <code class="code">dim</code> and optionally <code class="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">class</code> attribute then that attribute will be examined during <a class="index-entry-id" id="index-evaluation_002c-symbol"></a> evaluation. The class structure in R is described in detail in <a class="ref" href="#Object_002doriented-programming">Object-oriented programming</a>. </p> <ul class="mini-toc"> <li><a href="#Names" accesskey="1">Names</a></li> <li><a href="#Dimensions" accesskey="2">Dimensions</a></li> <li><a href="#Dimnames" accesskey="3">Dimnames</a></li> <li><a href="#Classes" accesskey="4">Classes</a></li> <li><a href="#Time-series-attributes" accesskey="5">Time series attributes</a></li> <li><a href="#Copying-of-attributes" accesskey="6">Copying of attributes</a></li> </ul> <hr> <div class="subsection-level-extent" id="Names"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Names-1"><span>2.2.1 Names<a class="copiable-link" href="#Names-1"> ¶</a></span></h4> <p>A <code class="code">names</code> attribute, when present, labels the individual elements of a vector or list. When an object is printed the <code class="code">names</code> attribute, when present, is used to label the elements. The <code class="code">names</code> attribute can also be used for indexing purposes, for example, <code class="code">quantile(x)["25%"]</code>. </p> <p>One may get and set the names using <code class="code">names</code> and <code class="code">names<-</code> constructions. <a class="index-entry-id" id="index-names"></a> <a class="index-entry-id" id="index-names_003c_002d"></a> <a class="index-entry-id" id="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 class="code">names</code> attribute is used; the <code class="code">names</code> attribute is really constructed from the tags of the list components. For one-dimensional arrays the <code class="code">names</code> attribute really accesses <code class="code">dimnames[[1]]</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Dimensions"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Dimensions-1"><span>2.2.2 Dimensions<a class="copiable-link" href="#Dimensions-1"> ¶</a></span></h4> <p>The <code class="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 class="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 class="index-entry-id" id="index-vector-1"></a> <p>A vector is not the same as a one-dimensional array since the latter has a <code class="code">dim</code> attribute of length one, whereas the former has no <code class="code">dim</code> attribute. </p> <hr> </div> <div class="subsection-level-extent" id="Dimnames"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Dimnames-1"><span>2.2.3 Dimnames<a class="copiable-link" href="#Dimnames-1"> ¶</a></span></h4> <p>Arrays may name each dimension separately using the <code class="code">dimnames</code> attribute which is a list of character vectors. The <code class="code">dimnames</code> list may itself have names which are then used for extent headings when printing arrays. </p> <hr> </div> <div class="subsection-level-extent" id="Classes"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Classes-1"><span>2.2.4 Classes<a class="copiable-link" href="#Classes-1"> ¶</a></span></h4> <p>R has an elaborate class system<a class="footnote" id="DOCF2" href="#FOOT2"><sup>2</sup></a>, principally controlled via the <code class="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 “generic methods” 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">class</code> attribute should be done with caution, and when they are available specific creation and <a class="index-entry-id" id="index-coercion-3"></a> coercion functions should be preferred. </p> <hr> </div> <div class="subsection-level-extent" id="Time-series-attributes"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Time-series-attributes-1"><span>2.2.5 Time series attributes<a class="copiable-link" href="#Time-series-attributes-1"> ¶</a></span></h4> <p>The <code class="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> </div> <div class="subsection-level-extent" id="Copying-of-attributes"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Copying-of-attributes-1"><span>2.2.6 Copying of attributes<a class="copiable-link" href="#Copying-of-attributes-1"> ¶</a></span></h4> <p>Whether attributes should be copied when an object is altered is a complex area, but there are some general rules (Becker, Chambers & Wilks, 1988, pp. 144–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 ‘most’ means all except the <code class="code">names</code>, <code class="code">dim</code> and <code class="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 class="code">names</code>, <code class="code">dim</code> and <code class="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> </div> </div> <div class="section-level-extent" id="Special-compound-objects"> <div class="nav-panel"> <p> Previous: <a href="#Attributes" accesskey="p" rel="prev">Attributes</a>, Up: <a href="#Objects" accesskey="u" rel="up">Objects</a> [<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> <h3 class="section" id="Special-compound-objects-1"><span>2.3 Special compound objects<a class="copiable-link" href="#Special-compound-objects-1"> ¶</a></span></h3> <ul class="mini-toc"> <li><a href="#Factors" accesskey="1">Factors</a></li> <li><a href="#Data-frame-objects" accesskey="2">Data frame objects</a></li> </ul> <hr> <div class="subsection-level-extent" id="Factors"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Factors-1"><span>2.3.1 Factors<a class="copiable-link" href="#Factors-1"> ¶</a></span></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 class="code">levels</code> attribute and class <code class="code">"factor"</code>. Optionally, it may also contain a <code class="code">contrasts</code> attribute which controls the parametrisation used when the factor is used in a <a class="index-entry-id" id="index-function_002c-modeling"></a> <a class="index-entry-id" id="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">class</code> vector <code class="code">c("ordered"," factor")</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> </div> <div class="subsection-level-extent" id="Data-frame-objects"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Data-frame-objects-1"><span>2.3.2 Data frame objects<a class="copiable-link" href="#Data-frame-objects-1"> ¶</a></span></h4> <p>Data frames are the R structures which most closely mimic the SAS or SPSS data set, i.e. a “cases by variables” 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 class="code">names</code> attribute labeling the variables and a <code class="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> </div> </div> </div> <div class="chapter-level-extent" id="Evaluation-of-expressions"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Evaluation-of-expressions-1"><span>3 Evaluation of expressions<a class="copiable-link" href="#Evaluation-of-expressions-1"> ¶</a></span></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 class="index-entry-id" id="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> <ul class="mini-toc"> <li><a href="#Simple-evaluation" accesskey="1">Simple evaluation</a></li> <li><a href="#Control-structures" accesskey="2">Control structures</a></li> <li><a href="#Elementary-arithmetic-operations" accesskey="3">Elementary arithmetic operations</a></li> <li><a href="#Indexing" accesskey="4">Indexing</a></li> <li><a href="#Scope-of-variables" accesskey="5">Scope of variables</a></li> </ul> <hr> <div class="section-level-extent" id="Simple-evaluation"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Simple-evaluation-1"><span>3.1 Simple evaluation<a class="copiable-link" href="#Simple-evaluation-1"> ¶</a></span></h3> <ul class="mini-toc"> <li><a href="#Constants" accesskey="1">Constants</a></li> <li><a href="#Symbol-lookup" accesskey="2">Symbol lookup</a></li> <li><a href="#Function-calls" accesskey="3">Function calls</a></li> <li><a href="#Operators" accesskey="4">Operators</a></li> </ul> <hr> <div class="subsection-level-extent" id="Constants"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Constants-1"><span>3.1.1 Constants<a class="copiable-link" href="#Constants-1"> ¶</a></span></h4> <p>Any number typed directly at the prompt is a constant and is evaluated. </p> <div class="example"> <pre class="example-preformatted">> 1 [1] 1 </pre></div> <p>Perhaps unexpectedly, the number returned from the expression <code class="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 class="code">as.integer</code> or using various other techniques. But perhaps the simplest approach is to qualify our constant with the suffix character ‘L’. For example, to create the integer value 1, we might use </p> <div class="example"> <pre class="example-preformatted">> 1L [1] </pre></div> <p>We can use the ‘L’ suffix to qualify any number with the intent of making it an explicit integer. So ‘0x10L’ creates the integer value 16 from the hexadecimal representation. The constant <code class="code">1e3L</code> gives 1000 as an integer rather than a numeric value and is equivalent to <code class="code">1000L</code>. (Note that the ‘L’ is treated as qualifying the term <code class="code">1e3</code> and not the <code class="code">3</code>.) If we qualify a value with ‘L’ that is not an integer value, e.g. <code class="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 class="code">1.L</code>. </p> <p>We get a syntax error when using ‘L’ with complex numbers, e.g. <code class="code">12iL</code> gives an error. </p> <p>Constants are fairly boring and to do more we need symbols. </p> <hr> </div> <div class="subsection-level-extent" id="Symbol-lookup"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Symbol-lookup-1"><span>3.1.2 Symbol lookup<a class="copiable-link" href="#Symbol-lookup-1"> ¶</a></span></h4> <p>When a new variable is created it must have a <a class="index-entry-id" id="index-name-2"></a> name so it can be referenced and it usually has a value. The name itself is a <a class="index-entry-id" id="index-symbol-2"></a> symbol. When a symbol is <a class="index-entry-id" id="index-evaluation_002c-symbol-1"></a> evaluated its <a class="index-entry-id" id="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 class="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 “programming on the language” (<a class="ref" href="#Computing-on-the-language">Computing on the language</a>). </p> <div class="example"> <pre class="example-preformatted">> y <- 4 > y [1] 4 </pre></div> <hr> </div> <div class="subsection-level-extent" id="Function-calls"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Function-calls-1"><span>3.1.3 Function calls<a class="copiable-link" href="#Function-calls-1"> ¶</a></span></h4> <p>Most of the computations carried out in R involve the evaluation of functions. We will also refer to this as <a class="index-entry-id" id="index-function-invocation"></a> function <em class="emph">invocation</em>. Functions are invoked by name with a list of arguments separated by commas. </p> <div class="example"> <pre class="example-preformatted">> mean(1:10) [1] 5.5 </pre></div> <p>In this example the function <code class="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 class="index-entry-id" id="index-function-7"></a> <a class="index-entry-id" id="index-function-arguments"></a> <p>Function calls can have <em class="emph">tagged</em> (or <em class="emph">named</em>) arguments, as in <code class="code">plot(x, y, pch = 3)</code>. Arguments without tags are known as <em class="emph">positional</em> since the function must distinguish their meaning from their sequential positions among the arguments of the call, e.g., that <code class="code">x</code> denotes the abscissa variable and <code class="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 class="index-entry-id" id="index-function_002c-assignment"></a> <p>A special type of function calls can appear on the left hand side of the <a class="index-entry-id" id="index-assignment-1"></a> assignment operator as in </p> <div class="example"> <pre class="example-preformatted">> class(x) <- "foo" </pre></div> <p>What this construction really does is to call the function <code class="code">class<-</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> </div> <div class="subsection-level-extent" id="Operators"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Operators-1"><span>3.1.4 Operators<a class="copiable-link" href="#Operators-1"> ¶</a></span></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-preformatted">> 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-preformatted">> y <- 2 * (a + log(x)) </pre></div> <p>R contains a number of operators. They are listed in the table below. </p> <blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="10%"><code class="code">-</code></td><td width="70%">Minus, can be unary or binary</td></tr> <tr><td width="10%"><code class="code">+</code></td><td width="70%">Plus, can be unary or binary</td></tr> <tr><td width="10%"><code class="code">!</code></td><td width="70%">Unary not</td></tr> <tr><td width="10%"><code class="code">~</code></td><td width="70%">Tilde, used for model formulae, can be either unary or binary</td></tr> <tr><td width="10%"><code class="code">?</code></td><td width="70%">Help</td></tr> <tr><td width="10%"><code class="code">:</code></td><td width="70%">Sequence, binary (in model formulae: interaction)</td></tr> <tr><td width="10%"><code class="code">*</code></td><td width="70%">Multiplication, binary</td></tr> <tr><td width="10%"><code class="code">/</code></td><td width="70%">Division, binary</td></tr> <tr><td width="10%"><code class="code">^</code></td><td width="70%">Exponentiation, binary</td></tr> <tr><td width="10%"><code class="code">%<var class="var">x</var>%</code></td><td width="70%">Special binary operators, <var class="var">x</var> can be replaced by any valid name</td></tr> <tr><td width="10%"><code class="code">%%</code></td><td width="70%">Modulus, binary</td></tr> <tr><td width="10%"><code class="code">%/%</code></td><td width="70%">Integer divide, binary</td></tr> <tr><td width="10%"><code class="code">%*%</code></td><td width="70%">Matrix product, binary</td></tr> <tr><td width="10%"><code class="code">%o%</code></td><td width="70%">Outer product, binary</td></tr> <tr><td width="10%"><code class="code">%x%</code></td><td width="70%">Kronecker product, binary</td></tr> <tr><td width="10%"><code class="code">%in%</code></td><td width="70%">Matching operator, binary (in model formulae: nesting)</td></tr> <tr><td width="10%"><code class="code">%||%</code></td><td width="70%">Null coalescing operator, binary</td></tr> <tr><td width="10%"><code class="code"><</code></td><td width="70%">Less than, binary</td></tr> <tr><td width="10%"><code class="code">></code></td><td width="70%">Greater than, binary</td></tr> <tr><td width="10%"><code class="code">==</code></td><td width="70%">Equal to, binary</td></tr> <tr><td width="10%"><code class="code">>=</code></td><td width="70%">Greater than or equal to, binary</td></tr> <tr><td width="10%"><code class="code"><=</code></td><td width="70%">Less than or equal to, binary</td></tr> <tr><td width="10%"><code class="code">&</code></td><td width="70%">And, binary, vectorized</td></tr> <tr><td width="10%"><code class="code">&&</code></td><td width="70%">And, binary, not vectorized</td></tr> <tr><td width="10%"><code class="code">|</code></td><td width="70%">Or, binary, vectorized</td></tr> <tr><td width="10%"><code class="code">||</code></td><td width="70%">Or, binary, not vectorized</td></tr> <tr><td width="10%"><code class="code"><-</code></td><td width="70%">Left assignment, binary</td></tr> <tr><td width="10%"><code class="code">-></code></td><td width="70%">Right assignment, binary</td></tr> <tr><td width="10%"><code class="code">$</code></td><td width="70%">List subset, binary</td></tr> </tbody> </table> </blockquote> <p>Except for the syntax, there is no difference between applying an operator and calling a function. In fact, <code class="code">x + y</code> can equivalently be written <code class="code">`+`(x, y)</code>. Notice that since ‘<samp class="samp">+</samp>’ is a non-standard function name, it needs to be quoted. </p> <a class="index-entry-id" id="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 class="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 class="code">-</code>, <code class="code">*</code>, and <code class="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 class="code">%*%</code> operator exists for that purpose). Some finer points relating to vectorized operations will be discussed in <a class="ref" href="#Elementary-arithmetic-operations">Elementary arithmetic operations</a>. </p> <p>To access individual elements of an atomic vector, one generally uses the <code class="code">x[i]</code> construction. </p> <div class="example"> <pre class="example-preformatted">> x <- rnorm(5) > x [1] -0.12526937 -0.27961154 -1.03718717 -0.08156527 1.37167090 > x[2] [1] -0.2796115 </pre></div> <p>List components are more commonly accessed using <code class="code">x$a</code> or <code class="code">x[[i]]</code>. </p> <div class="example"> <pre class="example-preformatted">> x <- options() > x$prompt [1] "> " </pre></div> <p>Indexing constructs can also appear on the right hand side of an <a class="index-entry-id" id="index-assignment-2"></a> assignment. </p> <p>Like the other operators, indexing is really done by functions, and one could have used <code class="code">`[`(x, 2)</code> instead of <code class="code">x[2]</code>. </p> <p>R’s indexing operations contain many advanced features which are further described in <a class="ref" href="#Indexing">Indexing</a>. </p> <hr> </div> </div> <div class="section-level-extent" id="Control-structures"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Control-structures-1"><span>3.2 Control structures<a class="copiable-link" href="#Control-structures-1"> ¶</a></span></h3> <p>Computation in R consists of sequentially evaluating <em class="emph">statements</em>. Statements, such as <code class="code">x<-1:10</code> or <code class="code">mean(y)</code>, can be separated by either a semi-colon or a new line. Whenever the <a class="index-entry-id" id="index-evaluation_002c-statement"></a> evaluator is presented with a syntactically complete statement that statement is evaluated and the <em class="emph">value</em> returned. The result of evaluating a statement can be referred to as the value of the statement<a class="footnote" id="DOCF3" href="#FOOT3"><sup>3</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 class="emph">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 ‘<samp class="samp">></samp>’ to ‘<samp class="samp">+</samp>’. </p> <div class="example"> <pre class="example-preformatted">> x <- 0; x + 5 [1] 5 > y <- 1:10 > 1; 2 [1] 1 [1] 2 </pre></div> <p>Statements can be grouped together using braces ‘<samp class="samp">{</samp>’ and ‘<samp class="samp">}</samp>’. A group of statements is sometimes called a <em class="emph">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 class="emph">statement</em> refers to either a single statement or a block. </p> <div class="example"> <pre class="example-preformatted">> { x <- 0 + x + 5 + } [1] 5 </pre></div> <ul class="mini-toc"> <li><a href="#if" accesskey="1">if</a></li> <li><a href="#Looping" accesskey="2">Looping</a></li> <li><a href="#repeat" accesskey="3">repeat</a></li> <li><a href="#while" accesskey="4">while</a></li> <li><a href="#for" accesskey="5">for</a></li> <li><a href="#switch" accesskey="6">switch</a></li> </ul> <hr> <div class="subsection-level-extent" id="if"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="if-1"><span>3.2.1 if<a class="copiable-link" href="#if-1"> ¶</a></span></h4> <p>The <code class="code">if</code>/<code class="code">else</code> statement conditionally evaluates two statements. There is a <em class="emph">condition</em> which is evaluated and if the <em class="emph">value</em> is <code class="code">TRUE</code> then the first statement is evaluated; otherwise the second statement will be evaluated. The <code class="code">if</code>/<code class="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-preformatted">if ( <var class="var">statement1</var> ) <var class="var">statement2</var> else <var class="var">statement3</var> </pre></div> <p>First, <var class="var">statement1</var> is evaluated to yield <var class="var">value1</var>. If <var class="var">value1</var> is a logical vector with first element <code class="code">TRUE</code> then <var class="var">statement2</var> is evaluated. If the first element of <var class="var">value1</var> is <code class="code">FALSE</code> then <var class="var">statement3</var> is evaluated. If <var class="var">value1</var> is a numeric vector then <var class="var">statement3</var> is evaluated when the first element of <var class="var">value1</var> is zero and otherwise <var class="var">statement2</var> is evaluated. Only the first element of <var class="var">value1</var> is used. All other elements are ignored. If <var class="var">value1</var> has any type other than a logical or a numeric vector an error is signalled. </p> <p><code class="code">if</code>/<code class="code">else</code> statements can be used to avoid numeric problems such as taking the logarithm of a negative number. Because <code class="code">if</code>/<code class="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-preformatted">> if( any(x <= 0) ) y <- log(1+x) else y <- log(x) > y <- if( any(x <= 0) ) log(1+x) else log(x) </pre></div> <p>The <code class="code">else</code> clause is optional. The statement <code class="code">if(any(x <= 0)) x <- x[x <= 0]</code> is valid. When the <code class="code">if</code> statement is not in a block the <code class="code">else</code>, if present, must appear on the same line as the end of <var class="var">statement2</var>. Otherwise the new line at the end of <var class="var">statement2</var> completes the <code class="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 class="code">else</code> on the same line as the closing brace that marks the end of the statement. </p> <p><code class="code">if</code>/<code class="code">else</code> statements can be nested. </p> <div class="example"> <pre class="example-preformatted">if ( <var class="var">statement1</var> ) { <var class="var">statement2</var> } else if ( <var class="var">statement3</var> ) { <var class="var">statement4</var> } else if ( <var class="var">statement5</var> ) { <var class="var">statement6</var> } else <var class="var">statement8</var> </pre></div> <p>One of the even numbered statements will be evaluated and the resulting value returned. If the optional <code class="code">else</code> clause is omitted and all the odd numbered <var class="var">statement</var>s evaluate to <code class="code">FALSE</code> no statement will be evaluated and <code class="code">NULL</code> is returned. </p> <p>The odd numbered <var class="var">statement</var>s are evaluated, in order, until one evaluates to <code class="code">TRUE</code> and then the associated even numbered <var class="var">statement</var> is evaluated. In this example, <var class="var">statement6</var> will only be evaluated if <var class="var">statement1</var> is <code class="code">FALSE</code> and <var class="var">statement3</var> is <code class="code">FALSE</code> and <var class="var">statement5</var> is <code class="code">TRUE</code>. There is no limit to the number of <code class="code">else if</code> clauses that are permitted. </p> <hr> </div> <div class="subsection-level-extent" id="Looping"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Looping-1"><span>3.2.2 Looping<a class="copiable-link" href="#Looping-1"> ¶</a></span></h4> <p>R has three statements that provide explicit looping.<a class="footnote" id="DOCF4" href="#FOOT4"><sup>4</sup></a> They are <code class="code">for</code>, <code class="code">while</code> and <code class="code">repeat</code>. The two built-in constructs, <code class="code">next</code> and <code class="code">break</code>, provide additional control over the evaluation. R provides other functions for implicit looping such as <code class="code">tapply</code>, <code class="code">apply</code>, and <code class="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 class="code">break</code> and <code class="code">next</code>. <a class="index-entry-id" id="index-break"></a> <a class="index-entry-id" id="index-next"></a> The <code class="code">break</code> statement causes an exit from the innermost loop that is currently being executed. The <code class="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 class="code">next</code> in the current loop is evaluated. </p> <p>The value returned by a loop statement is always <code class="code">NULL</code> and is returned invisibly. </p> <hr> </div> <div class="subsection-level-extent" id="repeat"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="repeat-1"><span>3.2.3 repeat<a class="copiable-link" href="#repeat-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-repeat"></a> <p>The <code class="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 class="code">repeat</code> because of the danger of an infinite loop. The syntax of the <code class="code">repeat</code> loop is </p> <div class="example"> <pre class="example-preformatted">repeat <var class="var">statement</var> </pre></div> <p>When using <code class="code">repeat</code>, <var class="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> </div> <div class="subsection-level-extent" id="while"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="while-1"><span>3.2.4 while<a class="copiable-link" href="#while-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-while"></a> <p>The <code class="code">while</code> statement is very similar to the <code class="code">repeat</code> statement. The syntax of the <code class="code">while</code> loop is </p> <div class="example"> <pre class="example-preformatted">while ( <var class="var">statement1</var> ) <var class="var">statement2</var> </pre></div> <p>where <var class="var">statement1</var> is evaluated and if its value is <code class="code">TRUE</code> then <var class="var">statement2</var> is evaluated. This process continues until <var class="var">statement1</var> evaluates to <code class="code">FALSE</code>. </p> <hr> </div> <div class="subsection-level-extent" id="for"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="for-1"><span>3.2.5 for<a class="copiable-link" href="#for-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-for"></a> <p>The syntax of the <code class="code">for</code> loop is </p> <div class="example"> <pre class="example-preformatted">for ( <var class="var">name</var> in <var class="var">vector</var> ) <var class="var">statement1</var> </pre></div> <p>where <var class="var">vector</var> can be either a vector or a list. For each element in <var class="var">vector</var> the variable <var class="var">name</var> is set to the value of that element and <var class="var">statement1</var> is evaluated. A side effect is that the variable <var class="var">name</var> still exists after the loop has concluded and it has the value of the last element of <var class="var">vector</var> that the loop was evaluated for. </p> <hr> </div> <div class="subsection-level-extent" id="switch"> <div class="nav-panel"> <p> Previous: <a href="#for" accesskey="p" rel="prev">for</a>, Up: <a href="#Control-structures" accesskey="u" rel="up">Control structures</a> [<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> <h4 class="subsection" id="switch-1"><span>3.2.6 switch<a class="copiable-link" href="#switch-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-switch"></a> <p>Technically speaking, <code class="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-preformatted">switch (<var class="var">statement</var>, <var class="var">list</var>) </pre></div> <p>where the elements of <var class="var">list</var> may be named. First, <var class="var">statement</var> is evaluated and the result, <var class="var">value</var>, obtained. If <var class="var">value</var> is a number between 1 and the length of <var class="var">list</var> then the corresponding element of <var class="var">list</var> is evaluated and the result returned. If <var class="var">value</var> is too large or too small <code class="code">NULL</code> is returned. </p> <div class="example"> <pre class="example-preformatted">> x <- 3 > switch(x, 2+2, mean(1:10), rnorm(5)) [1] 2.2903605 2.3271663 -0.7060073 1.3622045 -0.2892720 > switch(2, 2+2, mean(1:10), rnorm(5)) [1] 5.5 > switch(6, 2+2, mean(1:10), rnorm(5)) NULL </pre></div> <p>If <var class="var">value</var> is a character vector then the element of <code class="code">...</code> with a name that exactly matches <var class="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 class="code">NULL</code> is returned. </p> <div class="example"> <pre class="example-preformatted">> y <- "fruit" > switch(y, fruit = "banana", vegetable = "broccoli", "Neither") [1] "banana" > y <- "meat" > switch(y, fruit = "banana", vegetable = "broccoli", "Neither") [1] "Neither" </pre></div> <p>A common use of <code class="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-preformatted">> centre <- function(x, type) { + switch(type, + mean = mean(x), + median = median(x), + trimmed = mean(x, trim = .1)) + } > x <- rcauchy(10) > centre(x, "mean") [1] 0.8760325 > centre(x, "median") [1] 0.5360891 > centre(x, "trimmed") [1] 0.6086504 </pre></div> <p><code class="code">switch</code> returns either the value of the statement that was evaluated or <code class="code">NULL</code> if no statement was evaluated. </p> <p>To choose from a list of alternatives that already exists <code class="code">switch</code> may not be the best way to select one for evaluation. It is often better to use <code class="code">eval</code> and the subset operator, <code class="code">[[</code>, directly via <code class="code">eval(x[[condition]])</code>. </p> <hr> </div> </div> <div class="section-level-extent" id="Elementary-arithmetic-operations"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Elementary-arithmetic-operations-1"><span>3.3 Elementary arithmetic operations<a class="copiable-link" href="#Elementary-arithmetic-operations-1"> ¶</a></span></h3> <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> <ul class="mini-toc"> <li><a href="#Recycling-rules" accesskey="1">Recycling rules</a></li> <li><a href="#Propagation-of-names" accesskey="2">Propagation of names</a></li> <li><a href="#Dimensional-attributes" accesskey="3">Dimensional attributes</a></li> <li><a href="#NA-handling" accesskey="4">NA handling</a></li> </ul> <hr> <div class="subsection-level-extent" id="Recycling-rules"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Recycling-rules-1"><span>3.3.1 Recycling rules<a class="copiable-link" href="#Recycling-rules-1"> ¶</a></span></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 class="code">c(1, 2, 3)</code> to a six-element vector then you will really add <code class="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> </div> <div class="subsection-level-extent" id="Propagation-of-names"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Propagation-of-names-1"><span>3.3.2 Propagation of names<a class="copiable-link" href="#Propagation-of-names-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-name-3"></a> <p>propagation of names (first one wins, I think - also if it has no names?? —- first one *with names* wins, recycling causes shortest to lose names) </p> <hr> </div> <div class="subsection-level-extent" id="Dimensional-attributes"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Dimensional-attributes-1"><span>3.3.3 Dimensional attributes<a class="copiable-link" href="#Dimensional-attributes-1"> ¶</a></span></h4> <p>(matrix+matrix, dimensions must match. vector+matrix: first recycle, then check if dims fit, error if not) </p> <hr> </div> <div class="subsection-level-extent" id="NA-handling"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="NA-handling-1"><span>3.3.4 NA handling<a class="copiable-link" href="#NA-handling-1"> ¶</a></span></h4> <p>Missing values in the statistical sense, that is, variables whose value is not known, have the value <code class="code">NA</code>. This should not be confused with the <code class="code">missing</code> property for a function argument that has not been supplied (see <a class="ref" href="#Arguments">Arguments</a>). <a class="index-entry-id" id="index-missing"></a> <a class="index-entry-id" id="index-NA"></a> <a class="index-entry-id" id="index-NaN"></a> </p> <a class="index-entry-id" id="index-type-5"></a> <p>As the elements of an atomic vector must be of the same type there are multiple types of <code class="code">NA</code> values. There is one case where this is particularly important to the user. The default type of <code class="code">NA</code> is <code class="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 class="ref" href="#Indexing">Indexing</a> for details). </p> <p>Numeric and logical calculations with <code class="code">NA</code> generally return <code class="code">NA</code>. In cases where the result of the operation would be the same for all possible values the <code class="code">NA</code> could take, the operation may return this value. In particular, ‘<samp class="samp">FALSE & NA</samp>’ is <code class="code">FALSE</code>, ‘<samp class="samp">TRUE | NA</samp>’ is <code class="code">TRUE</code>. <code class="code">NA</code> is not equal to any other value or to itself; testing for <code class="code">NA</code> is done using <code class="code">is.na</code>. <a class="index-entry-id" id="index-is_002ena"></a> However, an <code class="code">NA</code> value will match another <code class="code">NA</code> value in <code class="code">match</code>. </p> <p>Numeric calculations whose result is undefined, such as ‘<samp class="samp">0/0</samp>’, produce the value <code class="code">NaN</code>. This exists only in the <code class="code">double</code> type and for real or imaginary components of the complex type. The function <code class="code">is.nan</code> is provided to check specifically for <a class="index-entry-id" id="index-is_002enan"></a> <code class="code">NaN</code>, <code class="code">is.na</code> also returns <code class="code">TRUE</code> for <code class="code">NaN</code>. <a class="index-entry-id" id="index-coercion-4"></a> Coercing <code class="code">NaN</code> to logical or integer type gives an <code class="code">NA</code> of the appropriate type, but coercion to character gives the string <code class="code">"NaN"</code>. <code class="code">NaN</code> values are incomparable so tests of equality or collation involving <code class="code">NaN</code> will result in <code class="code">NA</code>. They are regarded as matching any <code class="code">NaN</code> value (and no other value, not even <code class="code">NA</code>) by <code class="code">match</code>. </p> <p>The <code class="code">NA</code> of character type is distinct from the string <code class="code">"NA"</code>. Programmers who need to specify an explicit string <code class="code">NA</code> should use ‘<samp class="samp">NA_character_</samp>’ rather than <code class="code">"NA"</code>, or set elements to <code class="code">NA</code> using <code class="code">is.na<-</code>. </p> <p>The constants <code class="code">NA_integer_</code>, <code class="code">NA_real_</code>, <code class="code">NA_complex_</code> and <code class="code">NA_character_</code> will generate (in the parser) an <code class="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 class="code">NA</code> (and the <code class="code">control</code> options ask for this to be done). </p> <p>There is no <code class="code">NA</code> value for raw vectors. </p> <hr> </div> </div> <div class="section-level-extent" id="Indexing"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Indexing-1"><span>3.4 Indexing<a class="copiable-link" href="#Indexing-1"> ¶</a></span></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 class="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-preformatted">x[i] x[i, j] x[[i]] x[[i, j]] x$a x$"a" </pre></div> <a class="index-entry-id" id="index-_005b"></a> <a class="index-entry-id" id="index-_005b_005b"></a> <a class="index-entry-id" id="index-_0024"></a> <a class="index-entry-id" id="index-index-2"></a> <p>For vectors and matrices the <code class="code">[[</code> forms are rarely used, although they have some slight semantic differences from the <code class="code">[</code> form (e.g. it drops any <code class="code">names</code> or <code class="code">dimnames</code> attribute, and that partial matching is used for character indices). When indexing multi-dimensional structures with a single index, <code class="code">x[[i]]</code> or <code class="code">x[i]</code> will return the <code class="code">i</code>-th sequential element of <code class="code">x</code>. </p> <p>For lists, one generally uses <code class="code">[[</code> to select any single element, whereas <code class="code">[</code> returns a list of the selected elements. </p> <p>The <code class="code">[[</code> form allows only a single element to be selected using integer or character indices, whereas <code class="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 class="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 class="code">x[[expr]]</code>. Applying <code class="code">$</code> to a non-recursive object is an error. </p> <ul class="mini-toc"> <li><a href="#Indexing-by-vectors" accesskey="1">Indexing by vectors</a></li> <li><a href="#Indexing-matrices-and-arrays" accesskey="2">Indexing matrices and arrays</a></li> <li><a href="#Indexing-other-structures" accesskey="3">Indexing other structures</a></li> <li><a href="#Subset-assignment" accesskey="4">Subset assignment</a></li> </ul> <hr> <div class="subsection-level-extent" id="Indexing-by-vectors"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Indexing-by-vectors-1"><span>3.4.1 Indexing by vectors<a class="copiable-link" href="#Indexing-by-vectors-1"> ¶</a></span></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 class="code">x[i]</code>. Then the following possibilities exist according to the type of <code class="code">i</code>. </p> <ul class="itemize mark-bullet"> <li><a class="index-entry-id" id="index-index-3"></a> <strong class="strong">Integer</strong>. All elements of <code class="code">i</code> must have the same sign. If they are positive, the elements of <code class="code">x</code> with those index numbers are selected. If <code class="code">i</code> contains negative elements, all elements except those indicated are selected. <p>If <code class="code">i</code> is positive and exceeds <code class="code">length(x)</code> then the corresponding selection is <code class="code">NA</code>. Negative out of bounds values for <code class="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 (“no-op”). </p> <p>A special case is the zero index, which has null effects: <code class="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 class="strong">Other numeric</strong>. Non-integer values are converted to integer (by truncation towards zero) before use. </li><li><strong class="strong">Logical</strong>. The indexing <code class="code">i</code> should generally have the same length as <code class="code">x</code>. If it is shorter, then its elements will be recycled as discussed in <a class="ref" href="#Elementary-arithmetic-operations">Elementary arithmetic operations</a>. If it is longer, then <code class="code">x</code> is conceptually extended with <code class="code">NA</code>s. The selected values of <code class="code">x</code> are those for which <code class="code">i</code> is <code class="code">TRUE</code>. </li><li><a class="index-entry-id" id="index-partial-matching"></a> <strong class="strong">Character</strong>. The strings in <code class="code">i</code> are matched against the names attribute of <code class="code">x</code> and the resulting integers are used. For <code class="code">[[</code> and <code class="code">$</code> partial matching is used if exact matching fails, so <code class="code">x$aa</code> will match <code class="code">x$aabb</code> if <code class="code">x</code> does not contain a component named <code class="code">"aa"</code> and <code class="code">"aabb"</code> is the only name which has prefix <code class="code">"aa"</code>. For <code class="code">[[</code>, partial matching can be controlled via the <code class="code">exact</code> argument which defaults to <code class="code">NA</code> indicating that partial matching is allowed, but should result in a warning when it occurs. Setting <code class="code">exact</code> to <code class="code">TRUE</code> prevents partial matching from occurring, a <code class="code">FALSE</code> value allows it and does not issue any warnings. Note that <code class="code">[</code> always requires an exact match. The string <code class="code">""</code> is treated specially: it indicates ‘no name’ 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 class="strong">Factor</strong>. The result is identical to <code class="code">x[as.integer(i)]</code>. The factor levels are never used. If so desired, use <code class="code">x[as.character(i)]</code> or a similar construction. </li><li><strong class="strong">Empty</strong>. The expression <code class="code">x[]</code> returns <code class="code">x</code>, but drops “irrelevant” attributes from the result. Only <code class="code">names</code> and in multi-dimensional arrays <code class="code">dim</code> and <code class="code">dimnames</code> attributes are retained. </li><li><strong class="strong">NULL</strong>. This is treated as if it were <code class="code">integer(0)</code>. </li></ul> <p>Indexing with a missing (i.e. <code class="code">NA</code>) value gives an <code class="code">NA</code> result. This rule applies also to the case of logical indexing, i.e. the elements of <code class="code">x</code> that have an <code class="code">NA</code> selector in <code class="code">i</code> get included in the result, but their value will be <code class="code">NA</code>. <a class="index-entry-id" id="index-NA-1"></a> </p> <p>Notice however, that there are different modes of <code class="code">NA</code>—the literal constant is of mode <code class="code">"logical"</code>, but it is frequently automatically coerced to other types. One effect of this is that <code class="code">x[NA]</code> has the length of <code class="code">x</code>, but <code class="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 class="code">[</code> will also carry out the relevant subsetting of any names attributes. </p> <hr> </div> <div class="subsection-level-extent" id="Indexing-matrices-and-arrays"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Indexing-matrices-and-arrays-1"><span>3.4.2 Indexing matrices and arrays<a class="copiable-link" href="#Indexing-matrices-and-arrays-1"> ¶</a></span></h4> <a class="index-entry-id" id="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 class="code">dimnames</code> taking the place of <code class="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 class="code">dim</code> and <code class="code">dimnames</code> attributes are disregarded and the result is effectively that of <code class="code">c(m)[i]</code>. Notice that <code class="code">m[1]</code> is usually very different from <code class="code">m[1, ]</code> or <code class="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 class="code">m[1, 1]</code> and <code class="code">m[2, 2]</code> in one operation. </p> <div class="example"> <pre class="example-preformatted">> m <- matrix(1:4, 2) > m [,1] [,2] [1,] 1 3 [2,] 2 4 > i <- matrix(c(1, 1, 2, 2), 2, byrow = TRUE) > i [,1] [,2] [1,] 1 1 [2,] 2 2 > m[i] [1] 1 4 </pre></div> <p>Indexing matrices may not contain negative indices. <code class="code">NA</code> and zero values are allowed: rows in an index matrix containing a zero are ignored, whereas rows containing an <code class="code">NA</code> produce an <code class="code">NA</code> in the result. </p> <p>Both in the case of using a single <a class="index-entry-id" id="index-index-5"></a> index and in matrix indexing, a <code class="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 class="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 ‘<samp class="samp">drop = FALSE</samp>’ to the indexing operation. Notice that this is an additional argument to the <code class="code">[</code> function and doesn’t add to the index count. Hence the correct way of selecting the first row of a matrix as a <em class="math">1</em> by <em class="math">n</em> matrix is <code class="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 ‘<samp class="samp">drop = FALSE</samp>’ is used. </p> <p>Notice that vectors are distinct from one-dimensional arrays in that the latter have <code class="code">dim</code> and <code class="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 class="code">table</code>. This is sometimes useful because the elements of the <code class="code">dimnames</code> list may themselves be named, which is not the case for the <code class="code">names</code> attribute. </p> <p>Some operations such as <code class="code">m[FALSE, ]</code> result in structures in which a dimension has zero extent. R generally tries to handle these structures sensibly. </p> <hr> </div> <div class="subsection-level-extent" id="Indexing-other-structures"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Indexing-other-structures-1"><span>3.4.3 Indexing other structures<a class="copiable-link" href="#Indexing-other-structures-1"> ¶</a></span></h4> <p>The operator <code class="code">[</code> is a generic function which allows class methods to be added, and the <code class="code">$</code> and <code class="code">[[</code> operators likewise. Thus, it is possible to have user-defined indexing operations for any structure. Such a function, say <code class="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 class="code">$</code>, the index argument is of mode <code class="code">"symbol"</code> even when using the <code class="code">x$"abc"</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 class="code">[</code> is that used for data frames. It is not described in detail here (see the help page for <code class="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—in that case the <code class="code">drop</code> argument is ignored, with a warning. </p> <p>The basic operators <code class="code">$</code> and <code class="code">[[</code> can be applied to environments. Only character indices are allowed and no partial matching is done. </p> <hr> </div> <div class="subsection-level-extent" id="Subset-assignment"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Subset-assignment-1"><span>3.4.4 Subset assignment<a class="copiable-link" href="#Subset-assignment-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-assignment-3"></a> <a class="index-entry-id" id="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-preformatted">x[3:5] <- 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-preformatted">`*tmp*` <- x x <- "[<-"(`*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 class="code">for</code> loop had been used. Any existing variable called <code class="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 class="code">[</code>. The replacement function has the same name with <code class="code"><-</code> pasted on. Its last argument, which must be called <code class="code">value</code>, is the new value to be assigned. For example, </p><div class="example"> <pre class="example-preformatted">names(x) <- c("a","b") </pre></div> <p>is equivalent to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- x x <- "names<-"(`*tmp*`, value=c("a","b")) rm(`*tmp*`) </pre></div> <p>Nesting of complex assignments is evaluated recursively </p><div class="example"> <pre class="example-preformatted">names(x)[3] <- "Three" </pre></div> <p>is equivalent to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- x x <- "names<-"(`*tmp*`, value="[<-"(names(`*tmp*`), 3, value="Three")) rm(`*tmp*`) </pre></div> <p>Complex assignments in the enclosing environment (using <code class="code"><<-</code>) are also permitted: </p><div class="example"> <pre class="example-preformatted">names(x)[3] <<- "Three" </pre></div> <p>is equivalent to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <<- get(x, envir=parent.env(), inherits=TRUE) names(`*tmp*`)[3] <- "Three" x <<- `*tmp*` rm(`*tmp*`) </pre></div> <p>and also to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- get(x,envir=parent.env(), inherits=TRUE) x <<- "names<-"(`*tmp*`, value="[<-"(names(`*tmp*`), 3, value="Three")) rm(`*tmp*`) </pre></div> <p>Only the target variable is evaluated in the enclosing environment, so </p><div class="example"> <pre class="example-preformatted">e<-c(a=1,b=2) i<-1 local({ e <- c(A=10,B=11) i <-2 e[i] <<- e[i]+1 }) </pre></div> <p>uses the local value of <code class="code">i</code> on both the LHS and RHS, and the local value of <code class="code">e</code> on the RHS of the superassignment statement. It sets <code class="code">e</code> in the outer environment to </p><div class="example"> <pre class="example-preformatted"> a b 1 12 </pre></div> <p>That is, the superassignment is equivalent to the four lines </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- get(e, envir=parent.env(), inherits=TRUE) `*tmp*`[i] <- e[i]+1 e <<- `*tmp*` rm(`*tmp*`) </pre></div> <p>Similarly </p><div class="example"> <pre class="example-preformatted">x[is.na(x)] <<- 0 </pre></div> <p>is equivalent to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- get(x,envir=parent.env(), inherits=TRUE) `*tmp*`[is.na(x)] <- 0 x <<- `*tmp*` rm(`*tmp*`) </pre></div> <p>and not to </p><div class="example"> <pre class="example-preformatted">`*tmp*` <- get(x,envir=parent.env(), inherits=TRUE) `*tmp*`[is.na(`*tmp*`)] <- 0 x <<- `*tmp*` rm(`*tmp*`) </pre></div> <p>These two candidate interpretations differ only if there is also a local variable <code class="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> </div> </div> <div class="section-level-extent" id="Scope-of-variables"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Scope-of-variables-1"><span>3.5 Scope of variables<a class="copiable-link" href="#Scope-of-variables-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-scope"></a> <a class="index-entry-id" id="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 class="emph">lexical scoping</em> model, similar to languages like Pascal. However, R is a <em class="emph">functional programming language</em> and allows dynamic creation and manipulation of functions and language objects, and has additional features reflecting this fact. </p> <ul class="mini-toc"> <li><a href="#Global-environment" accesskey="1">Global environment</a></li> <li><a href="#Lexical-environment" accesskey="2">Lexical environment</a></li> <li><a href="#Stacks" accesskey="3">The call stack</a></li> <li><a href="#Search-path" accesskey="4">Search path</a></li> </ul> <hr> <div class="subsection-level-extent" id="Global-environment"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Global-environment-1"><span>3.5.1 Global environment<a class="copiable-link" href="#Global-environment-1"> ¶</a></span></h4> <p>The global <a class="index-entry-id" id="index-environment-5"></a> environment is the root of the user workspace. An <a class="index-entry-id" id="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> </div> <div class="subsection-level-extent" id="Lexical-environment"> <div class="nav-panel"> <p> Next: <a href="#Stacks" accesskey="n" rel="next">The call stack</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> [<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> <h4 class="subsection" id="Lexical-environment-1"><span>3.5.2 Lexical environment<a class="copiable-link" href="#Lexical-environment-1"> ¶</a></span></h4> <p>Every call to a <a class="index-entry-id" id="index-function-8"></a> function creates a <a class="index-entry-id" id="index-frame"></a> <a class="index-entry-id" id="index-environment-6"></a> <em class="emph">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—in particular, they are not copied on assignment. </p> <p>A closure (mode <code class="code">"function"</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 class="code">environment<-</code>). When the function is subsequently called, its <a class="index-entry-id" id="index-environment_002c-evaluation"></a> evaluation environment is created with the closure’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 class="index-entry-id" id="index-function-9"></a> function, it is first sought in the <a class="index-entry-id" id="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> </div> <div class="subsection-level-extent" id="Stacks"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="The-call-stack"><span>3.5.3 The call stack<a class="copiable-link" href="#The-call-stack"> ¶</a></span></h4> <p>Every time a <a class="index-entry-id" id="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 class="emph">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 class="index-entry-id" id="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 class="index-entry-id" id="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 class="index-entry-id" id="index-call-stack"></a> call stack is provided through a family of functions which have names that start with ‘<samp class="samp">sys.</samp>’. They are listed briefly below. </p> <a class="index-entry-id" id="index-evaluation"></a> <dl class="table"> <dt><code class="code">sys.call</code></dt> <dd><p>Get the call for the specified context. </p></dd> <dt><code class="code">sys.frame</code></dt> <dd><p>Get the evaluation frame for the specified context. </p></dd> <dt><code class="code">sys.nframe</code></dt> <dd><p>Get the environment frame for all active contexts. </p></dd> <dt><code class="code">sys.function</code></dt> <dd><p>Get the function being invoked in the specified context. </p></dd> <dt><code class="code">sys.parent</code></dt> <dd><p>Get the parent of the current function invocation. </p></dd> <dt><code class="code">sys.calls</code></dt> <dd><p>Get the calls for all the active contexts. </p></dd> <dt><code class="code">sys.frames</code></dt> <dd><p>Get the evaluation frames for all the active contexts. </p></dd> <dt><code class="code">sys.parents</code></dt> <dd><p>Get the numeric labels for all active contexts. </p></dd> <dt><code class="code">sys.on.exit</code></dt> <dd><p>Set a function to be executed when the specified context is exited. </p></dd> <dt><code class="code">sys.status</code></dt> <dd><p>Calls <code class="code">sys.frames</code>, <code class="code">sys.parents</code> and <code class="code">sys.calls</code>. </p></dd> <dt><code class="code">parent.frame</code></dt> <dd><p>Get the evaluation frame for the specified parent context. </p></dd> </dl> <hr> </div> <div class="subsection-level-extent" id="Search-path"> <div class="nav-panel"> <p> Previous: <a href="#Stacks" accesskey="p" rel="prev">The call stack</a>, Up: <a href="#Scope-of-variables" accesskey="u" rel="up">Scope of variables</a> [<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> <h4 class="subsection" id="Search-path-1"><span>3.5.4 Search path<a class="copiable-link" href="#Search-path-1"> ¶</a></span></h4> <p>In addition to the evaluation <a class="index-entry-id" id="index-environment-8"></a> <a class="index-entry-id" id="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 class="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 class="code">attach</code> or <code class="code">library</code>. </p> <a class="index-entry-id" id="index-namespace"></a> <p>Packages which have a <em class="emph">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> </div> </div> </div> <div class="chapter-level-extent" id="Functions"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Functions-1"><span>4 Functions<a class="copiable-link" href="#Functions-1"> ¶</a></span></h2> <ul class="mini-toc"> <li><a href="#Writing-functions" accesskey="1">Writing functions</a></li> <li><a href="#Functions-as-objects" accesskey="2">Functions as objects</a></li> <li><a href="#Evaluation" accesskey="3">Evaluation</a></li> </ul> <hr> <div class="section-level-extent" id="Writing-functions"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Writing-functions-1"><span>4.1 Writing functions<a class="copiable-link" href="#Writing-functions-1"> ¶</a></span></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 class="index-entry-id" id="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 data-manual="R-exts" href="https://cran.r-project.org/doc/manuals/R-exts.html#Writing-R-documentation">Writing R documentation</a> in <cite class="cite">Writing R Extensions</cite>. </p> <ul class="mini-toc"> <li><a href="#Syntax-and-examples" accesskey="1">Syntax and examples</a></li> <li><a href="#Arguments" accesskey="2">Arguments</a></li> </ul> <hr> <div class="subsection-level-extent" id="Syntax-and-examples"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Syntax-and-examples-1"><span>4.1.1 Syntax and examples<a class="copiable-link" href="#Syntax-and-examples-1"> ¶</a></span></h4> <p>The syntax for writing a <a class="index-entry-id" id="index-function-12"></a> function is </p> <div class="example"> <pre class="example-preformatted">function ( <var class="var">arglist</var> ) <var class="var">body</var> </pre></div> <p>The first component of the function declaration is the keyword <code class="code">function</code> which indicates to R that you want to create a function. </p> <p>An <a class="index-entry-id" id="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 ‘<samp class="samp"><var class="var">symbol</var> = <var class="var">expression</var></samp>’, or the special formal argument <code class="code">...</code>. </p> <p>The <em class="emph">body</em> can be any valid R expression. Generally, the body is a group of expressions contained in curly braces (‘<samp class="samp">{</samp>’ and ‘<samp class="samp">}</samp>’). </p> <p>Generally <a class="index-entry-id" id="index-function-13"></a> functions are assigned to symbols but they don’t need to be. The value returned by the call to <code class="code">function</code> is a function. If this is not given a name it is referred to as an <a class="index-entry-id" id="index-function_002c-anonymous"></a> anonymous function. Anonymous functions are most frequently used as arguments to other functions such as the <code class="code">apply</code> family or <code class="code">outer</code>. </p> <p>Here is a simple function: <code class="code">echo <- function(x) print(x)</code>. So <code class="code">echo</code> is a function that takes a single argument and when <code class="code">echo</code> is invoked it prints its argument. </p> <hr> </div> <div class="subsection-level-extent" id="Arguments"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Arguments-1"><span>4.1.2 Arguments<a class="copiable-link" href="#Arguments-1"> ¶</a></span></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 class="index-entry-id" id="index-argument_002c-default-values"></a> <p>Default values for arguments can be specified using the special form ‘<samp class="samp"><var class="var">name</var> = <var class="var">expression</var></samp>’. 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 class="var">expression</var> is <a class="index-entry-id" id="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 class="code">missing</code>. When <code class="code">missing</code> is called with the <a class="index-entry-id" id="index-name-5"></a> name of a formal argument it returns <code class="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 class="code">missing</code> will thus have its default value, if any. The <code class="code">missing</code> function does not force evaluation of the argument. </p> <p>The special type of argument <code class="code">...</code> can contain any number of supplied arguments. It is used for a variety of purposes. It allows you to write a <a class="index-entry-id" id="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> </div> </div> <div class="section-level-extent" id="Functions-as-objects"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Functions-as-objects-1"><span>4.2 Functions as objects<a class="copiable-link" href="#Functions-as-objects-1"> ¶</a></span></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 class="ref" href="#Function-objects">Function objects</a> for the details. </p> <hr> </div> <div class="section-level-extent" id="Evaluation"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Evaluation-1"><span>4.3 Evaluation<a class="copiable-link" href="#Evaluation-1"> ¶</a></span></h3> <ul class="mini-toc"> <li><a href="#Evaluation-environment" accesskey="1">Evaluation environment</a></li> <li><a href="#Argument-matching" accesskey="2">Argument matching</a></li> <li><a href="#Argument-evaluation" accesskey="3">Argument evaluation</a></li> <li><a href="#Scope" accesskey="4">Scope</a></li> </ul> <hr> <div class="subsection-level-extent" id="Evaluation-environment"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Evaluation-environment-1"><span>4.3.1 Evaluation environment<a class="copiable-link" href="#Evaluation-environment-1"> ¶</a></span></h4> <p>When a <a class="index-entry-id" id="index-function-15"></a> function is called or invoked a new <a class="index-entry-id" id="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 class="ref" href="#Argument-matching">Argument matching</a>. The statements in the body of the function are evaluated sequentially in this <a class="index-entry-id" id="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 class="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> </div> <div class="subsection-level-extent" id="Argument-matching"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Argument-matching-1"><span>4.3.2 Argument matching<a class="copiable-link" href="#Argument-matching-1"> ¶</a></span></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 class="code">log</code>, <code class="code">round</code>, <code class="code">signif</code>, <code class="code">rep</code> and <code class="code">seq.int</code>. </p> <p>The first thing that occurs in a <a class="index-entry-id" id="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 class="enumerate"> <li> <strong class="strong">Exact matching on tags</strong>. <a class="index-entry-id" id="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 class="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 class="code">f <- function(fumble, fooey) fbody</code>, then <code class="code">f(f = 1, fo = 2)</code> is illegal, even though the 2nd actual argument only matches <code class="code">fooey</code>. <code class="code">f(f = 1, fooey = 2)</code> <em class="emph">is</em> legal though since the second argument matches exactly and is removed from consideration for partial matching. If the formal arguments contain <code class="code">...</code> then partial matching is only applied to arguments that precede it. </li><li> <strong class="strong">Positional matching</strong>. Any unmatched formal arguments are bound to <em class="emph">unnamed</em> supplied arguments, in order. If there is a <code class="code">...</code> 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 class="code">match.arg</code>, <code class="code">match.call</code> and <code class="code">match.fun</code>. <a class="index-entry-id" id="index-match_002earg"></a> <a class="index-entry-id" id="index-match_002ecall"></a> <a class="index-entry-id" id="index-match_002efun"></a> Access to the partial matching algorithm used by R is via <code class="code">pmatch</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Argument-evaluation"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Argument-evaluation-1"><span>4.3.3 Argument evaluation<a class="copiable-link" href="#Argument-evaluation-1"> ¶</a></span></h4> <p>One of the most important things to know about the <a class="index-entry-id" id="index-evaluation_002c-argument"></a> evaluation of arguments to a <a class="index-entry-id" id="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 class="emph">call-by-value</em>. In general, supplied arguments behave as if they are local variables initialized with the value supplied and the <a class="index-entry-id" id="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 class="strong">C</strong> it is common to use the form, <code class="code">foo(x = y)</code> to invoke <code class="code">foo</code> with the value of <code class="code">y</code> and simultaneously to assign the value of <code class="code">y</code> to <code class="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 class="index-entry-id" id="index-assignment-5"></a> assignment may not take place. </p> <p>It is also worth noting that the effect of <code class="code">foo(x <- y)</code> if the argument is evaluated is to change the value of <code class="code">x</code> in the calling <a class="index-entry-id" id="index-environment-10"></a> environment and not in the <a class="index-entry-id" id="index-environment_002c-evaluation-2"></a> evaluation environment of <code class="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 class="index-entry-id" id="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 class="code">substitute</code>. </p> <p>When a <a class="index-entry-id" id="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 class="index-entry-id" id="index-environment-11"></a> environment set to the local environment. </p> <p>The process of filling the value slot of a promise by <a class="index-entry-id" id="index-evaluation-2"></a> evaluating the contents of the expression slot in the promise’s environment is called <em class="emph">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 class="index-entry-id" id="index-function-20"></a> <a class="index-entry-id" id="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 class="code">label</code> ensures that the label is based on the value of <code class="code">x</code> before it is changed in the next line. </p> <div class="example"> <pre class="example-preformatted">function(x, label = deparse(x)) { label x <- 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> </div> <div class="subsection-level-extent" id="Scope"> <div class="nav-panel"> <p> Previous: <a href="#Argument-evaluation" accesskey="p" rel="prev">Argument evaluation</a>, Up: <a href="#Evaluation" accesskey="u" rel="up">Evaluation</a> [<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> <h4 class="subsection" id="Scope-1"><span>4.3.4 Scope<a class="copiable-link" href="#Scope-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-scope-2"></a> <p>Scope or the scoping rules are simply the set of rules used by the <a class="index-entry-id" id="index-evaluation_002c-symbol-2"></a> evaluator to find a value for a <a class="index-entry-id" id="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 class="emph">lexical scope</em>. This means the variable <a class="index-entry-id" id="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 class="index-entry-id" id="index-scope-3"></a> scope are involved with evaluating <a class="index-entry-id" id="index-function-21"></a> functions and we concentrate on this issue. A symbol can be either <a class="index-entry-id" id="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 class="index-entry-id" id="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 class="index-entry-id" id="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 class="index-entry-id" id="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-preformatted">f <- function() { y <- 10 g <- function(x) x + y return(g) } h <- f() h(3) </pre></div> <a class="index-entry-id" id="index-evaluation-3"></a> <p>A rather interesting question is what happens when <code class="code">h</code> is evaluated. 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 class="code">h(3)</code> is evaluated we see that its body is that of <code class="code">g</code>. Within that body <code class="code">x</code> is bound to the formal argument and <code class="code">y</code> is unbound. In a language with <a class="index-entry-id" id="index-scope-4"></a> lexical scope <code class="code">x</code> will be associated with the value 3 and <code class="code">y</code> with the value 10 local to <code class="code">f</code> so <code class="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 class="code">y</code> is not found, unless there is a variable <code class="code">y</code> in your workspace in which case its value will be used. </p> <hr> </div> </div> </div> <div class="chapter-level-extent" id="Object_002doriented-programming"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Object_002doriented-programming-1"><span>5 Object-oriented programming<a class="copiable-link" href="#Object_002doriented-programming-1"> ¶</a></span></h2> <a class="index-entry-id" id="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="emph">class</em> is a definition of an object. Typically a class contains several <em class="emph">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 class="emph">methods</em>. Methods are basically <a class="index-entry-id" id="index-function-23"></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 class="emph">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 class="code">print</code> methods, <code class="code">summary</code> methods and <code class="code">plot</code> methods. These methods allow us to have one generic <a class="index-entry-id" id="index-function_002c-generic"></a> function call, <code class="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> <ul class="mini-toc"> <li><a href="#Definition" accesskey="1">Definition</a></li> <li><a href="#Inheritance" accesskey="2">Inheritance</a></li> <li><a href="#Method-dispatching" accesskey="3">Method dispatching</a></li> <li><a href="#UseMethod" accesskey="4"><code class="code">UseMethod</code></a></li> <li><a href="#NextMethod" accesskey="5"><code class="code">NextMethod</code></a></li> <li><a href="#Group-methods" accesskey="6">Group methods</a></li> <li><a href="#Writing-methods" accesskey="7">Writing methods</a></li> </ul> <hr> <div class="section-level-extent" id="Definition"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Definition-1"><span>5.1 Definition<a class="copiable-link" href="#Definition-1"> ¶</a></span></h3> <p>Rather than having a full-fledged <a class="index-entry-id" id="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 class="code">.Generic</code>, <code class="code">.Class</code>, <code class="code">.Method</code> and <code class="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">class</code> attribute. This attribute is a character vector of class names. So to create an object of class <code class="code">"foo"</code> one simply attaches a class attribute with the string ‘<samp class="samp">"foo"</samp>’ in it. Thus, virtually anything can be turned in to an object of class <code class="code">"foo"</code>. </p> <p>The object system makes use of <a class="index-entry-id" id="index-function_002c-generic-1"></a> <em class="emph">generic functions</em> via two dispatching functions, <code class="code">UseMethod</code> and <code class="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 class="code">mean</code> is just such a function, </p> <div class="example"> <pre class="example-preformatted">> mean function (x, ...) UseMethod("mean") </pre></div> <p>When <code class="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">.Class</code> is set to the class attribute of <code class="code">x</code>, <code class="code">.Generic</code> is set to the string <code class="code">"mean"</code> and a search is made for the correct method to invoke. The class attributes of any other arguments to <code class="code">mean</code> are ignored. </p> <p>Suppose that <code class="code">x</code> had a class attribute that contained <code class="code">"foo"</code> and <code class="code">"bar"</code>, in that order. Then R would first search for a function called <code class="code">mean.foo</code> and if it did not find one it would then search for a function <code class="code">mean.bar</code> and if that search was also unsuccessful then a final search for <code class="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 class="code">mean.foo</code> etc. are referred to, in this context, as methods. </p> <p><code class="code">NextMethod</code> provides another mechanism for dispatching. A <a class="index-entry-id" id="index-function-24"></a> function may have a call to <code class="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">.Class</code> and <code class="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 class="code">.Generic</code> or <code class="code">.Class</code>. </p> <p>If a method is invoked directly and it contains a call to <code class="code">NextMethod</code> then the first argument to <code class="code">NextMethod</code> is used to determine the <a class="index-entry-id" id="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">.Class</code>. </p> <p>Methods themselves employ <code class="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 class="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 class="code">"xypoint"</code> and <code class="code">"rthetapoint"</code>. All the ‘xypoint’ data structures are lists with an x-component and a y-component. All ‘rthetapoint’ 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 class="index-entry-id" id="index-function_002c-generic-3"></a> generic functions. We define the generic function <code class="code">xpos</code> as follows. </p> <div class="example"> <pre class="example-preformatted">xpos <- function(x, ...) UseMethod("xpos") </pre></div> <p>Now we can define methods: </p> <div class="example"> <pre class="example-preformatted">xpos.xypoint <- function(x) x$x xpos.rthetapoint <- function(x) x$r * cos(x$theta) </pre></div> <p>The user simply calls the function <code class="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 class="code">print</code>. </p> <hr> </div> <div class="section-level-extent" id="Inheritance"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Inheritance-1"><span>5.2 Inheritance<a class="copiable-link" href="#Inheritance-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-evaluation-4"></a> <p>The class attribute of an object can have several elements. When a <a class="index-entry-id" id="index-function_002c-generic-4"></a> generic function is called the first inheritance is mainly handled through <code class="code">NextMethod</code>. <code class="code">NextMethod</code> determines the method currently being evaluated, finds the next class from the </p> <p>FIXME: something is missing here </p> <hr> </div> <div class="section-level-extent" id="Method-dispatching"> <div class="nav-panel"> <p> Next: <a href="#UseMethod" accesskey="n" rel="next"><code class="code">UseMethod</code></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> [<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> <h3 class="section" id="Method-dispatching-1"><span>5.3 Method dispatching<a class="copiable-link" href="#Method-dispatching-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-function_002c-generic-5"></a> <p>Generic functions should consist of a single statement. They should usually be of the form <code class="code">foo <- function(x, ...) UseMethod("foo", x)</code>. When <code class="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 class="index-entry-id" id="index-name-8"></a> name of the generic function is combined with the first element of the class attribute into the form, <code class="code"><var class="var">generic</var>.<var class="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 class="code"><var class="var">generic</var>.<var class="var">default</var></code> is used. If the first argument to the generic function has no class attribute then <code class="code"><var class="var">generic</var>.<var class="var">default</var></code> is used. Since the introduction of namespaces the methods may not be accessible by their names (i.e. <code class="code">get("<var class="var">generic</var>.<var class="var">class</var>")</code> may fail), but they will be accessible by <code class="code">getS3method("<var class="var">generic</var>","<var class="var">class</var>")</code>. </p> <a class="index-entry-id" id="index-object-4"></a> <p>Any object can have a <code class="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> </div> <div class="section-level-extent" id="UseMethod"> <div class="nav-panel"> <p> Next: <a href="#NextMethod" accesskey="n" rel="next"><code class="code">NextMethod</code></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> [<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> <h3 class="section" id="UseMethod-1"><span>5.4 <code class="code">UseMethod</code><a class="copiable-link" href="#UseMethod-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-UseMethod"></a> <p><code class="code">UseMethod</code> is a special function and it behaves differently from other function calls. The syntax of a call to it is <code class="code">UseMethod(<var class="var">generic</var>, <var class="var">object</var>)</code>, where <var class="var">generic</var> is the name of the generic function, <var class="var">object</var> is the object used to determine which method should be chosen. <code class="code">UseMethod</code> can only be called from the body of a function. </p> <a class="index-entry-id" id="index-evaluation-5"></a> <p><code class="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 class="index-entry-id" id="index-environment-13"></a> environment; this process will be described shortly. The second way in which <code class="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 class="code">UseMethod</code> are guaranteed not to be executed. </p> <p>When <code class="code">UseMethod</code> is invoked the generic function is the specified value in the call to <code class="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 class="code">foo</code> and the class of the object is <code class="code">"bar"</code>, then R will search for a method named <code class="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 class="index-entry-id" id="index-environment-14"></a> environment R uses the environment of the current function call (the call to the generic). Any <a class="index-entry-id" id="index-assignment-7"></a> assignments or evaluations that were made before the call to <code class="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 class="code">UseMethod</code> has the effect of placing some special objects in the evaluation frame. They are <code class="code">.Class</code>, <code class="code">.Generic</code> and <code class="code">.Method</code>. These special objects are used to by R to handle the method dispatch and inheritance. <code class="code">.Class</code> is the class of the object, <code class="code">.Generic</code> is the name of the generic function and <code class="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 class="code">.Group</code>. This will be described in Section <a class="ref" href="#Group-methods">Group methods</a>. After the initial call to <code class="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. [Prior to R 4.4.0 any local <a class="index-entry-id" id="index-assignment-8"></a> assignments in the frame of the generic would be carried forward into the call to the method; this is no longer the case.] It is important to realize that control will never return to the generic and hence any expressions after a call to <code class="code">UseMethod</code> will never be executed. </p> <p>Any arguments to the generic that were evaluated prior to the call to <code class="code">UseMethod</code> remain evaluated. </p> <p>If the first argument to <code class="code">UseMethod</code> is not supplied it is assumed to be the name of the current function. If two arguments are supplied to <code class="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 class="code">NextMethod</code> where the arguments in the call to the next method can be altered. </p> <hr> </div> <div class="section-level-extent" id="NextMethod"> <div class="nav-panel"> <p> Next: <a href="#Group-methods" accesskey="n" rel="next">Group methods</a>, Previous: <a href="#UseMethod" accesskey="p" rel="prev"><code class="code">UseMethod</code></a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> [<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> <h3 class="section" id="NextMethod-1"><span>5.5 <code class="code">NextMethod</code><a class="copiable-link" href="#NextMethod-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-NextMethod"></a> <p><code class="code">NextMethod</code> is used to provide a simple inheritance mechanism. </p> <p>Methods invoked as a result of a call to <code class="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 <code class="code">NextMethod</code> was invoked. </p> <p>Unevaluated arguments remain unevaluated. Missing arguments remain missing. </p> <p>The syntax for a call to <code class="code">NextMethod</code> is <code class="code">NextMethod(generic, object, ...)</code>. If the <code class="code">generic</code> is not supplied the value of <code class="code">.Generic</code> is used. If the <code class="code">object</code> is not supplied the first argument in the call to the current method is used. Values in the <code class="code">...</code> 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 class="code">.Generic</code> and <code class="code">.Class</code> and not on the object. So changing the object in a call to <code class="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 class="code">.Generic</code>, <code class="code">.Class</code> or <code class="code">.Method</code>. In this case the <code class="code">generic</code> argument of <code class="code">NextMethod</code> must be specified. The value of <code class="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 class="code">.Method</code> is the name of the current function. These choices for default values ensure that the behaviour of a method doesn’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 <code class="code">...</code> argument to <code class="code">NextMethod</code>. The White Book describes the behaviour as follows: </p> <a class="index-entry-id" id="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 <code class="code">NextMethod</code>; -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 <code class="code">.Class</code>, second from the first argument to the method and last from the object specified in the call to <code class="code">NextMethod</code> </p> <p>Generic: comes first from <code class="code">.Generic</code>, if nothing then from the first argument to the method and if it’s still missing from the call to <code class="code">NextMethod</code> </p> <p>Method: this should just be the current function name. </p> <hr> </div> <div class="section-level-extent" id="Group-methods"> <div class="nav-panel"> <p> Next: <a href="#Writing-methods" accesskey="n" rel="next">Writing methods</a>, Previous: <a href="#NextMethod" accesskey="p" rel="prev"><code class="code">NextMethod</code></a>, Up: <a href="#Object_002doriented-programming" accesskey="u" rel="up">Object-oriented programming</a> [<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> <h3 class="section" id="Group-methods-1"><span>5.6 Group methods<a class="copiable-link" href="#Group-methods-1"> ¶</a></span></h3> <p>For several types of <a class="index-entry-id" id="index-function_002c-internal-1"></a> internal functions R provides a dispatching mechanism for operators. This means that operators such as <code class="code">==</code> or <code class="code"><</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 class="table"> <dt>‘<samp class="samp">Math</samp>’</dt> <dd><p><code class="code">abs</code>, <code class="code">acos</code>, <code class="code">acosh</code>, <code class="code">asin</code>, <code class="code">asinh</code>, <code class="code">atan</code>, <code class="code">atanh</code>, <code class="code">ceiling</code>, <code class="code">cos</code>, <code class="code">cosh</code>, <code class="code">cospi</code>, <code class="code">cumsum</code>, <code class="code">exp</code>, <code class="code">floor</code>, <code class="code">gamma</code>, <code class="code">lgamma</code>, <code class="code">log</code>, <code class="code">log10</code>, <code class="code">round</code>, <code class="code">signif</code>, <code class="code">sin</code>, <code class="code">sinh</code>, <code class="code">sinpi</code>, <code class="code">tan</code>, <code class="code">tanh</code>, <code class="code">tanpi</code>, <code class="code">trunc</code> </p> </dd> <dt>‘<samp class="samp">Summary</samp>’</dt> <dd><p><code class="code">all</code>, <code class="code">any</code>, <code class="code">max</code>, <code class="code">min</code>, <code class="code">prod</code>, <code class="code">range</code>, <code class="code">sum</code> </p> </dd> <dt>‘<samp class="samp">Ops</samp>’</dt> <dd><p><code class="code">+</code>, <code class="code">-</code>, <code class="code">*</code>, <code class="code">/</code>, <code class="code">^</code>, <code class="code"><</code>, <code class="code">></code>, <code class="code"><=</code>, <code class="code">>=</code>, <code class="code">!=</code>, <code class="code">==</code>, <code class="code">%%</code>, <code class="code">%/%</code>, <code class="code">&</code>, <code class="code">|</code>, <code class="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 class="code">.Method</code> is a character vector with two elements. The elements of <code class="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 class="code">.Method</code> is set to the zero length string, <code class="code">""</code>. </p> <hr> </div> <div class="section-level-extent" id="Writing-methods"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Writing-methods-1"><span>5.7 Writing methods<a class="copiable-link" href="#Writing-methods-1"> ¶</a></span></h3> <p>Users can easily write their own methods and generic functions. A <a class="index-entry-id" id="index-function_002c-generic-6"></a> generic function is simply a function with a call to <code class="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 class="code">UseMethod</code> or <code class="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 class="code">UseMethod</code> having been made and hence the special variables <code class="code">.Generic</code>, <code class="code">.Class</code> and <code class="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 class="index-entry-id" id="index-function_002c-generic-7"></a> generic functions is to provide <code class="code">print</code> and <code class="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 class="code">print</code> or <code class="code">summary</code> will provide nice output for the results of any analysis. </p> <hr> </div> </div> <div class="chapter-level-extent" id="Computing-on-the-language"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Computing-on-the-language-1"><span>6 Computing on the language<a class="copiable-link" href="#Computing-on-the-language-1"> ¶</a></span></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 “functional programming” variety, but in contrast to FORTRAN and the ALGOL family. The Lisp family takes this feature to the extreme by the “everything is a list” 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 class="index-entry-id" id="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 class="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 class="code">curve</code> function, which allows you to draw the graph of a function given as an expression like <code class="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> <ul class="mini-toc"> <li><a href="#Direct-manipulation-of-language-objects" accesskey="1">Direct manipulation of language objects</a></li> <li><a href="#Substitutions" accesskey="2">Substitutions</a></li> <li><a href="#More-on-evaluation" accesskey="3">More on evaluation</a></li> <li><a href="#Evaluation-of-expression-objects" accesskey="4">Evaluation of expression objects</a></li> <li><a href="#Manipulation-of-function-calls" accesskey="5">Manipulation of function calls</a></li> <li><a href="#Manipulation-of-functions" accesskey="6">Manipulation of functions</a></li> </ul> <hr> <div class="section-level-extent" id="Direct-manipulation-of-language-objects"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Direct-manipulation-of-language-objects-1"><span>6.1 Direct manipulation of language objects<a class="copiable-link" href="#Direct-manipulation-of-language-objects-1"> ¶</a></span></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 “unevaluated expressions”, although this terminology is somewhat confusing. The most direct method of obtaining a call object is to use <code class="code">quote</code> with an expression argument, e.g., </p> <div class="example"> <pre class="example-preformatted">> e1 <- quote(2 + 2) > e2 <- quote(plot(x, y)) </pre></div> <p>The arguments are not evaluated, the result is simply the parsed argument. The objects <code class="code">e1</code> and <code class="code">e2</code> may be evaluated later using <code class="code">eval</code>, or simply manipulated as data. It is perhaps most immediately obvious why the <code class="code">e2</code> object has mode <code class="code">"call"</code>, since it involves a call to the <code class="code">plot</code> function with some arguments. However, <code class="code">e1</code> actually has exactly the same structure as a call to the binary operator <code class="code">+</code> with two arguments, a fact that gets clearly displayed by the following </p> <div class="example"> <pre class="example-preformatted">> quote("+"(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 class="code">as.list</code> and <code class="code">as.call</code> </p> <div class="example"> <pre class="example-preformatted">> e2[[1]] plot > e2[[2]] x > 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-preformatted">> e3 <- quote(plot(x = age, y = weight)) > e3$x age > e3$y weight </pre></div> <p>All the components of the call object have mode <code class="code">"name"</code> in the preceding examples. This is true for identifiers in calls, but the components of a call can also be constants—which can be of any type, although the first component had better be a function if the call is to be evaluated successfully—or other call objects, corresponding to subexpressions. Objects of mode <a class="index-entry-id" id="index-name-10"></a> name can be constructed from character strings using <code class="code">as.name</code>, so one might modify the <code class="code">e2</code> object as follows </p> <div class="example"> <pre class="example-preformatted">> e2[[1]] <- as.name("+") > 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-preformatted">> e1[[2]] <- e2 > 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 class="code">4 - (2 - 2)</code> becomes <code class="code">"-"(4, "(" ("-"(2, 2)))</code> in prefix notation. In evaluations, the ‘<samp class="samp">(</samp>’ operator just returns its argument. </p> <p>This is a bit unfortunate, but it is not easy to write a <a class="index-entry-id" id="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’s parser is not perfectly invertible, nor is its deparser, as the following examples show </p> <div class="example"> <pre class="example-preformatted">> str(quote(c(1,2))) language c(1, 2) > str(c(1,2)) num [1:2] 1 2 > deparse(quote(c(1,2))) [1] "c(1, 2)" > deparse(c(1,2)) [1] "c(1, 2)" > quote("-"(2, 2)) 2 - 2 > 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> </div> <div class="section-level-extent" id="Substitutions"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Substitutions-1"><span>6.2 Substitutions<a class="copiable-link" href="#Substitutions-1"> ¶</a></span></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 class="code">plot.default</code>: <a class="index-entry-id" id="index-substitute"></a> </p> <div class="example"> <pre class="example-preformatted">xlabel <- if (!missing(x)) deparse(substitute(x)) </pre></div> <p>This causes the variable or expression given as the <code class="code">x</code> argument to <code class="code">plot</code> to be used for labeling the x-axis later on. </p> <p>The function used to achieve this is <code class="code">substitute</code> which takes the expression <code class="code">x</code> and substitutes the expression that was passed through the formal argument <code class="code">x</code>. Notice that for this to happen, <code class="code">x</code> must carry information about the expression that creates its value. This is related to the <a class="index-entry-id" id="index-evaluation_002c-lazy-1"></a> lazy evaluation scheme of R (see <a class="pxref" href="#Promise-objects">Promise objects</a>). A formal argument is really a <em class="emph">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 class="code">substitute</code> will recognize a promise variable and substitute the value of its expression slot. If <code class="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 class="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 class="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-preformatted">> substitute(a + b, list(a = 1, b = quote(x))) 1 + x </pre></div> <p>Notice that quoting was necessary to substitute the <code class="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-preformatted">> plot(0) > 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 class="code">substitute(x <- x + 1, list(x = 2))</code> will happily return <code class="code">2 <- 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 “math in graphs” feature often involves constructions that are syntactically correct, but which would be meaningless to evaluate, like ‘<samp class="samp">{}>=40*" years"</samp>’. </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 class="code">substitute</code> once more, like this </p> <div class="example"> <pre class="example-preformatted">> expr <- quote(x + y) > substitute(substitute(e, list(x = 3)), list(e = expr)) substitute(x + y, list(x = 3)) > 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 class="index-entry-id" id="index-symbol-4"></a> symbol in the <a class="index-entry-id" id="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 class="emph">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 class="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 class="code">substitute</code> is used. R will then use the new value of the variable, whereas S will unconditionally use the argument expression—unless it was a constant, which has the curious consequence that <code class="code">f((1))</code> may be very different from <code class="code">f(1)</code> in S. The R rule is considerably cleaner, although it does have consequences in connection with <a class="index-entry-id" id="index-evaluation_002c-lazy-2"></a> lazy evaluation that comes as a surprise to some. Consider </p> <div class="example"> <pre class="example-preformatted">logplot <- function(y, ylab = deparse(substitute(y))) { y <- log(y) plot(y, ylab = ylab) } </pre></div> <p>This looks straightforward, but one will discover that the y label becomes an ugly <code class="code">c(...)</code> expression. It happens because the rules of lazy evaluation cause the evaluation of the <code class="code">ylab</code> expression to happen <em class="emph">after</em> <code class="code">y</code> has been modified. The solution is to force <code class="code">ylab</code> to be evaluated first, i.e., </p> <div class="example"> <pre class="example-preformatted">logplot <- function(y, ylab = deparse(substitute(y))) { ylab y <- log(y) plot(y, ylab = ylab) } </pre></div> <p>Notice that one should not use <code class="code">eval(ylab)</code> in this situation. If <code class="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 class="code">quote(log[e](y))</code> was being passed. </p> <p>A variant on <code class="code">substitute</code> is <code class="code">bquote</code>, which is used to replace some subexpressions with their values. The example from above </p><div class="example"> <pre class="example-preformatted">> plot(0) > 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-preformatted">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 class="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 class="code">bquote</code> is borrowed from the LISP backquote macro. </p> <hr> </div> <div class="section-level-extent" id="More-on-evaluation"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="More-on-evaluation-1"><span>6.3 More on evaluation<a class="copiable-link" href="#More-on-evaluation-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-evaluation-6"></a> <p>The <code class="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 class="index-entry-id" id="index-environment-15"></a> environment in which the evaluation is to take place. By default this is the evaluation frame from which <code class="code">eval</code> is called, but quite frequently it needs to be set to something else. <a class="index-entry-id" id="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 class="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 class="index-entry-id" id="index-scope-5"></a> lexical scope of the callee). Since evaluation in the parent frame occurs frequently, an <code class="code">eval.parent</code> function exists as a shorthand for <code class="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 class="code">model.frame</code> function when a <code class="code">data</code> argument is given. Generally, the terms of the model formula need to be evaluated in <code class="code">data</code>, but they may occasionally also contain references to items in the caller of <code class="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-preformatted">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 class="index-entry-id" id="index-assignment-9"></a> assignment operator, such as </p> <div class="example"> <pre class="example-preformatted">eval(quote(total <- 0), environment(robert$balance)) # <span class="r">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> </div> <div class="section-level-extent" id="Evaluation-of-expression-objects"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Evaluation-of-expression-objects-1"><span>6.4 Evaluation of expression objects<a class="copiable-link" href="#Evaluation-of-expression-objects-1"> ¶</a></span></h3> <p>Objects of mode <code class="code">"expression"</code> are defined in <a class="ref" href="#Expression-objects">Expression objects</a>. They are very similar to lists of call objects. </p> <div class="example"> <pre class="example-preformatted">> ex <- expression(2 + 2, 3 + 4) > ex[[1]] 2 + 2 > ex[[2]] 3 + 4 > 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 class="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 class="code">eval</code> function as above. The difference can be seen like this: </p> <div class="example"> <pre class="example-preformatted">> eval(substitute(mode(x), list(x = quote(2 + 2)))) [1] "numeric" > eval(substitute(mode(x), list(x = expression(2 + 2)))) [1] "expression" </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-preformatted">> e <- quote(expression(2 + 2)) > e expression(2 + 2) > mode(e) [1] "call" > ee <- expression(2 + 2) > ee expression(2 + 2) > mode(ee) [1] "expression" </pre></div> <p>I.e., <code class="code">e</code> and <code class="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> </div> <div class="section-level-extent" id="Manipulation-of-function-calls"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Manipulation-of-function-calls-1"><span>6.5 Manipulation of function calls<a class="copiable-link" href="#Manipulation-of-function-calls-1"> ¶</a></span></h3> <p>It is possible for a <a class="index-entry-id" id="index-function-25"></a> function to find out how it has been called by looking at the result of <code class="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-preformatted">> f <- function(x, y, ...) sys.call() > 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 class="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 class="code">match.call</code> is used. Here’s a variant of the preceding example, a function that returns its own call with arguments matched </p> <div class="example"> <pre class="example-preformatted">> f <- function(x, y, ...) match.call() > 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 class="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 class="code">lm</code> function: </p> <div class="example"> <pre class="example-preformatted"> mf <- cl <- match.call() mf$singular.ok <- mf$model <- mf$method <- NULL mf$x <- mf$y <- mf$qr <- mf$contrasts <- NULL mf$drop.unused.levels <- TRUE mf[[1]] <- as.name("model.frame") mf <- eval(mf, sys.frame(sys.parent())) </pre></div> <p>Notice that the resulting call is <a class="index-entry-id" id="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 class="code">NULL</code>, as seen in lines 2 and 3, and to add an argument one uses tagged list <a class="index-entry-id" id="index-assignment-10"></a> assignment (here to pass <code class="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 class="code">as.name("model.frame")</code> construction here or <code class="code">quote(model.frame)</code>. </p> <p>The <code class="code">match.call</code> function has an <code class="code">expand.dots</code> argument, a switch which if set to <code class="code">FALSE</code> lets all <code class="code">...</code> arguments be collected as a single argument with the tag <code class="code">...</code>. <a class="index-entry-id" id="index-match_002ecall-1"></a> </p> <div class="example"> <pre class="example-preformatted">> f <- function(x, y, ...) match.call(expand.dots = FALSE) > f(y = 1, 2, z = 3, 4) f(x = 2, y = 1, ... = list(z = 3, 4)) </pre></div> <p>The <code class="code">...</code> argument is a list (a pairlist to be precise), not a call to <code class="code">list</code> like it is in S: </p> <div class="example"> <pre class="example-preformatted">> e1 <- f(y = 1, 2, z = 3, 4)$... > e1 $z [1] 3 [[2]] [1] 4 </pre></div> <p>One reason for using this form of <code class="code">match.call</code> is simply to get rid of any <code class="code">...</code> arguments in order not to be passing unspecified arguments on to functions that may not know them. Here’s an example paraphrased from <code class="code">plot.formula</code>: </p> <div class="example"> <pre class="example-preformatted">m <- match.call(expand.dots = FALSE) m$... <- NULL m[[1]] <- "model.frame" </pre></div> <p>A more elaborate application is in <code class="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-preformatted">extras <- match.call(expand.dots = FALSE)$... if (length(extras) > 0) { existing <- !is.na(match(names(extras), names(call))) for (a in names(extras)[existing]) call[[a]] <- extras[[a]] if (any(!existing)) { call <- c(as.list(call), extras[!existing]) call <- as.call(call) } } </pre></div> <p>Notice that care is taken to modify existing arguments individually in case <code class="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 class="code">call</code> and <code class="code">do.call</code>. </p> <p>The function <code class="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-preformatted">> x <- 10.5 > call("round", x) round(10.5) </pre></div> <p>As seen, the value of <code class="code">x</code> rather than the <a class="index-entry-id" id="index-symbol-5"></a> symbol is inserted in the call, so it is distinctly different from <code class="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 class="code">do.call</code> is related, but evaluates the call immediately and takes the arguments from an object of mode <code class="code">"list"</code> containing all the arguments. A natural use of this is when one wants to apply a function like <code class="code">cbind</code> to all elements of a list or data frame. <a class="index-entry-id" id="index-do_002ecall"></a> </p> <div class="example"> <pre class="example-preformatted">is.na.data.frame <- function (x) { y <- do.call(cbind, lapply(x, is.na)) rownames(y) <- row.names(x) y } </pre></div> <p>Other uses include variations over constructions like <code class="code">do.call("f", 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 class="code">call</code> function. </p> <hr> </div> <div class="section-level-extent" id="Manipulation-of-functions"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Manipulation-of-functions-1"><span>6.6 Manipulation of functions<a class="copiable-link" href="#Manipulation-of-functions-1"> ¶</a></span></h3> <p>It is often useful to be able to manipulate the components of a <a class="index-entry-id" id="index-function-26"></a> function or closure. R provides a set of interface functions for this purpose. </p> <dl class="ftable"> <dt><a id="index-body-1"></a><span><code class="code">body</code><a class="copiable-link" href="#index-body-1"> ¶</a></span></dt> <dd><p>Returns the expression that is the body of the function. </p></dd> <dt><a id="index-formals-1"></a><span><code class="code">formals</code><a class="copiable-link" href="#index-formals-1"> ¶</a></span></dt> <dd><p>Returns a list of the formal arguments to the function. This is a <code class="code">pairlist</code>. </p></dd> <dt><a id="index-environment-21"></a><span><code class="code">environment</code><a class="copiable-link" href="#index-environment-21"> ¶</a></span></dt> <dd><a class="index-entry-id" id="index-environment-16"></a> <p>Returns the environment associated with the function. </p></dd> <dt><a id="index-body_003c_002d"></a><span><code class="code">body<-</code><a class="copiable-link" href="#index-body_003c_002d"> ¶</a></span></dt> <dd><p>This sets the body of the function to the supplied expression. </p></dd> <dt><a id="index-formals_003c_002d"></a><span><code class="code">formals<-</code><a class="copiable-link" href="#index-formals_003c_002d"> ¶</a></span></dt> <dd><p>Sets the formal arguments of the function to the supplied list. </p></dd> <dt><a id="index-environment_003c_002d"></a><span><code class="code">environment<-</code><a class="copiable-link" href="#index-environment_003c_002d"> ¶</a></span></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 class="code">evalq(x <- 5, environment(f))</code>. </p> <p>It is also possible to convert a <a class="index-entry-id" id="index-function-27"></a> function to a list using <code class="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 class="code">as.function</code>. This functionality is mainly included for S compatibility. Notice that environment information is lost when <code class="code">as.list</code> is used, whereas <code class="code">as.function</code> has an argument that allows the environment to be set. </p> <hr> </div> </div> <div class="chapter-level-extent" id="System-and-foreign-language-interfaces"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="System-and-foreign-language-interfaces-1"><span>7 System and foreign language interfaces<a class="copiable-link" href="#System-and-foreign-language-interfaces-1"> ¶</a></span></h2> <ul class="mini-toc"> <li><a href="#Operating-system-access" accesskey="1">Operating system access</a></li> <li><a href="#Foreign-language-interfaces" accesskey="2">Foreign language interfaces</a></li> <li><a href="#g_t_002eInternal-and-_002ePrimitive" accesskey="3">.Internal and .Primitive</a></li> </ul> <hr> <div class="section-level-extent" id="Operating-system-access"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Operating-system-access-1"><span>7.1 Operating system access<a class="copiable-link" href="#Operating-system-access-1"> ¶</a></span></h3> <p>Access to the operating system shell is via the R function <code class="code">system</code>. <a class="index-entry-id" id="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 class="code">command</code> that will be passed for execution (not necessarily by a shell) and the second argument will be <code class="code">internal</code> which if true will collect the output of the command into an R character vector. </p> <p>The functions <code class="code">system.time</code> <a class="index-entry-id" id="index-system_002etime"></a> and <code class="code">proc.time</code> <a class="index-entry-id" id="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 class="index-entry-id" id="index-environment-17"></a> environment can be accessed and manipulated with </p><blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="30%"><code class="code">Sys.getenv</code></td><td width="70%">OS environment variables <a class="index-entry-id" id="index-Sys_002egetenv"></a></td></tr> <tr><td width="30%"><code class="code">Sys.putenv</code> <a class="index-entry-id" id="index-Sys_002eputenv"></a></td></tr> <tr><td width="30%"><code class="code">Sys.getlocale</code></td><td width="70%">System locale <a class="index-entry-id" id="index-Sys_002egetlocale"></a></td></tr> <tr><td width="30%"><code class="code">Sys.putlocale</code> <a class="index-entry-id" id="index-Sys_002eputlocale"></a></td></tr> <tr><td width="30%"><code class="code">Sys.localeconv</code> <a class="index-entry-id" id="index-Sys_002elocaleconv"></a></td></tr> <tr><td width="30%"><code class="code">Sys.time</code></td><td width="70%">Current time <a class="index-entry-id" id="index-Sys_002etime"></a></td></tr> <tr><td width="30%"><code class="code">Sys.timezone</code></td><td width="70%">Time zone <a class="index-entry-id" id="index-Sys_002etimezone"></a></td></tr> </tbody> </table> </blockquote> <p>A uniform set of file access functions is provided on all platforms: </p><blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="30%"><code class="code">file.access</code></td><td width="70%">Ascertain File Accessibility <a class="index-entry-id" id="index-file_002eaccess"></a></td></tr> <tr><td width="30%"><code class="code">file.append</code></td><td width="70%">Concatenate files <a class="index-entry-id" id="index-file_002eappend"></a></td></tr> <tr><td width="30%"><code class="code">file.choose</code></td><td width="70%">Prompt user for file name <a class="index-entry-id" id="index-file_002echoose"></a></td></tr> <tr><td width="30%"><code class="code">file.copy</code></td><td width="70%">Copy files <a class="index-entry-id" id="index-file_002ecopy"></a></td></tr> <tr><td width="30%"><code class="code">file.create</code></td><td width="70%">Create or truncate a files <a class="index-entry-id" id="index-file_002ecreate"></a></td></tr> <tr><td width="30%"><code class="code">file.exists</code></td><td width="70%">Test for existence <a class="index-entry-id" id="index-file_002eexists"></a></td></tr> <tr><td width="30%"><code class="code">file.info</code></td><td width="70%">Miscellaneous file information <a class="index-entry-id" id="index-file_002einfo"></a></td></tr> <tr><td width="30%"><code class="code">file.remove</code></td><td width="70%">remove files <a class="index-entry-id" id="index-file_002eremove"></a></td></tr> <tr><td width="30%"><code class="code">file.rename</code></td><td width="70%">rename files <a class="index-entry-id" id="index-file_002erename"></a></td></tr> <tr><td width="30%"><code class="code">file.show</code></td><td width="70%">Display a text file <a class="index-entry-id" id="index-file_002eshow"></a></td></tr> <tr><td width="30%"><code class="code">unlink</code></td><td width="70%">Remove files or directories. <a class="index-entry-id" id="index-unlink"></a></td></tr> </tbody> </table> </blockquote> <p>There are also functions for manipulating file names and paths in a platform-independent way. </p><blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="30%"><code class="code">basename</code></td><td width="70%">File name without directory <a class="index-entry-id" id="index-basename"></a></td></tr> <tr><td width="30%"><code class="code">dirname</code></td><td width="70%">Directory name <a class="index-entry-id" id="index-dirname"></a></td></tr> <tr><td width="30%"><code class="code">file.path</code></td><td width="70%">Construct path to file <a class="index-entry-id" id="index-file_002epath"></a></td></tr> <tr><td width="30%"><code class="code">path.expand</code></td><td width="70%">Expand <code class="code">~</code> in Unix path <a class="index-entry-id" id="index-path_002eexpand"></a></td></tr> </tbody> </table> </blockquote> <hr> </div> <div class="section-level-extent" id="Foreign-language-interfaces"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Foreign-language-interfaces-1"><span>7.2 Foreign language interfaces<a class="copiable-link" href="#Foreign-language-interfaces-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-_002eC"></a> <a class="index-entry-id" id="index-_002eFortran"></a> <a class="index-entry-id" id="index-_002eCall"></a> <a class="index-entry-id" id="index-_002eExternal"></a> <p>See <a data-manual="R-exts" href="https://cran.r-project.org/doc/manuals/R-exts.html#System-and-foreign-language-interfaces">System and foreign language interfaces</a> in <cite class="cite">Writing R Extensions</cite> for the details of adding functionality to R via compiled code. </p> <p>Functions <code class="code">.C</code> and <code class="code">.Fortran</code> provide a standard interface to compiled code that has been linked into R, either at build time or via <code class="code">dyn.load</code>. They are primarily intended for compiled <strong class="strong">C</strong> and FORTRAN code respectively, but the <code class="code">.C</code> function can be used with other languages which can generate C interfaces, for example C++. </p> <p>Functions <code class="code">.Call</code> and <code class="code">.External</code> provide interfaces which allow compiled code (primarily compiled <strong class="strong">C</strong> code) to manipulate R objects. </p> <hr> </div> <div class="section-level-extent" id="g_t_002eInternal-and-_002ePrimitive"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="g_t_002eInternal-and-_002ePrimitive-1"><span>7.3 .Internal and .Primitive<a class="copiable-link" href="#g_t_002eInternal-and-_002ePrimitive-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-_002eInternal-1"></a> <a class="index-entry-id" id="index-_002ePrimitive-1"></a> <p>The <code class="code">.Internal</code> and <code class="code">.Primitive</code> interfaces are used to call <strong class="strong">C</strong> code compiled into R at build time. See <a data-manual="R-ints" href="https://cran.r-project.org/doc/manuals/R-ints.html#g_t_002eInternal-vs-_002ePrimitive">.Internal vs .Primitive</a> in <cite class="cite">R Internals</cite>. </p> <hr> </div> </div> <div class="chapter-level-extent" id="Exception-handling"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Exception-handling-1"><span>8 Exception handling<a class="copiable-link" href="#Exception-handling-1"> ¶</a></span></h2> <p>The exception handling facilities in R are provided through two mechanisms. Functions such as <code class="code">stop</code> or <code class="code">warning</code> can be called directly or options such as <code class="code">"warn"</code> can be used to control the handling of problems. </p> <ul class="mini-toc"> <li><a href="#stop" accesskey="1">stop</a></li> <li><a href="#warning" accesskey="2">warning</a></li> <li><a href="#on_002eexit" accesskey="3">on.exit</a></li> <li><a href="#Error-options" accesskey="4">Error options</a></li> </ul> <hr> <div class="section-level-extent" id="stop"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="stop-1"><span>8.1 stop<a class="copiable-link" href="#stop-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-stop"></a> <p>A call to <code class="code">stop</code> halts the evaluation of the current expression, prints the message argument and returns execution to top-level. </p> <hr> </div> <div class="section-level-extent" id="warning"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="warning-1"><span>8.2 warning<a class="copiable-link" href="#warning-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-warning"></a> <a class="index-entry-id" id="index-warnings"></a> <p>The function <code class="code">warning</code> takes a single argument that is a character string. The behaviour of a call to <code class="code">warning</code> depends on the value of the option <code class="code">"warn"</code>. If <code class="code">"warn"</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 class="code">"warn"</code> is zero (the default), a variable <code class="code">last.warning</code> is created and the messages associated with each call to <code class="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 class="code">last.warning</code> contains the vector of messages, and <code class="code">warnings</code> provides a way to access and print it. </p> <hr> </div> <div class="section-level-extent" id="on_002eexit"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="on_002eexit-1"><span>8.3 on.exit<a class="copiable-link" href="#on_002eexit-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-on_002eexit"></a> <p>A function can insert a call to <code class="code">on.exit</code> at any point in the body of a function. The effect of a call to <code class="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 class="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 class="code">on.exit</code> code causes an immediate jump to top-level without further processing of the <code class="code">on.exit</code> code. </p> <p><code class="code">on.exit</code> takes a single argument which is an expression to be evaluated when the function is exited. </p> <hr> </div> <div class="section-level-extent" id="Error-options"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Error-options-1"><span>8.4 Error options<a class="copiable-link" href="#Error-options-1"> ¶</a></span></h3> <p>There are a number of <code class="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 class="table"> <dt>‘<samp class="samp">warn</samp>’</dt> <dd><p>Controls the printing of warnings. </p></dd> <dt>‘<samp class="samp">warning.expression</samp>’</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>‘<samp class="samp">error</samp>’</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 class="code">options("error")</code> are evaluated before calls to <code class="code">on.exit</code> are carried out. </p> <p>One can use <code class="code">options(error = expression(q("yes")))</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> </div> </div> <div class="chapter-level-extent" id="Debugging"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Debugging-1"><span>9 Debugging<a class="copiable-link" href="#Debugging-1"> ¶</a></span></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 class="code">browser</code> or <code class="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 class="index-entry-id" id="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 class="table"> <dt>‘<samp class="samp"><kbd class="key">RET</kbd></samp>’</dt> <dd><p>Go to the next statement if the function is being debugged. Continue execution if the browser was invoked. </p></dd> <dt>‘<samp class="samp">c</samp>’</dt> <dt>‘<samp class="samp">cont</samp>’</dt> <dd><p>Continue the execution. </p></dd> <dt>‘<samp class="samp">n</samp>’</dt> <dd><p>Execute the next statement in the function. This works from the browser as well. </p></dd> <dt>‘<samp class="samp">where</samp>’</dt> <dd><p>Show the call stack </p></dd> <dt>‘<samp class="samp">Q</samp>’</dt> <dd><p>Halt execution and jump to the top-level immediately. </p></dd> </dl> <a class="index-entry-id" id="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 class="code">get</code>. A call to <code class="code">get</code> with the name in quotes will retrieve the value in the current <a class="index-entry-id" id="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 class="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 class="code">gdb</code> can be used to debug compiled code. </p> <ul class="mini-toc"> <li><a href="#browser" accesskey="1">browser</a></li> <li><a href="#debug_002fundebug" accesskey="2"><code class="code">debug</code>/<code class="code">undebug</code></a></li> <li><a href="#trace_002funtrace" accesskey="3">trace/untrace</a></li> <li><a href="#traceback" accesskey="4">traceback</a></li> </ul> <hr> <div class="section-level-extent" id="browser"> <div class="nav-panel"> <p> Next: <a href="#debug_002fundebug" accesskey="n" rel="next"><code class="code">debug</code>/<code class="code">undebug</code></a>, Previous: <a href="#Debugging" accesskey="p" rel="prev">Debugging</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> [<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> <h3 class="section" id="browser-1"><span>9.1 browser<a class="copiable-link" href="#browser-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-browser"></a> <p>A call to the function <code class="code">browser</code> causes R to halt execution at that point and to provide the user with a special prompt. Arguments to <code class="code">browser</code> are ignored. </p> <div class="example"> <pre class="example-preformatted">> foo <- function(s) { + c <- 3 + browser() + } > foo(4) Called from: foo(4) Browse[1]> s [1] 4 Browse[1]> get("c") [1] 3 Browse[1]> </pre></div> <hr> </div> <div class="section-level-extent" id="debug_002fundebug"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="debug_002fundebug-1"><span>9.2 <code class="code">debug</code>/<code class="code">undebug</code><a class="copiable-link" href="#debug_002fundebug-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-debug"></a> <a class="index-entry-id" id="index-undebug"></a> <p>The debugger can be invoked on any function by using the command <code class="code">debug(<var class="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 class="code">undebug</code> with the function as an argument. </p> <div class="example"> <pre class="example-preformatted">> debug(mean.default) > mean(1:10) debugging in: mean.default(1:10) debug: { if (na.rm) x <- x[!is.na(x)] trim <- trim[1] n <- length(c(x, recursive = TRUE)) if (trim > 0) { if (trim >= 0.5) return(median(x, na.rm = FALSE)) lo <- floor(n * trim) + 1 hi <- n + 1 - lo x <- sort(x, partial = unique(c(lo, hi)))[lo:hi] n <- hi - lo + 1 } sum(x)/n } Browse[1]> debug: if (na.rm) x <- x[!is.na(x)] Browse[1]> debug: trim <- trim[1] Browse[1]> debug: n <- length(c(x, recursive = TRUE)) Browse[1]> c exiting from: mean.default(1:10) [1] 5.5 </pre></div> <hr> </div> <div class="section-level-extent" id="trace_002funtrace"> <div class="nav-panel"> <p> Next: <a href="#traceback" accesskey="n" rel="next">traceback</a>, Previous: <a href="#debug_002fundebug" accesskey="p" rel="prev"><code class="code">debug</code>/<code class="code">undebug</code></a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> [<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> <h3 class="section" id="trace_002funtrace-1"><span>9.3 trace/untrace<a class="copiable-link" href="#trace_002funtrace-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-trace"></a> <a class="index-entry-id" id="index-untrace"></a> <p>Another way of monitoring the behaviour of R is through the <code class="code">trace</code> mechanism. <code class="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 class="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 class="code">untrace</code> with the function as an argument. </p> <div class="example"> <pre class="example-preformatted">> trace("[<-") > x <- 1:10 > x[3] <- 4 trace: "[<-"(*tmp*, 3, value = 4) </pre></div> <hr> </div> <div class="section-level-extent" id="traceback"> <div class="nav-panel"> <p> Previous: <a href="#trace_002funtrace" accesskey="p" rel="prev">trace/untrace</a>, Up: <a href="#Debugging" accesskey="u" rel="up">Debugging</a> [<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> <h3 class="section" id="traceback-1"><span>9.4 traceback<a class="copiable-link" href="#traceback-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-traceback"></a> <p>When an error has caused a jump to top-level a special variable called <code class="code">.Traceback</code> is placed into the base environment. <code class="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 class="code">.Traceback</code> can be carried out by a call to <code class="code">traceback</code>. </p> <hr> </div> </div> <div class="chapter-level-extent" id="Parser"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="chapter" id="Parser-1"><span>10 Parser<a class="copiable-link" href="#Parser-1"> ¶</a></span></h2> <a class="index-entry-id" id="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> <ul class="mini-toc"> <li><a href="#The-parsing-process" accesskey="1">The parsing process</a></li> <li><a href="#Comments" accesskey="2">Comments</a></li> <li><a href="#Tokens" accesskey="3">Tokens</a></li> <li><a href="#Expressions" accesskey="4">Expressions</a></li> <li><a href="#Directives" accesskey="5">Directives</a></li> </ul> <hr> <div class="section-level-extent" id="The-parsing-process"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="The-parsing-process-1"><span>10.1 The parsing process<a class="copiable-link" href="#The-parsing-process-1"> ¶</a></span></h3> <ul class="mini-toc"> <li><a href="#Modes-of-parsing" accesskey="1">Modes of parsing</a></li> <li><a href="#Internal-representation" accesskey="2">Internal representation</a></li> <li><a href="#Deparsing" accesskey="3">Deparsing</a></li> </ul> <hr> <div class="subsection-level-extent" id="Modes-of-parsing"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Modes-of-parsing-1"><span>10.1.1 Modes of parsing<a class="copiable-link" href="#Modes-of-parsing-1"> ¶</a></span></h4> <p>Parsing in R occurs in three different variants: </p> <ul class="itemize mark-bullet"> <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 ‘<samp class="samp">> </samp>’) indicates that the parser is ready for a new expression, and a continuation prompt (by default ‘<samp class="samp">+ </samp>’) 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 “Syntax Error” 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 class="code">parse</code> function. In particular, this is done during execution of the <code class="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 class="code">text=</code> argument to <code class="code">parse</code>. The strings are treated exactly as if they were the lines of an input file. </p> <hr> </div> <div class="subsection-level-extent" id="Internal-representation"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Internal-representation-1"><span>10.1.2 Internal representation<a class="copiable-link" href="#Internal-representation-1"> ¶</a></span></h4> <a class="index-entry-id" id="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 class="ref" href="#Language-objects">Language objects</a> and <a class="ref" href="#Expression-objects">Expression objects</a>. Briefly, every elementary R expression is stored in <a class="index-entry-id" id="index-function-28"></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 class="emph">all</em> R syntax elements are treated in this way, e.g. the assignment <code class="code">x <- 1</code> is encoded as <code class="code">"<-"(x, 1)</code>. </p> <hr> </div> <div class="subsection-level-extent" id="Deparsing"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Deparsing-1"><span>10.1.3 Deparsing<a class="copiable-link" href="#Deparsing-1"> ¶</a></span></h4> <p>Any R object can be converted to an R expression using <code class="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 class="code">"expression"</code> can be expected to be unchanged by reparsing the output of deparsing. For instance, the numeric vector <code class="code">1:5</code> will deparse as <code class="code">"c(1, 2, 3, 4, 5)"</code>, which will reparse as a call to the function <code class="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’t generated from a textual representation in the first place. </p> <hr> </div> </div> <div class="section-level-extent" id="Comments"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Comments-1"><span>10.2 Comments<a class="copiable-link" href="#Comments-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-comments"></a> <p>Comments in R are ignored by the parser. Any text from a <a class="index-entry-id" id="index-_0023"></a> <code class="code">#</code> character to the end of the line is taken to be a comment, unless the <code class="code">#</code> character is inside a quoted string. For example, </p> <div class="example"> <pre class="example-preformatted">> x <- 1 # This is a comment... > y <- " #... but this is not." </pre></div> <hr> </div> <div class="section-level-extent" id="Tokens"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Tokens-1"><span>10.3 Tokens<a class="copiable-link" href="#Tokens-1"> ¶</a></span></h3> <p>Tokens are the elementary building blocks of a programming language. They are recognised during <em class="emph">lexical analysis</em> which (conceptually, at least) takes place prior to the syntactic analysis performed by the parser itself. </p> <ul class="mini-toc"> <li><a href="#Literal-constants" accesskey="1">Constants</a></li> <li><a href="#Identifiers" accesskey="2">Identifiers</a></li> <li><a href="#Reserved-words" accesskey="3">Reserved words</a></li> <li><a href="#Special-operators" accesskey="4">Special operators</a></li> <li><a href="#Separators" accesskey="5">Separators</a></li> <li><a href="#Operator-tokens" accesskey="6">Operator tokens</a></li> <li><a href="#Grouping" accesskey="7">Grouping</a></li> <li><a href="#Indexing-tokens" accesskey="8">Indexing tokens</a></li> </ul> <hr> <div class="subsection-level-extent" id="Literal-constants"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Constants-2"><span>10.3.1 Constants<a class="copiable-link" href="#Constants-2"> ¶</a></span></h4> <p>There are five types of constants: integer, logical, numeric, complex and string. </p> <p>In addition, there is the special constant <code class="code">NULL</code>. Also, the numeric <code class="code">Inf</code>, and <code class="code">NaN</code>, the logical <code class="code">NA</code>, and <code class="code">NA_character_</code>, <code class="code">NA_integer_</code>, <code class="code">NA_real_</code>, and <code class="code">NA_complex_</code> deserve mentioning; for the latter, see <a class="ref" href="#NA-handling">NA handling</a>. </p> <p><code class="code">NULL</code> is used to indicate the empty object. <code class="code">NA</code> is used for absent (“Not Available”) data values. <code class="code">Inf</code> denotes infinity and <code class="code">NaN</code> is not-a-number in the <abbr class="acronym">IEEE</abbr> floating point calculus (results of the operations respectively <em class="math">1/0</em> and <em class="math">0/0</em>, for instance). </p> <p>Logical constants are either <code class="code">TRUE</code>, <code class="code">FALSE</code> or <code class="code">NA</code>. </p> <p>Numeric constants follow a similar syntax to that of the <strong class="strong">C</strong> language. They consist of an integer part consisting of zero or more digits, followed optionally by ‘<samp class="samp">.</samp>’ and a fractional part of zero or more digits optionally followed by an exponent part consisting of an ‘<samp class="samp">E</samp>’ or an ‘<samp class="samp">e</samp>’, 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-preformatted"><span class="r">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 ‘<samp class="samp">0x</samp>’ or ‘<samp class="samp">0x</samp>’ followed by zero or more digits, ‘<samp class="samp">a-f</samp>’ or ‘<samp class="samp">A-F</samp>’. Hexadecimal floating point constants are supported using C99 syntax, e.g. ‘<samp class="samp">0x1.1p1</samp>’. </p> <p>There is now a separate class of integer constants. They are created by using the qualifier <code class="code">L</code> at the end of the number. For example, <code class="code">123L</code> gives an integer value rather than a numeric value. The suffix <code class="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-preformatted"><span class="r">Valid integer constants:</span> 1L, 0x10L, 1000000L, 1e6L <span class="r">Valid numeric constants:</span> 1.1L, 1e-3L, 0x1.1p-2 <span class="r">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 class="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 class="code">+</code> or <code class="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 class="code">?NumericConstants</code>. </p> <p>Complex constants have the form of a decimal numeric constant followed by ‘<samp class="samp">i</samp>’. 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-preformatted"><span class="r">Valid complex constants:</span> 2i 4.1i 1e-2i </pre></div> <p>String constants are delimited by a pair of single (‘<samp class="samp">'</samp>’) or double (‘<samp class="samp">"</samp>’) quotes and can contain all other printable characters. Quotes and other special characters within strings are specified using <em class="emph">escape sequences</em>: </p> <dl class="table"> <dt><code class="code">\'</code></dt> <dd><p>single quote </p></dd> <dt><code class="code">\"</code></dt> <dd><p>double quote </p></dd> <dt><code class="code">\n</code></dt> <dd><p>newline (aka ‘line feed’, <kbd class="key">LF</kbd>) </p></dd> <dt><code class="code">\r</code></dt> <dd><p>carriage return (<kbd class="key">CR</kbd>) </p></dd> <dt><code class="code">\t</code></dt> <dd><p>tab character </p></dd> <dt><code class="code">\b</code></dt> <dd><p>backspace </p></dd> <dt><code class="code">\a</code></dt> <dd><p>bell </p></dd> <dt><code class="code">\f</code></dt> <dd><p>form feed </p></dd> <dt><code class="code">\v</code></dt> <dd><p>vertical tab </p></dd> <dt><code class="code">\\</code></dt> <dd><p>backslash itself </p></dd> <dt><code class="code">\<var class="var">nnn</var></code></dt> <dd><p>character with given octal code – sequences of one, two or three digits in the range <code class="code">0 ... 7</code> are accepted. </p></dd> <dt><code class="code">\x<var class="var">nn</var></code></dt> <dd><p>character with given hex code – sequences of one or two hex digits (with entries <code class="code">0 ... 9 A ... F a ... f</code>). </p></dd> <dt><code class="code">\u<var class="var">nnnn</var> \u{<var class="var">nnnn</var>}</code></dt> <dd><p>(where multibyte locales are supported, otherwise an error). Unicode character with given hex code – sequences of up to four hex digits. The character needs to be valid in the current locale. </p></dd> <dt><code class="code">\U<var class="var">nnnnnnnn</var> \U{<var class="var">nnnnnnnn</var>}</code></dt> <dd><p>(where multibyte locales are supported, otherwise an error). Unicode character with given hex code – 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>A <abbr class="abbr">NUL</abbr> (<code class="code">\0</code>) is not allowed in a character string, so using <code class="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> </div> <div class="subsection-level-extent" id="Identifiers"> <div class="nav-panel"> <p> Next: <a href="#Reserved-words" accesskey="n" rel="next">Reserved words</a>, Previous: <a href="#Literal-constants" accesskey="p" rel="prev">Constants</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> [<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> <h4 class="subsection" id="Identifiers-1"><span>10.3.2 Identifiers<a class="copiable-link" href="#Identifiers-1"> ¶</a></span></h4> <a class="index-entry-id" id="index-identifier"></a> <p>Identifiers consist of a sequence of letters, digits, the period (‘<samp class="samp">.</samp>’) 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 class="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 class="code">ls</code> function and that <code class="code">...</code> and <code class="code">..1</code>, <code class="code">..2</code>, etc. are special. </p> <p>Notice also that objects can have names that are not identifiers. These are generally accessed via <code class="code">get</code> and <code class="code">assign</code>, although they can also be represented by text strings in some limited circumstances when there is no ambiguity (e.g. <code class="code">"x" <- 1</code>). As <code class="code">get</code> and <code class="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 class="emph">not</em> equivalent <a class="index-entry-id" id="index-get-1"></a> <a class="index-entry-id" id="index-assign-1"></a> </p> <blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td><code class="code">x$a<-1</code></td><td><code class="code">assign("x$a",1)</code></td></tr> <tr><td><code class="code">x[[1]]</code></td><td><code class="code">get("x[[1]]")</code></td></tr> <tr><td><code class="code">names(x)<-nm</code></td><td><code class="code">assign("names(x)",nm)</code></td></tr> </tbody> </table> </blockquote> <hr> </div> <div class="subsection-level-extent" id="Reserved-words"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Reserved-words-1"><span>10.3.3 Reserved words<a class="copiable-link" href="#Reserved-words-1"> ¶</a></span></h4> <p>The following identifiers have a special meaning and cannot be used for object names </p> <div class="example"> <pre class="example-preformatted">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="r">etc.</span> </pre></div> <hr> </div> <div class="subsection-level-extent" id="Special-operators"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Special-operators-1"><span>10.3.4 Special operators<a class="copiable-link" href="#Special-operators-1"> ¶</a></span></h4> <p>R allows user-defined infix operators. These have the form of a string of characters delimited by the ‘<samp class="samp">%</samp>’ character. The string can contain any printable character except ‘<samp class="samp">%</samp>’. 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-preformatted">%% %*% %/% %in% %o% %x% %||% </pre></div> <hr> </div> <div class="subsection-level-extent" id="Separators"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Separators-1"><span>10.3.5 Separators<a class="copiable-link" href="#Separators-1"> ¶</a></span></h4> <p>Although not strictly tokens, stretches of whitespace characters (spaces, tabs and form feeds, on Windows and UTF-8 locales other Unicode whitespace characters<a class="footnote" id="DOCF5" href="#FOOT5"><sup>5</sup></a>) serve to delimit tokens in case of ambiguity, (compare <code class="code">x<-5</code> and <code class="code">x < -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 (‘<samp class="samp">;</samp>’) may be used to separate elementary <a class="index-entry-id" id="index-expression-2"></a> expressions on the same line. </p> <p>Special rules apply to the <code class="code">else</code> keyword: inside a compound expression, a newline before <code class="code">else</code> is discarded, whereas at the outermost level, the newline terminates the <code class="code">if</code> construction and a subsequent <code class="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 <kbd class="key">RET</kbd>. </p> <p>The comma (‘<samp class="samp">,</samp>’) is used to separate function arguments and multiple indices. </p> <hr> </div> <div class="subsection-level-extent" id="Operator-tokens"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Operator-tokens-1"><span>10.3.6 Operator tokens<a class="copiable-link" href="#Operator-tokens-1"> ¶</a></span></h4> <p>R uses the following operator tokens </p> <blockquote class="quotation"> <table summary="" class="multitable"> <tbody><tr><td width="30%"><code class="code">+ - * / %% %/% ^</code></td><td width="60%">arithmetic</td></tr> <tr><td width="30%"><code class="code">> >= < <= == !=</code></td><td width="60%">relational</td></tr> <tr><td width="30%"><code class="code">! & |</code></td><td width="60%">logical</td></tr> <tr><td width="30%"><code class="code">~</code></td><td width="60%">model formulae</td></tr> <tr><td width="30%"><code class="code">-> <-</code></td><td width="60%">assignment</td></tr> <tr><td width="30%"><code class="code">$</code></td><td width="60%">list indexing</td></tr> <tr><td width="30%"><code class="code">:</code></td><td width="60%">sequence</td></tr> </tbody> </table> </blockquote> <p>(Several of the operators have different meaning inside model formulas) </p> <hr> </div> <div class="subsection-level-extent" id="Grouping"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Grouping-1"><span>10.3.7 Grouping<a class="copiable-link" href="#Grouping-1"> ¶</a></span></h4> <p>Ordinary parentheses—‘<samp class="samp">(</samp>’ and ‘<samp class="samp">)</samp>’—are used for explicit grouping within expressions and to delimit the argument lists for function definitions and function calls. </p> <p>Braces—‘<samp class="samp">{</samp>’ and ‘<samp class="samp">}</samp>’—delimit blocks of expressions in function definitions, conditional expressions, and iterative constructs. </p> <hr> </div> <div class="subsection-level-extent" id="Indexing-tokens"> <div class="nav-panel"> <p> Previous: <a href="#Grouping" accesskey="p" rel="prev">Grouping</a>, Up: <a href="#Tokens" accesskey="u" rel="up">Tokens</a> [<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> <h4 class="subsection" id="Indexing-tokens-1"><span>10.3.8 Indexing tokens<a class="copiable-link" href="#Indexing-tokens-1"> ¶</a></span></h4> <p>Indexing of arrays and vectors is performed using the single and double brackets, ‘<samp class="samp">[]</samp>’ and ‘<samp class="samp">[[]]</samp>’. Also, indexing tagged lists may be done using the ‘<samp class="samp">$</samp>’ operator. </p> <hr> </div> </div> <div class="section-level-extent" id="Expressions"> <div class="nav-panel"> <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> [<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> <h3 class="section" id="Expressions-1"><span>10.4 Expressions<a class="copiable-link" href="#Expressions-1"> ¶</a></span></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> <ul class="mini-toc"> <li><a href="#Function-calls-_0028expressions_0029" accesskey="1">Function calls</a></li> <li><a href="#Infix-and-prefix-operators" accesskey="2">Infix and prefix operators</a></li> <li><a href="#Index-constructions" accesskey="3">Index constructions</a></li> <li><a href="#Compound-expressions" accesskey="4">Compound expressions</a></li> <li><a href="#Flow-control-elements" accesskey="5">Flow control elements</a></li> <li><a href="#Function-definitions" accesskey="6">Function definitions</a></li> </ul> <hr> <div class="subsection-level-extent" id="Function-calls-_0028expressions_0029"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Function-calls-2"><span>10.4.1 Function calls<a class="copiable-link" href="#Function-calls-2"> ¶</a></span></h4> <a class="index-entry-id" id="index-function-29"></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-preformatted"><var class="var">function_reference</var> ( <var class="var">arg1</var>, <var class="var">arg2</var>, ...... , <var class="var">argn</var> ) </pre></div> <p>The function reference can be either </p><ul class="itemize mark-bullet"> <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 class="code"><var class="var">tag</var>=<var class="var">expr</var></code>), or just be a simple expression. It can also be empty or it can be one of the special tokens <code class="code">...</code>, <code class="code">..2</code>, etc. </p> <p>A tag can be an identifier or a text string. </p> <p>Examples: </p> <div class="example"> <pre class="example-preformatted">f(x) g(tag = value, , 5) "odd name"("strange tag" = 5, y) (function(x) x^2)(5) </pre></div> <hr> </div> <div class="subsection-level-extent" id="Infix-and-prefix-operators"> <div class="nav-panel"> <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</a>, Up: <a href="#Expressions" accesskey="u" rel="up">Expressions</a> [<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> <h4 class="subsection" id="Infix-and-prefix-operators-1"><span>10.4.2 Infix and prefix operators<a class="copiable-link" href="#Infix-and-prefix-operators-1"> ¶</a></span></h4> <p>The order of precedence (highest first) of the operators is </p> <div class="example"> <pre class="example-preformatted">:: $ @ ^ - + <span class="r">(unary)</span> : %<var class="var">xyz</var>% |> * / + - <span class="r">(binary)</span> > >= < <= == != ! & && | || ~ <span class="r">(unary and binary)</span> -> ->> <- <<- = <span class="r">(as assignment)</span> </pre></div> <p>Note that <code class="code">:</code> precedes binary +/-, but not <code class="code">^</code>. Hence, <code class="code">1:3-1</code> is <em class="math">0 1 2</em>, but <code class="code">1:2^3</code> is <code class="code">1:8</code>. </p> <p>The exponentiation operator ‘<samp class="samp">^</samp>’ and the <a class="index-entry-id" id="index-assignment-11"></a> left assignment plus minus operators ‘<samp class="samp"><- - = <<-</samp>’ group right to left, all other operators group left to right. That is, <code class="code">2 ^ 2 ^ 3</code> is <em class="math">2 ^ 8</em>, not <em class="math">4 ^ 3</em>, whereas <code class="code">1 - 1 - 1</code> is <em class="math">-1</em>, not 1. </p> <p>Notice that the operators <code class="code">%%</code> and <code class="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 ‘<samp class="samp">=</samp>’ sign is used for tagging arguments in function calls and for assigning default values in function definitions. </p> <p>The ‘<samp class="samp">$</samp>’ sign is in some sense an operator, but does not allow arbitrary right hand sides and is discussed under <a class="ref" 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 class="code">"("</code>, even in cases where the parentheses could be inferred from operator precedence (e.g., <code class="code">a * (b + c)</code>). </p> <p>Notice that the <a class="index-entry-id" id="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 class="code">2 + 2 <- 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> </div> <div class="subsection-level-extent" id="Index-constructions"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Index-constructions-1"><span>10.4.3 Index constructions<a class="copiable-link" href="#Index-constructions-1"> ¶</a></span></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-preformatted"><var class="var">object</var> [ <var class="var">arg1</var>, ...... , <var class="var">argn</var> ] <var class="var">object</var> [[ <var class="var">arg1</var>, ...... , <var class="var">argn</var> ]] </pre></div> <a class="index-entry-id" id="index-_005b-1"></a> <a class="index-entry-id" id="index-_005b_005b-1"></a> <p>The <var class="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 class="code">drop = FALSE</code>). </p> <p>Internally, these index constructs are stored as function calls with function name <code class="code">"["</code> respectively <code class="code">"[["</code>. </p> <p>The third index construction is </p> <div class="example"> <pre class="example-preformatted"><var class="var">object</var> $ <var class="var">tag</var> </pre></div> <a class="index-entry-id" id="index-_0024-1"></a> <p>Here, <var class="var">object</var> is as above, whereas <var class="var">tag</var> is an identifier or a text string. Internally, it is stored as a function call with name <code class="code">"$"</code> </p> <hr> </div> <div class="subsection-level-extent" id="Compound-expressions"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Compound-expressions-1"><span>10.4.4 Compound expressions<a class="copiable-link" href="#Compound-expressions-1"> ¶</a></span></h4> <p>A compound expression is of the form </p> <div class="example"> <pre class="example-preformatted">{ <var class="var">expr1</var> ; <var class="var">expr2</var> ; ...... ; <var class="var">exprn</var> } </pre></div> <p>The semicolons may be replaced by newlines. Internally, this is stored as a function call with <code class="code">"{"</code> as the function name and the expressions as arguments. </p> <hr> </div> <div class="subsection-level-extent" id="Flow-control-elements"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Flow-control-elements-1"><span>10.4.5 Flow control elements<a class="copiable-link" href="#Flow-control-elements-1"> ¶</a></span></h4> <p>R contains the following control structures as special syntactic constructs </p> <div class="example"> <pre class="example-preformatted">if ( <var class="var">cond</var> ) <var class="var">expr</var> if ( <var class="var">cond</var> ) <var class="var">expr1</var> else <var class="var">expr2</var> while ( <var class="var">cond</var> ) <var class="var">expr</var> repeat <var class="var">expr</var> for ( <var class="var">var</var> in <var class="var">list</var> ) <var class="var">expr</var> </pre></div> <p>The expressions in these constructs will typically be compound expressions. </p> <p>Within the loop constructs (<code class="code">while</code>, <code class="code">repeat</code>, <code class="code">for</code>), one may use <code class="code">break</code> (to terminate the loop) and <code class="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-preformatted">"if"(<var class="var">cond</var>, <var class="var">expr</var>) "if"(<var class="var">cond</var>, <var class="var">expr1</var>, <var class="var">expr2</var>) "while"(<var class="var">cond</var>, <var class="var">expr</var>) "repeat"(<var class="var">expr</var>) "for"(<var class="var">var</var>, <var class="var">list</var>, <var class="var">expr</var>) "break"() "next"() </pre></div> <hr> </div> <div class="subsection-level-extent" id="Function-definitions"> <div class="nav-panel"> <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> [<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> <h4 class="subsection" id="Function-definitions-1"><span>10.4.6 Function definitions<a class="copiable-link" href="#Function-definitions-1"> ¶</a></span></h4> <p>A <a class="index-entry-id" id="index-function-30"></a> function definition is of the form </p> <div class="example"> <pre class="example-preformatted">function ( <var class="var">arglist</var> ) <var class="var">body</var> </pre></div> <p>The function body is an expression, often a compound expression. The <var class="var">arglist</var> is a comma-separated list of items each of which can be an identifier, or of the form ‘<samp class="samp"><var class="var">identifier</var> = <var class="var">default</var></samp>’, or the special token <code class="code">...</code>. The <var class="var">default</var> can be any valid expression. </p> <p>Notice that function arguments unlike list tags, etc., cannot have “strange names” given as text strings. </p> <p>Internally, a function definition is stored as a function call with function name <code class="code">function</code> and two arguments, the <var class="var">arglist</var> and the <var class="var">body</var>. The <var class="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> </div> </div> <div class="section-level-extent" id="Directives"> <div class="nav-panel"> <p> Previous: <a href="#Expressions" accesskey="p" rel="prev">Expressions</a>, Up: <a href="#Parser" accesskey="u" rel="up">Parser</a> [<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> <h3 class="section" id="Directives-1"><span>10.5 Directives<a class="copiable-link" href="#Directives-1"> ¶</a></span></h3> <a class="index-entry-id" id="index-_0023line"></a> <p>The parser currently only supports one directive, <code class="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-preformatted"><var class="var">#line</var> <var class="var">nn</var> [ <code class="code">"filename"</code> ] </pre></div> <p>where <var class="var">nn</var> is an integer line number, and the optional <var class="var">filename</var> (in required double quotes) names the source file. </p> <p>Unlike the C directive, <code class="code">#line</code> must appear as the first five characters on a line. As in C, <var class="var">nn</var> and <code class="code">"filename"</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 class="var">nn</var> of file <var class="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> </div> </div> <div class="unnumbered-level-extent" id="Function-and-Variable-Index"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="unnumbered" id="Function-and-Variable-Index-1"><span>Function and Variable Index<a class="copiable-link" href="#Function-and-Variable-Index-1"> ¶</a></span></h2> <div class="printindex vr-printindex"> <table summary="" class="vr-letters-header-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-1"><b>.</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-2"><b>[</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-3"><b>#</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-4"><b>$</b></a> <br> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-A"><b>A</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-B"><b>B</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-D"><b>D</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-E"><b>E</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-F"><b>F</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-G"><b>G</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-I"><b>I</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-M"><b>M</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-N"><b>N</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-O"><b>O</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-P"><b>P</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-Q"><b>Q</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-R"><b>R</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-S"><b>S</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-T"><b>T</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-U"><b>U</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-W"><b>W</b></a> </td></tr></table> <table summary="" class="vr-entries-printindex" border="0"> <tr><td></td><th class="entries-header-printindex">Index Entry</th><th class="sections-header-printindex">Section</th></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_symbol-1">.</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eC"><code>.C</code></a></td><td class="printindex-index-section"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eCall"><code>.Call</code></a></td><td class="printindex-index-section"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eExternal"><code>.External</code></a></td><td class="printindex-index-section"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eFortran"><code>.Fortran</code></a></td><td class="printindex-index-section"><a href="#Foreign-language-interfaces">Foreign language interfaces</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eInternal-1"><code>.Internal</code></a></td><td class="printindex-index-section"><a href="#g_t_002eInternal-and-_002ePrimitive">.Internal and .Primitive</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002ePrimitive-1"><code>.Primitive</code></a></td><td class="printindex-index-section"><a href="#g_t_002eInternal-and-_002ePrimitive">.Internal and .Primitive</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_symbol-2">[</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_005b"><code>[</code></a></td><td class="printindex-index-section"><a href="#Indexing">Indexing</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_005b-1"><code>[</code></a></td><td class="printindex-index-section"><a href="#Index-constructions">Index constructions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_005b_005b"><code>[[</code></a></td><td class="printindex-index-section"><a href="#Indexing">Indexing</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_005b_005b-1"><code>[[</code></a></td><td class="printindex-index-section"><a href="#Index-constructions">Index constructions</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_symbol-3">#</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_0023"><code>#</code></a></td><td class="printindex-index-section"><a href="#Comments">Comments</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_symbol-4">$</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_0024"><code>$</code></a></td><td class="printindex-index-section"><a href="#Indexing">Indexing</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_0024-1"><code>$</code></a></td><td class="printindex-index-section"><a href="#Index-constructions">Index constructions</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-A">A</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-as_002ecall"><code>as.call</code></a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-as_002echaracter"><code>as.character</code></a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-as_002efunction"><code>as.function</code></a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-as_002elist"><code>as.list</code></a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-as_002ename"><code>as.name</code></a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assign"><code>assign</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assign-1"><code>assign</code></a></td><td class="printindex-index-section"><a href="#Identifiers">Identifiers</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-attr"><code>attr</code></a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-attr_003c_002d"><code>attr<-</code></a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-attributes-1"><code>attributes</code></a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-attributes_003c_002d"><code>attributes<-</code></a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-B">B</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-baseenv"><code>baseenv</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-basename"><code>basename</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-body"><code>body</code></a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-body-1"><code>body</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-body_003c_002d"><code>body<-</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-break"><code>break</code></a></td><td class="printindex-index-section"><a href="#Looping">Looping</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-browser"><code>browser</code></a></td><td class="printindex-index-section"><a href="#browser">browser</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-D">D</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-debug"><code>debug</code></a></td><td class="printindex-index-section"><a href="#debug_002fundebug">debug/undebug</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-dirname"><code>dirname</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-do_002ecall"><code>do.call</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-E">E</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-emptyenv"><code>emptyenv</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-20"><code>environment</code></a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-21"><code>environment</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment_003c_002d"><code>environment<-</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-eval"><code>eval</code></a></td><td class="printindex-index-section"><a href="#More-on-evaluation">More on evaluation</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-F">F</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002eaccess"><code>file.access</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002eappend"><code>file.append</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002echoose"><code>file.choose</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002ecopy"><code>file.copy</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002ecreate"><code>file.create</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002eexists"><code>file.exists</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002einfo"><code>file.info</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002epath"><code>file.path</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002eremove"><code>file.remove</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002erename"><code>file.rename</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-file_002eshow"><code>file.show</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-for"><code>for</code></a></td><td class="printindex-index-section"><a href="#for">for</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-formals"><code>formals</code></a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-formals-1"><code>formals</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-formals_003c_002d"><code>formals<-</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-G">G</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-get"><code>get</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-get-1"><code>get</code></a></td><td class="printindex-index-section"><a href="#Identifiers">Identifiers</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-get0"><code>get0</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-I">I</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-is_002ena"><code>is.na</code></a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-is_002enan"><code>is.nan</code></a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-M">M</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-match_002earg"><code>match.arg</code></a></td><td class="printindex-index-section"><a href="#Argument-matching">Argument matching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-match_002ecall"><code>match.call</code></a></td><td class="printindex-index-section"><a href="#Argument-matching">Argument matching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-match_002ecall-1"><code>match.call</code></a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-match_002efun"><code>match.fun</code></a></td><td class="printindex-index-section"><a href="#Argument-matching">Argument matching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-missing"><code>missing</code></a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-mode-3"><code>mode</code></a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-N">N</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-NA"><code>NA</code></a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-NA-1"><code>NA</code></a></td><td class="printindex-index-section"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-names"><code>names</code></a></td><td class="printindex-index-section"><a href="#Names">Names</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-names_003c_002d"><code>names<-</code></a></td><td class="printindex-index-section"><a href="#Names">Names</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-NaN"><code>NaN</code></a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-new_002eenv"><code>new.env</code></a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-next"><code>next</code></a></td><td class="printindex-index-section"><a href="#Looping">Looping</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-NextMethod"><code>NextMethod</code></a></td><td class="printindex-index-section"><a href="#NextMethod">NextMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-NULL"><code>NULL</code></a></td><td class="printindex-index-section"><a href="#NULL-object">NULL object</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-O">O</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-on_002eexit"><code>on.exit</code></a></td><td class="printindex-index-section"><a href="#on_002eexit">on.exit</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-P">P</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-pairlist-1"><code>pairlist</code></a></td><td class="printindex-index-section"><a href="#Pairlist-objects">Pairlist objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-path_002eexpand"><code>path.expand</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-proc_002etime"><code>proc.time</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-Q">Q</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-quote"><code>quote</code></a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-R">R</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-repeat"><code>repeat</code></a></td><td class="printindex-index-section"><a href="#repeat">repeat</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-S">S</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-stop"><code>stop</code></a></td><td class="printindex-index-section"><a href="#stop">stop</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-storage_002emode"><code>storage.mode</code></a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-substitute"><code>substitute</code></a></td><td class="printindex-index-section"><a href="#Substitutions">Substitutions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-switch"><code>switch</code></a></td><td class="printindex-index-section"><a href="#switch">switch</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002egetenv"><code>Sys.getenv</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002egetlocale"><code>Sys.getlocale</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002elocaleconv"><code>Sys.localeconv</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002eputenv"><code>Sys.putenv</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002eputlocale"><code>Sys.putlocale</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002etime"><code>Sys.time</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-Sys_002etimezone"><code>Sys.timezone</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-system"><code>system</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-system_002etime"><code>system.time</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-T">T</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-trace"><code>trace</code></a></td><td class="printindex-index-section"><a href="#trace_002funtrace">trace/untrace</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-traceback"><code>traceback</code></a></td><td class="printindex-index-section"><a href="#traceback">traceback</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-typeof"><code>typeof</code></a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-U">U</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-undebug"><code>undebug</code></a></td><td class="printindex-index-section"><a href="#debug_002fundebug">debug/undebug</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-unlink"><code>unlink</code></a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-untrace"><code>untrace</code></a></td><td class="printindex-index-section"><a href="#trace_002funtrace">trace/untrace</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-UseMethod"><code>UseMethod</code></a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Function-and-Variable-Index_vr_letter-W">W</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-warning"><code>warning</code></a></td><td class="printindex-index-section"><a href="#warning">warning</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-warnings"><code>warnings</code></a></td><td class="printindex-index-section"><a href="#warning">warning</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-while"><code>while</code></a></td><td class="printindex-index-section"><a href="#while">while</a></td></tr> <tr><td colspan="3"><hr></td></tr> </table> <table summary="" class="vr-letters-footer-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-1"><b>.</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-2"><b>[</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-3"><b>#</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_symbol-4"><b>$</b></a> <br> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-A"><b>A</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-B"><b>B</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-D"><b>D</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-E"><b>E</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-F"><b>F</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-G"><b>G</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-I"><b>I</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-M"><b>M</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-N"><b>N</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-O"><b>O</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-P"><b>P</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-Q"><b>Q</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-R"><b>R</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-S"><b>S</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-T"><b>T</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-U"><b>U</b></a> <a class="summary-letter-printindex" href="#Function-and-Variable-Index_vr_letter-W"><b>W</b></a> </td></tr></table> </div> <hr> </div> <div class="unnumbered-level-extent" id="Concept-Index"> <div class="nav-panel"> <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">R Language Definition</a> [<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> <h2 class="unnumbered" id="Concept-Index-1"><span>Concept Index<a class="copiable-link" href="#Concept-Index-1"> ¶</a></span></h2> <div class="printindex cp-printindex"> <table summary="" class="cp-letters-header-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-1"><b>.</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-2"><b>#</b></a> <br> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-A"><b>A</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-B"><b>B</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-C"><b>C</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-E"><b>E</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-F"><b>F</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-I"><b>I</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-M"><b>M</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-N"><b>N</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-O"><b>O</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-P"><b>P</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-S"><b>S</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-T"><b>T</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-V"><b>V</b></a> </td></tr></table> <table summary="" class="cp-entries-printindex" border="0"> <tr><td></td><th class="entries-header-printindex">Index Entry</th><th class="sections-header-printindex">Section</th></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_symbol-1">.</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002eInternal">.Internal</a></td><td class="printindex-index-section"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_002ePrimitive">.Primitive</a></td><td class="printindex-index-section"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_symbol-2">#</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-_0023line">#line</a></td><td class="printindex-index-section"><a href="#Directives">Directives</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-A">A</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-argument">argument</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-argument-1">argument</a></td><td class="printindex-index-section"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-argument_002c-default-values">argument, default values</a></td><td class="printindex-index-section"><a href="#Arguments">Arguments</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment">assignment</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-1">assignment</a></td><td class="printindex-index-section"><a href="#Function-calls">Function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-2">assignment</a></td><td class="printindex-index-section"><a href="#Operators">Operators</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-3">assignment</a></td><td class="printindex-index-section"><a href="#Subset-assignment">Subset assignment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-4">assignment</a></td><td class="printindex-index-section"><a href="#Global-environment">Global environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-5">assignment</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-6">assignment</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-7">assignment</a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-8">assignment</a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-9">assignment</a></td><td class="printindex-index-section"><a href="#More-on-evaluation">More on evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-10">assignment</a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-11">assignment</a></td><td class="printindex-index-section"><a href="#Infix-and-prefix-operators">Infix and prefix operators</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-assignment-12">assignment</a></td><td class="printindex-index-section"><a href="#Infix-and-prefix-operators">Infix and prefix operators</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-atomic">atomic</a></td><td class="printindex-index-section"><a href="#Vector-objects">Vector objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-attributes">attributes</a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-B">B</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-binding">binding</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-binding-1">binding</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-C">C</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-call">call</a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-call-stack">call stack</a></td><td class="printindex-index-section"><a href="#Stacks">Stacks</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-coercion">coercion</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-coercion-1">coercion</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-coercion-2">coercion</a></td><td class="printindex-index-section"><a href="#Any_002dtype">Any-type</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-coercion-3">coercion</a></td><td class="printindex-index-section"><a href="#Classes">Classes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-coercion-4">coercion</a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-comments">comments</a></td><td class="printindex-index-section"><a href="#Comments">Comments</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-complex-assignment">complex assignment</a></td><td class="printindex-index-section"><a href="#Subset-assignment">Subset assignment</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-E">E</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment">environment</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-1">environment</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-2">environment</a></td><td class="printindex-index-section"><a href="#Promise-objects">Promise objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-3">environment</a></td><td class="printindex-index-section"><a href="#Environment-objects">Environment objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-4">environment</a></td><td class="printindex-index-section"><a href="#Control-structures">Control structures</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-5">environment</a></td><td class="printindex-index-section"><a href="#Global-environment">Global environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-6">environment</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-7">environment</a></td><td class="printindex-index-section"><a href="#Stacks">Stacks</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-8">environment</a></td><td class="printindex-index-section"><a href="#Search-path">Search path</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-9">environment</a></td><td class="printindex-index-section"><a href="#Evaluation-environment">Evaluation environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-10">environment</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-11">environment</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-12">environment</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-13">environment</a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-14">environment</a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-15">environment</a></td><td class="printindex-index-section"><a href="#More-on-evaluation">More on evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-16">environment</a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-17">environment</a></td><td class="printindex-index-section"><a href="#Operating-system-access">Operating system access</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-18">environment</a></td><td class="printindex-index-section"><a href="#Debugging">Debugging</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment-19">environment</a></td><td class="printindex-index-section"><a href="#Debugging">Debugging</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment_002c-evaluation">environment, evaluation</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment_002c-evaluation-1">environment, evaluation</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-environment_002c-evaluation-2">environment, evaluation</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation">evaluation</a></td><td class="printindex-index-section"><a href="#Stacks">Stacks</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-1">evaluation</a></td><td class="printindex-index-section"><a href="#Evaluation-environment">Evaluation environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-2">evaluation</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-3">evaluation</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-4">evaluation</a></td><td class="printindex-index-section"><a href="#Inheritance">Inheritance</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-5">evaluation</a></td><td class="printindex-index-section"><a href="#UseMethod">UseMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-6">evaluation</a></td><td class="printindex-index-section"><a href="#More-on-evaluation">More on evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation-7">evaluation</a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-argument">evaluation, argument</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-expression">evaluation, expression</a></td><td class="printindex-index-section"><a href="#Expression-objects">Expression objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-expression-1">evaluation, expression</a></td><td class="printindex-index-section"><a href="#Promise-objects">Promise objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-expression-2">evaluation, expression</a></td><td class="printindex-index-section"><a href="#Arguments">Arguments</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-lazy">evaluation, lazy</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-lazy-1">evaluation, lazy</a></td><td class="printindex-index-section"><a href="#Substitutions">Substitutions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-lazy-2">evaluation, lazy</a></td><td class="printindex-index-section"><a href="#Substitutions">Substitutions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-statement">evaluation, statement</a></td><td class="printindex-index-section"><a href="#Control-structures">Control structures</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-symbol">evaluation, symbol</a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-symbol-1">evaluation, symbol</a></td><td class="printindex-index-section"><a href="#Symbol-lookup">Symbol lookup</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-evaluation_002c-symbol-2">evaluation, symbol</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-expression">expression</a></td><td class="printindex-index-section"><a href="#Introduction">Introduction</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-expression-1">expression</a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-expression-2">expression</a></td><td class="printindex-index-section"><a href="#Separators">Separators</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-expression-object">expression object</a></td><td class="printindex-index-section"><a href="#Expression-objects">Expression objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-expression-object-1">expression object</a></td><td class="printindex-index-section"><a href="#Expression-objects">Expression objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-F">F</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-frame">frame</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function">function</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-1">function</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-2">function</a></td><td class="printindex-index-section"><a href="#Function-objects">Function objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-3">function</a></td><td class="printindex-index-section"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-4">function</a></td><td class="printindex-index-section"><a href="#Builtin-objects-and-special-forms">Builtin objects and special forms</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-5">function</a></td><td class="printindex-index-section"><a href="#Promise-objects">Promise objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-6">function</a></td><td class="printindex-index-section"><a href="#Dot_002ddot_002ddot">Dot-dot-dot</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-7">function</a></td><td class="printindex-index-section"><a href="#Function-calls">Function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-8">function</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-9">function</a></td><td class="printindex-index-section"><a href="#Lexical-environment">Lexical environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-10">function</a></td><td class="printindex-index-section"><a href="#Stacks">Stacks</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-11">function</a></td><td class="printindex-index-section"><a href="#Writing-functions">Writing functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-12">function</a></td><td class="printindex-index-section"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-13">function</a></td><td class="printindex-index-section"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-14">function</a></td><td class="printindex-index-section"><a href="#Arguments">Arguments</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-15">function</a></td><td class="printindex-index-section"><a href="#Evaluation-environment">Evaluation environment</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-16">function</a></td><td class="printindex-index-section"><a href="#Argument-matching">Argument matching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-17">function</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-18">function</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-19">function</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-20">function</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-21">function</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-22">function</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-23">function</a></td><td class="printindex-index-section"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-24">function</a></td><td class="printindex-index-section"><a href="#Definition">Definition</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-25">function</a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-26">function</a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-27">function</a></td><td class="printindex-index-section"><a href="#Manipulation-of-functions">Manipulation of functions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-28">function</a></td><td class="printindex-index-section"><a href="#Internal-representation">Internal representation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-29">function</a></td><td class="printindex-index-section"><a href="#Function-calls-_0028expressions_0029">Function calls (expressions)</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-30">function</a></td><td class="printindex-index-section"><a href="#Function-definitions">Function definitions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-argument">function argument</a></td><td class="printindex-index-section"><a href="#Promise-objects">Promise objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-argument-1">function argument</a></td><td class="printindex-index-section"><a href="#Dot_002ddot_002ddot">Dot-dot-dot</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-arguments">function arguments</a></td><td class="printindex-index-section"><a href="#Function-calls">Function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function-invocation">function invocation</a></td><td class="printindex-index-section"><a href="#Function-calls">Function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-accessor">function, accessor</a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-anonymous">function, anonymous</a></td><td class="printindex-index-section"><a href="#Syntax-and-examples">Syntax and examples</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-assignment">function, assignment</a></td><td class="printindex-index-section"><a href="#Function-calls">Function calls</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic">function, generic</a></td><td class="printindex-index-section"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-1">function, generic</a></td><td class="printindex-index-section"><a href="#Definition">Definition</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-2">function, generic</a></td><td class="printindex-index-section"><a href="#Definition">Definition</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-3">function, generic</a></td><td class="printindex-index-section"><a href="#Definition">Definition</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-4">function, generic</a></td><td class="printindex-index-section"><a href="#Inheritance">Inheritance</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-5">function, generic</a></td><td class="printindex-index-section"><a href="#Method-dispatching">Method dispatching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-6">function, generic</a></td><td class="printindex-index-section"><a href="#Writing-methods">Writing methods</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-generic-7">function, generic</a></td><td class="printindex-index-section"><a href="#Writing-methods">Writing methods</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-internal">function, internal</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-internal-1">function, internal</a></td><td class="printindex-index-section"><a href="#Group-methods">Group methods</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-function_002c-modeling">function, modeling</a></td><td class="printindex-index-section"><a href="#Factors">Factors</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-I">I</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-identifier">identifier</a></td><td class="printindex-index-section"><a href="#Identifiers">Identifiers</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index">index</a></td><td class="printindex-index-section"><a href="#Vector-objects">Vector objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index-1">index</a></td><td class="printindex-index-section"><a href="#List-objects">List objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index-2">index</a></td><td class="printindex-index-section"><a href="#Indexing">Indexing</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index-3">index</a></td><td class="printindex-index-section"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index-4">index</a></td><td class="printindex-index-section"><a href="#Indexing-matrices-and-arrays">Indexing matrices and arrays</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-index-5">index</a></td><td class="printindex-index-section"><a href="#Indexing-matrices-and-arrays">Indexing matrices and arrays</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-M">M</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-mode">mode</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-mode-1">mode</a></td><td class="printindex-index-section"><a href="#Vector-objects">Vector objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-mode-2">mode</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-modeling-function">modeling function</a></td><td class="printindex-index-section"><a href="#Factors">Factors</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-N">N</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name">name</a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-1">name</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-2">name</a></td><td class="printindex-index-section"><a href="#Symbol-lookup">Symbol lookup</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-3">name</a></td><td class="printindex-index-section"><a href="#Propagation-of-names">Propagation of names</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-4">name</a></td><td class="printindex-index-section"><a href="#Scope-of-variables">Scope of variables</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-5">name</a></td><td class="printindex-index-section"><a href="#Arguments">Arguments</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-6">name</a></td><td class="printindex-index-section"><a href="#Argument-matching">Argument matching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-7">name</a></td><td class="printindex-index-section"><a href="#Argument-evaluation">Argument evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-8">name</a></td><td class="printindex-index-section"><a href="#Method-dispatching">Method dispatching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-9">name</a></td><td class="printindex-index-section"><a href="#NextMethod">NextMethod</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-10">name</a></td><td class="printindex-index-section"><a href="#Direct-manipulation-of-language-objects">Direct manipulation of language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-name-11">name</a></td><td class="printindex-index-section"><a href="#Debugging">Debugging</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-namespace">namespace</a></td><td class="printindex-index-section"><a href="#Search-path">Search path</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-O">O</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object">object</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object-1">object</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object-2">object</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object-3">object</a></td><td class="printindex-index-section"><a href="#Attributes">Attributes</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object-4">object</a></td><td class="printindex-index-section"><a href="#Method-dispatching">Method dispatching</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object_002doriented">object-oriented</a></td><td class="printindex-index-section"><a href="#Object_002doriented-programming">Object-oriented programming</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-object_002doriented-1">object-oriented</a></td><td class="printindex-index-section"><a href="#Definition">Definition</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-P">P</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-pairlist">pairlist</a></td><td class="printindex-index-section"><a href="#Pairlist-objects">Pairlist objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing">parsing</a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-1">parsing</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-2">parsing</a></td><td class="printindex-index-section"><a href="#Evaluation-of-expressions">Evaluation of expressions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-3">parsing</a></td><td class="printindex-index-section"><a href="#Computing-on-the-language">Computing on the language</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-4">parsing</a></td><td class="printindex-index-section"><a href="#Direct-manipulation-of-language-objects">Direct manipulation of language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-5">parsing</a></td><td class="printindex-index-section"><a href="#Substitutions">Substitutions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-6">parsing</a></td><td class="printindex-index-section"><a href="#Parser">Parser</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-parsing-7">parsing</a></td><td class="printindex-index-section"><a href="#Internal-representation">Internal representation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-partial-matching">partial matching</a></td><td class="printindex-index-section"><a href="#Indexing-by-vectors">Indexing by vectors</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-promise">promise</a></td><td class="printindex-index-section"><a href="#Promise-objects">Promise objects</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-S">S</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope">scope</a></td><td class="printindex-index-section"><a href="#Scope-of-variables">Scope of variables</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope-1">scope</a></td><td class="printindex-index-section"><a href="#Stacks">Stacks</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope-2">scope</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope-3">scope</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope-4">scope</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-scope-5">scope</a></td><td class="printindex-index-section"><a href="#More-on-evaluation">More on evaluation</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-search-path">search path</a></td><td class="printindex-index-section"><a href="#Search-path">Search path</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-statement">statement</a></td><td class="printindex-index-section"><a href="#Language-objects">Language objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol">symbol</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol-1">symbol</a></td><td class="printindex-index-section"><a href="#Symbol-objects">Symbol objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol-2">symbol</a></td><td class="printindex-index-section"><a href="#Symbol-lookup">Symbol lookup</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol-3">symbol</a></td><td class="printindex-index-section"><a href="#Scope">Scope</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol-4">symbol</a></td><td class="printindex-index-section"><a href="#Substitutions">Substitutions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-symbol-5">symbol</a></td><td class="printindex-index-section"><a href="#Manipulation-of-function-calls">Manipulation of function calls</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-T">T</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-token">token</a></td><td class="printindex-index-section"><a href="#Expression-objects">Expression objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type">type</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type-1">type</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type-2">type</a></td><td class="printindex-index-section"><a href="#Basic-types">Basic types</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type-3">type</a></td><td class="printindex-index-section"><a href="#Vector-objects">Vector objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type-4">type</a></td><td class="printindex-index-section"><a href="#Names">Names</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-type-5">type</a></td><td class="printindex-index-section"><a href="#NA-handling">NA handling</a></td></tr> <tr><td colspan="3"><hr></td></tr> <tr><th id="Concept-Index_cp_letter-V">V</th></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-value">value</a></td><td class="printindex-index-section"><a href="#Symbol-lookup">Symbol lookup</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-variable">variable</a></td><td class="printindex-index-section"><a href="#Objects">Objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-vector">vector</a></td><td class="printindex-index-section"><a href="#Vector-objects">Vector objects</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-vector-1">vector</a></td><td class="printindex-index-section"><a href="#Dimensions">Dimensions</a></td></tr> <tr><td></td><td class="printindex-index-entry"><a href="#index-vector-2">vector</a></td><td class="printindex-index-section"><a href="#Operators">Operators</a></td></tr> <tr><td colspan="3"><hr></td></tr> </table> <table summary="" class="cp-letters-footer-printindex"><tr><th>Jump to: </th><td><a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-1"><b>.</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_symbol-2"><b>#</b></a> <br> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-A"><b>A</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-B"><b>B</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-C"><b>C</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-E"><b>E</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-F"><b>F</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-I"><b>I</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-M"><b>M</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-N"><b>N</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-O"><b>O</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-P"><b>P</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-S"><b>S</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-T"><b>T</b></a> <a class="summary-letter-printindex" href="#Concept-Index_cp_letter-V"><b>V</b></a> </td></tr></table> </div> <hr> </div> <div class="appendix-level-extent" id="References"> <div class="nav-panel"> <p> Previous: <a href="#Concept-Index" accesskey="p" rel="prev">Concept Index</a>, Up: <a href="#Top" accesskey="u" rel="up">R Language Definition</a> [<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> <h2 class="appendix" id="References-1"><span>Appendix A References<a class="copiable-link" href="#References-1"> ¶</a></span></h2> <p>Richard A. Becker, John M. Chambers and Allan R. Wilks (1988), <em class="emph">The New S Language.</em> Chapman & Hall, New York. This book is often called the “<em class="emph">Blue Book</em>”. </p> </div> </div> <div class="footnotes-segment"> <hr> <h4 class="footnotes-heading">Footnotes</h4> <h5 class="footnote-body-heading"><a id="FOOT1" href="#DOCF1">(1)</a></h5> <p>Note that, internally, character vectors are of type <code class="code">STRSXP</code> and each element is of type <code class="code">CHARSXP</code> (with <em class="emph">internal only</em> <code class="code">typeof(.) == "char"</code>, see <a class="pxref" href="#typeof_002dchar">typeof-char</a>). Thus, an R <em class="emph">string</em> is internally a <code class="code">STRSXP</code> with one element, a <code class="code">CHARSXP</code>.</p> <h5 class="footnote-body-heading"><a id="FOOT2" href="#DOCF2">(2)</a></h5> <p>actually two, but this draft manual predates the <strong class="strong">methods</strong> package.</p> <h5 class="footnote-body-heading"><a id="FOOT3" href="#DOCF3">(3)</a></h5> <p>Evaluation always takes place in an <a class="index-entry-id" id="index-environment-4"></a> environment. See <a class="ref" href="#Scope-of-variables">Scope of variables</a> for more details.</p> <h5 class="footnote-body-heading"><a id="FOOT4" href="#DOCF4">(4)</a></h5> <p>Looping is the repeated evaluation of a statement or block of statements.</p> <h5 class="footnote-body-heading"><a id="FOOT5" href="#DOCF5">(5)</a></h5> <p>such as <code class="code">U+A0</code>, non-breaking space, and <code class="code">U+3000</code>, ideographic space.</p> </div> </body> </html>