forked from hadley/adv-r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Environments.tex
1407 lines (1187 loc) · 53.4 KB
/
Environments.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Modified from pandoc --print-default-template=latex
\documentclass[oneside]{book}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{amssymb,amsmath}
\usepackage{ifxetex,ifluatex}
\usepackage{fixltx2e} % provides \textsubscript
\usepackage{upquote}
\usepackage{mathspec}
\usepackage{xltxtra,xunicode}
\defaultfontfeatures{Mapping=tex-text,Scale=MatchLowercase}
\setmonofont[Mapping=tex-ansi]{Inconsolata}
% Taken from pandoc x.md -o test.tex --standalone
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
\newenvironment{Shaded}{}{}
\newcommand{\KeywordTok} [1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.56,0.13,0.00}{{#1}}}
\newcommand{\DecValTok} [1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\BaseNTok} [1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\FloatTok} [1]{\textcolor[rgb]{0.25,0.63,0.44}{{#1}}}
\newcommand{\CharTok} [1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\StringTok} [1]{\textcolor[rgb]{0.25,0.44,0.63}{{#1}}}
\newcommand{\CommentTok} [1]{\textcolor[rgb]{0.38,0.63,0.69}{{#1}}}
\newcommand{\OtherTok} [1]{\textcolor[rgb]{0.00,0.44,0.13}{{#1}}}
\newcommand{\AlertTok} [1]{\textcolor[rgb]{1.00,0.00,0.00}{{#1}}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.02,0.16,0.49}{{#1}}}
\newcommand{\ErrorTok} [1]{\textcolor[rgb]{1.00,0.00,0.00}{{#1}}}
\newcommand{\NormalTok} [1]{{#1}}
\usepackage{longtable}
\usepackage{booktabs}
\usepackage{graphicx}
\usepackage[hyphens]{url}
\usepackage[setpagesize=false, % page size defined by xetex
unicode=false, % unicode breaks when used with xetex
xetex]{hyperref}
\hypersetup{breaklinks=true,
bookmarks=true,
pdfauthor={},
pdftitle={Environments},
colorlinks=true,
citecolor=blue,
urlcolor=[rgb]{0.3,0.3,0.3},
linkcolor=[rgb]{0.3,0.3,0.3},
pdfborder={0 0 0}}
% Place links as footnotes
\renewcommand{\href}[2]{#2 (\url{#1})}
% Use ref for internal links
\renewcommand{\hyperref}[2][???]{\autoref{#1}}
\def\chapterautorefname{Chapter}
\def\sectionautorefname{Section}
\def\subsectionautorefname{Section}
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\title{Environments}
\author{}
\date{}
\begin{document}
\section{Environments}\label{environments}
\subsection{Introduction}\label{introduction}
The environment is the data structure that powers scoping in R. An
environment binds names to values, and understanding how they work will
help you understand scoping more deeply. Environments are also useful
because they have reference semantics.
The binding between names and values are usually created with
\texttt{\textless{}-} or \texttt{assign()}. Other functions, like
\texttt{delayedBinding()} and \texttt{activeBinding()}, allow you to
create special bindings that work in a different way. These are rarely
useful, but are fun to learn about and play with.
\subparagraph{Outline}\label{outline}
\subparagraph{Prerequisities}\label{prerequisities}
This chapter uses many functions from the \texttt{pryr} package to pry
open R and look inside at the messy details. You can install
\texttt{pryr} by running
\texttt{devtools::install\_github("hadley/pryr")}
\subsection{Environment basics}\label{environment-basics}
The job of an environment is to associate, or \textbf{bind}, a set of
names to a set of values. You can think of an environment as a bag of
names:
\includegraphics[width=1.18in,height=1.18in]{diagrams/environments.png/bag-of-names.png}
Each name points to an object stored elsewhere in memory:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\NormalTok{e$a <-}\StringTok{ }\OtherTok{FALSE}
\NormalTok{e$b <-}\StringTok{ "a"}
\NormalTok{e$c <-}\StringTok{ }\FloatTok{2.3}
\NormalTok{e$d <-}\StringTok{ }\DecValTok{1}\NormalTok{:}\DecValTok{3}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.36in,height=1.47in]{diagrams/environments.png/bindings.png}
The objects don't live in the environment so multiple names can point to
the same object:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e$a <-}\StringTok{ }\NormalTok{e$d}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.36in,height=1.47in]{diagrams/environments.png/multiple-names.png}
Confusingly they can also point to different objects that have the same
value:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e$a <-}\StringTok{ }\DecValTok{1}\NormalTok{:}\DecValTok{3}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.36in,height=1.47in]{diagrams/environments.png/copies.png}
If an object has no names pointing to it, it gets automatically deleted
by the garbage collector. This process is described in more detail in
\hyperref[gc]{gc}.
Every environment also has a parent, another environment. In diagrams,
I'll represent the parent with a small black circle. The parent is used
to implement lexical scoping: if a name is not found in an environment,
then R will look in its parent (and so on). Only one environment doesn't
have a parent: the \textbf{empty} environment.
\includegraphics[width=2.36in,height=1.47in]{diagrams/environments.png/parents.png}
We use the metaphor of a family to refer to environments. The
grandparent of a environment is the parent's parent, and the ancestors
include parent environments all the way up to the empty environment.
It's rare to talk about the children of an environment because there are
no back links: given an environment we have no way to find its children.
Generally, an environment is similar to a list, with four important
exceptions:
\begin{itemize}
\item
Every object in an environment has a unique name.
\item
The objects in an environment are not ordered (i.e.~it doesn't make
sense to ask what the first object in an environment is).
\item
An environment has a parent.
\item
Environments have reference semantics. When you modify a binding in an
environment, the environment is not copied; it's modified in place.
\hyperref[explicit-environments]{Explicit environments} discusses the
implications.
\end{itemize}
More technically, an environment is made up of two components, the
\textbf{frame}, which contains the name-object bindings (and behaves
much like a named list), and the parent environment. Unfortunately
``frame'' is used inconsistently in R. For example,
\texttt{parent.frame()} doesn't give you the parent frame of an
environment, it gives you the \emph{calling} environment. This is
discussed in more details in \hyperref[calling-environments]{calling
environments}.
There are four special environments:
\begin{itemize}
\item
\texttt{globalenv()}: the interactive workspace. This is the
environment in which your normally work. The parent of the global
environment is the last package that you attached with
\texttt{library()}.
\item
\texttt{baseenv()}: the environment of the base package. The parent of
the base environment is the empty environment.
\item
\texttt{emptyenv()}: the ultimate ancestor of all environments, and
the only environment without a parent.
\item
\texttt{environment()}: the current environment.
\end{itemize}
\texttt{search()} lists all parents of the global environment. This is
called the search path because objects in these environments can be
found from the top-level interactive workspace. It contains an
environment for each attached package and any other objects that you've
\texttt{attach()}ed. It also contains a special environment called
\texttt{Autoloads} which is used to save memory by only loading package
objects (like big datasets) when needed.
You can access the environments of any environment on the search list
using \texttt{as.environment()}.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{search}\NormalTok{()}
\CommentTok{#> [1] ".GlobalEnv" "package:bookdown" "package:stats" }
\CommentTok{#> [4] "package:graphics" "package:grDevices" "package:utils" }
\CommentTok{#> [7] "package:datasets" "package:methods" "Autoloads" }
\CommentTok{#> [10] "package:base"}
\KeywordTok{as.environment}\NormalTok{(}\StringTok{"package:stats"}\NormalTok{)}
\CommentTok{#> <environment: package:stats>}
\CommentTok{#> attr(,"name")}
\CommentTok{#> [1] "package:stats"}
\CommentTok{#> attr(,"path")}
\CommentTok{#> [1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stats"}
\end{Highlighting}
\end{Shaded}
\texttt{globalenv()}, \texttt{baseenv()}, the environments on the search
path, and \texttt{emptyenv()} are connected in this diagram. Each time
you load a new package with \texttt{library()} it is inserted between
the global environment and the package that was previously at the top of
the search path.
\includegraphics[width=4.43in,height=1.33in]{diagrams/environments.png/search-path.png}
To create an environment manually, use \texttt{new.env()}. You can list
the bindings in the environment's frame with \texttt{ls()} and see its
parent with \texttt{parent.env()}.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\CommentTok{# the default parent provided by new.env() is environment from }
\CommentTok{# which it is called - in this case that's the global environment.}
\KeywordTok{parent.env}\NormalTok{(e)}
\CommentTok{#> <environment: R_GlobalEnv>}
\KeywordTok{ls}\NormalTok{(e)}
\CommentTok{#> character(0)}
\end{Highlighting}
\end{Shaded}
The easiest way to modify the bindings in an environment is to treat it
like a list:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e$a <-}\StringTok{ }\DecValTok{1}
\NormalTok{e$b <-}\StringTok{ }\DecValTok{2}
\KeywordTok{ls}\NormalTok{(e)}
\CommentTok{#> [1] "a" "b"}
\NormalTok{e$a}
\CommentTok{#> [1] 1}
\end{Highlighting}
\end{Shaded}
By default, \texttt{ls()} only shows names that don't begin with
\texttt{.}. Use \texttt{all.names = TRUE} to show all bindings in an
environment:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e$.a <-}\StringTok{ }\DecValTok{2}
\KeywordTok{ls}\NormalTok{(e)}
\CommentTok{#> [1] "a" "b"}
\KeywordTok{ls}\NormalTok{(e, }\DataTypeTok{all.names =} \OtherTok{TRUE}\NormalTok{)}
\CommentTok{#> [1] ".a" "a" "b"}
\end{Highlighting}
\end{Shaded}
Another useful way to view an environment is \texttt{ls.str()}. It is
more useful than \texttt{str()} because it shows each object in the
environment. Like \texttt{ls()}, it also has an \texttt{all.names}
argument.
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{str}\NormalTok{(e)}
\CommentTok{#> <environment: 0x7fc13e600f50>}
\KeywordTok{ls.str}\NormalTok{(e)}
\CommentTok{#> a : num 1}
\CommentTok{#> b : num 2}
\end{Highlighting}
\end{Shaded}
Given a name, you can extract the value to which it is bound with
\texttt{\$}, \texttt{{[}{[}}, or \texttt{get()}:
\begin{itemize}
\item
\texttt{\$} and \texttt{{[}{[}} look only in one environment and
return \texttt{NULL} if there is no binding associated with the name.
\item
\texttt{get()} uses the regular scoping rules and throws an error if
the binding is not found.
\end{itemize}
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e$c <-}\StringTok{ }\DecValTok{3}
\NormalTok{e$c}
\CommentTok{#> [1] 3}
\NormalTok{e[[}\StringTok{"c"}\NormalTok{]]}
\CommentTok{#> [1] 3}
\KeywordTok{get}\NormalTok{(}\StringTok{"c"}\NormalTok{, }\DataTypeTok{envir =} \NormalTok{e)}
\CommentTok{#> [1] 3}
\end{Highlighting}
\end{Shaded}
Deleting objects from environments works a little differently from
lists. With a list you can remove an entry by setting it to
\texttt{NULL}. In environments, that will create a new binding to
\texttt{NULL}. Instead, use \texttt{rm()} to remove the binding.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\NormalTok{e$a <-}\StringTok{ }\DecValTok{1}
\NormalTok{e$a <-}\StringTok{ }\OtherTok{NULL}
\KeywordTok{ls}\NormalTok{(e)}
\CommentTok{#> [1] "a"}
\KeywordTok{rm}\NormalTok{(}\StringTok{"a"}\NormalTok{, }\DataTypeTok{envir =} \NormalTok{e)}
\KeywordTok{ls}\NormalTok{(e)}
\CommentTok{#> character(0)}
\end{Highlighting}
\end{Shaded}
You can determine if a binding exists in a environment with the
\texttt{exists()} function. Like \texttt{get()}, the default is to
follow regular scoping rules and look in parent environments. If you
don't want this behavior, use \texttt{inherits = FALSE}:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x <-}\StringTok{ }\DecValTok{10}
\KeywordTok{exists}\NormalTok{(}\StringTok{"x"}\NormalTok{, }\DataTypeTok{envir =} \NormalTok{e)}
\CommentTok{#> [1] TRUE}
\KeywordTok{exists}\NormalTok{(}\StringTok{"x"}\NormalTok{, }\DataTypeTok{envir =} \NormalTok{e, }\DataTypeTok{inherits =} \OtherTok{FALSE}\NormalTok{)}
\CommentTok{#> [1] FALSE}
\end{Highlighting}
\end{Shaded}
To compare environments, you must use \texttt{identical()} not
\texttt{==}:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{identical}\NormalTok{(}\KeywordTok{globalenv}\NormalTok{(), }\KeywordTok{environment}\NormalTok{())}
\CommentTok{#> [1] TRUE}
\KeywordTok{globalenv}\NormalTok{() ==}\StringTok{ }\KeywordTok{environment}\NormalTok{()}
\CommentTok{#> Error: comparison (1) is possible only for atomic and list types}
\end{Highlighting}
\end{Shaded}
\subsubsection{Exercises}\label{exercises}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
List three ways in which an environment differs from a list.
\item
If you don't supply an explcit environment, where do \texttt{ls()} and
\texttt{rm()} look? Where does \texttt{\textless{}-} make bindings?
\item
Using \texttt{parent.env()} and a loop (or a recursive function),
verify that the ancestors of \texttt{globalenv()} include
\texttt{baseenv()} and \texttt{emptyenv()}. Use the same basic idea to
implement your own version of \texttt{search()}.
\end{enumerate}
\subsection{Recursing over
environments}\label{recursing-over-environments}
Environments form a tree, so it's often convenient to write a recursive
function. This section shows you how by applying your new knowledge of
environments to understand the helpful \texttt{plyr::where()}. Given a
name, \texttt{where()} that finds the environment \emph{where} it's
defined, using R's regular scoping rules:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{library}\NormalTok{(pryr)}
\NormalTok{x <-}\StringTok{ }\DecValTok{5}
\KeywordTok{where}\NormalTok{(}\StringTok{"x"}\NormalTok{)}
\CommentTok{#> <environment: R_GlobalEnv>}
\KeywordTok{where}\NormalTok{(}\StringTok{"mean"}\NormalTok{)}
\CommentTok{#> <environment: base>}
\end{Highlighting}
\end{Shaded}
The definition of \texttt{where()} is straightforward. It has two
arguments: the name to look for (as a string), and the environment in
which to start the search. (We'll learn later why
\texttt{parent.frame()} is a good default in
\hyperref[calling-environments]{calling environments}.)
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{where <-}\StringTok{ }\NormalTok{function(name, }\DataTypeTok{env =} \KeywordTok{parent.frame}\NormalTok{()) \{}
\NormalTok{if (}\KeywordTok{identical}\NormalTok{(env, }\KeywordTok{emptyenv}\NormalTok{())) \{}
\CommentTok{# Base case}
\KeywordTok{stop}\NormalTok{(}\StringTok{"Can't find "}\NormalTok{, name, }\DataTypeTok{call. =} \OtherTok{FALSE}\NormalTok{)}
\NormalTok{\} else if (}\KeywordTok{exists}\NormalTok{(name, }\DataTypeTok{envir =} \NormalTok{env, }\DataTypeTok{inherits =} \OtherTok{FALSE}\NormalTok{)) \{}
\CommentTok{# Success case}
\NormalTok{env}
\NormalTok{\} else \{}
\CommentTok{# Recursive case}
\KeywordTok{where}\NormalTok{(name, }\KeywordTok{parent.env}\NormalTok{(env))}
\NormalTok{\}}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
There are three cases:
\begin{itemize}
\item
The base case: we've reached the empty environment and haven't found
the binding. We can't go any further, so we throw an error.
\item
The successful case: the name exists in this environment, so we return
the environment.
\item
The recursive case: the name was not found in this environment, so try
the parent.
\end{itemize}
It's easier to see what's going on with a picture. Imagine you have two
environments as in the following diagram:
\includegraphics[width=3.32in,height=0.65in]{diagrams/environments.png/where-ex.png}
\begin{itemize}
\item
If you're looking for \texttt{a}, \texttt{where()} will find it in the
first environment.
\item
If you're looking for \texttt{b}, it's not in the first environment,
so \texttt{where()} will look in its parent and find it there.
\item
If you're looking for \texttt{c}, it's not in the first environment,
or the second environment, so \texttt{where()} gets to the empty
environment and throws an error.
\end{itemize}
It's natural to work with environments recursively, so \texttt{where()}
provides a useful template to follow. Removing the specifics of
\texttt{where()} shows the structure more clearly:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{f <-}\StringTok{ }\NormalTok{function(..., }\DataTypeTok{env =} \KeywordTok{parent.frame}\NormalTok{()) \{}
\NormalTok{if (}\KeywordTok{identical}\NormalTok{(env, }\KeywordTok{emptyenv}\NormalTok{())) \{}
\CommentTok{# base case}
\NormalTok{\} else if (success) \{}
\CommentTok{# success case}
\NormalTok{\} else \{}
\CommentTok{# recursive case}
\KeywordTok{f}\NormalTok{(..., }\DataTypeTok{env =} \KeywordTok{parent.env}\NormalTok{(env))}
\NormalTok{\}}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
#> NULL
\end{verbatim}
It's possible use a loop instead of with recursion. This might run
slightly faster (because we eliminate some function calls), but I think
it's harder to understand. I include it because you might find it easier
to see what's happening if you're less familiar with recursive
functions.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{is_empty <-}\StringTok{ }\NormalTok{function(x) }\KeywordTok{identical}\NormalTok{(x, }\KeywordTok{emptyenv}\NormalTok{())}
\NormalTok{f2 <-}\StringTok{ }\NormalTok{function(..., }\DataTypeTok{env =} \KeywordTok{parent.frame}\NormalTok{()) \{}
\NormalTok{while(!}\KeywordTok{is_empty}\NormalTok{(env)) \{}
\NormalTok{if (success) \{}
\CommentTok{# success case}
\KeywordTok{return}\NormalTok{()}
\NormalTok{\}}
\CommentTok{# inspect parent}
\NormalTok{env <-}\StringTok{ }\KeywordTok{parent.env}\NormalTok{(env)}
\NormalTok{\}}
\CommentTok{# base case}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
\begin{verbatim}
#> NULL
\end{verbatim}
\subsubsection{Exercises}\label{exercises-1}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
Modify \texttt{where()} to find all environments that contain a
binding for \texttt{name}.
\item
Write your own version of \texttt{get()} using a function written in
the style of \texttt{where()}.
\item
Write a function called \texttt{fget()} that finds only function
objects. It should have two arguments, \texttt{name} and \texttt{env},
and should obey the regular scoping rules for functions: if there's an
object with a matching name that's not a function, look in the parent.
For an added challenge, also add an \texttt{inherits} argument which
controls whether the function recurses up the parents or only looks in
one environment.
\item
Write your own version of \texttt{exists(inherits = FALSE)} (Hint: use
\texttt{ls()}). Write a recursive version that behaves like
\texttt{exists(inherits = TRUE)}.
\end{enumerate}
\subsection{Function environments}\label{function-environments}
Most of the time, you do not create environments directly. They are
created as a consequence of working with functions. This section
discusses the four types of environments associated with a function:
enclosing, binding, execution, and calling.
The \textbf{enclosing} environment is a property of a function that is
set when it's created. Every function has one and only one enclosing
environment. For the three other types of environment, there may be 0, 1
or many environments associated with each function:
\begin{itemize}
\item
Binding a function to a name with \texttt{\textless{}-} defines a
\textbf{binding} environment.
\item
Calling a function creates an ephemeral \textbf{execution} environment
that stores variables created during execution.
\item
Every execution environment is associated with a \textbf{calling}
environment, which tells you where the function was called.
\end{itemize}
The following sections will explain why each of these environments are
important, how to access them, and how you might use them.
\subsubsection{The enclosing
environment}\label{the-enclosing-environment}
When a function is created, it gains a reference to the environment
where it was made. This is the \textbf{enclosing environment} and is
used for lexical scoping. When called with a function
\texttt{environment()} returns the enclosing environment:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{y <-}\StringTok{ }\DecValTok{1}
\NormalTok{f <-}\StringTok{ }\NormalTok{function(x) x +}\StringTok{ }\NormalTok{y}
\KeywordTok{environment}\NormalTok{(f)}
\CommentTok{#> <environment: R_GlobalEnv>}
\end{Highlighting}
\end{Shaded}
In diagrams, I'll depict functions as a rounded rectangles. The
enclosing environment of a function is given by a small black circle:
\includegraphics[width=2.06in,height=0.74in]{diagrams/environments.png/enclosing.png}
\subsubsection{Binding environments}\label{binding-environments}
The enclosing environment determines how the function finds values; the
binding environments determine how we find the function. The previous
diagram is too simple because functions don't have names. Instead, the
name of a function is defined by a binding. The binding environments of
a function are all the environments which have a binding to it. The
following diagram better reflects relatively because the the enclosing
environment contains a binding from \texttt{f} to the function:
\includegraphics[width=2.06in,height=1.18in]{diagrams/environments.png/binding.png}
In this case the enclosing and binding environments are the same. They
will be differently if you assign a function into a different
environment:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{e <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\NormalTok{e$g <-}\StringTok{ }\NormalTok{function() }\DecValTok{1}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.06in,height=1.18in]{diagrams/environments.png/binding-2.png}
The enclosing environment belongs to the function, and never changes,
even if the function is moved to a different environment.
The distinction between the binding environment and the enclosing
environment is important because it is critical for package namespaces.
Package namespaces keep packages independent. For example, if package A
uses the base \texttt{mean()} function, what happens if package B
creates it's own \texttt{mean()} function? Namespaces ensure that
package A continues to use the base \texttt{mean()} function, and that
package A is not affected by package B (unless explicitly asked for).
Namespaces are implemented using environments, taking advantage of the
fact that functions don't have to live in their enclosing environments.
For example, take the base function \texttt{sd()}. It's binding and
enclosing environments are different:
\begin{Shaded}
\begin{Highlighting}[]
\KeywordTok{environment}\NormalTok{(sd)}
\CommentTok{#> <environment: namespace:stats>}
\KeywordTok{where}\NormalTok{(}\StringTok{"sd"}\NormalTok{)}
\CommentTok{#> <environment: package:stats>}
\CommentTok{#> attr(,"name")}
\CommentTok{#> [1] "package:stats"}
\CommentTok{#> attr(,"path")}
\CommentTok{#> [1] "/Library/Frameworks/R.framework/Versions/3.1/Resources/library/stats"}
\end{Highlighting}
\end{Shaded}
The definition of \texttt{sd()} uses \texttt{var()}, but if we make our
own version of \texttt{var()} it doesn't affect \texttt{sd()}:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x <-}\StringTok{ }\DecValTok{1}\NormalTok{:}\DecValTok{10}
\KeywordTok{sd}\NormalTok{(x)}
\CommentTok{#> [1] 3.028}
\NormalTok{var <-}\StringTok{ }\NormalTok{function(x, }\DataTypeTok{na.rm =} \OtherTok{TRUE}\NormalTok{) }\DecValTok{100}
\KeywordTok{sd}\NormalTok{(x)}
\CommentTok{#> [1] 3.028}
\end{Highlighting}
\end{Shaded}
This works because every package has two environments associated with
it: the \emph{package} environment and the \emph{namespace} environment.
The package environment contains every publicly accessible function, and
is place on the search path. The namespace environment contains all
functions (including internal functions), and it's parent environment is
a special imports environment that contains bindings to all the
functions that the package needs. Every exported function in a package
is bound into the \emph{package} environment, but enclosed by the
\emph{namespace} environment.
\includegraphics[width=4.43in,height=2.36in]{diagrams/environments.png/namespace.png}
When we type \texttt{var} into the console, it's found first in the
global environment. When \texttt{sd()} looks for \texttt{var()} it finds
it first in its namespace environment so never looks in the
\texttt{globalenv()}.
\subsubsection{Execution environments}\label{execution-environments}
What will the following function return the first time its run? What
about the second?
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{g <-}\StringTok{ }\NormalTok{function(x) \{}
\NormalTok{if (!}\KeywordTok{exists}\NormalTok{(}\StringTok{"a"}\NormalTok{, }\DataTypeTok{inherits =} \OtherTok{FALSE}\NormalTok{)) \{}
\KeywordTok{message}\NormalTok{(}\StringTok{"Defining a"}\NormalTok{)}
\NormalTok{a <-}\StringTok{ }\DecValTok{1}
\NormalTok{\} else \{}
\NormalTok{a <-}\StringTok{ }\NormalTok{a +}\StringTok{ }\DecValTok{1}
\NormalTok{\}}
\NormalTok{a}
\NormalTok{\}}
\KeywordTok{g}\NormalTok{(}\DecValTok{10}\NormalTok{)}
\KeywordTok{g}\NormalTok{(}\DecValTok{10}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
This function returns the same value every time it is called because of
the fresh start principle, described in \hyperref[a-fresh-start]{a fresh
start}. Each time a function is called, a new environment is created to
host execution. The parent of the execution environment is the enclosing
environment of the function. Once the function has completed, this
environment is thrown away.
Let's depict that graphically with a simpler function. I draw execution
environments around the function they belong to with a dotted border.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{h <-}\StringTok{ }\NormalTok{function(x) \{}
\NormalTok{a <-}\StringTok{ }\DecValTok{1}
\NormalTok{x +}\StringTok{ }\NormalTok{a}
\NormalTok{\}}
\NormalTok{y <-}\StringTok{ }\KeywordTok{h}\NormalTok{(}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.95in,height=4.43in]{diagrams/environments.png/execution.png}
When you create a function inside another function, the enclosing
environment of the child is the execution environment of the parent.
When this happens, the execution environment is no longer ephemeral. The
following example illustrates that idea with a function factory,
\texttt{plus()}. We use that factory to create a function called
\texttt{plus\_one()}. The enclosing environment of \texttt{plus\_one()}
is the execution environment of \texttt{plus()} where \texttt{x} bound
to the value 1.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{plus <-}\StringTok{ }\NormalTok{function(x) \{}
\NormalTok{function(y) x +}\StringTok{ }\NormalTok{y}
\NormalTok{\}}
\NormalTok{plus_one <-}\StringTok{ }\KeywordTok{plus}\NormalTok{(}\DecValTok{1}\NormalTok{)}
\KeywordTok{identical}\NormalTok{(}\KeywordTok{parent.env}\NormalTok{(}\KeywordTok{environment}\NormalTok{(plus_one)), }\KeywordTok{environment}\NormalTok{(plus))}
\CommentTok{#> [1] TRUE}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=2.51in,height=2.06in]{diagrams/environments.png/closure-2.png}
\hyperdef{}{calling-environments}{\subsubsection{Calling
environments}\label{calling-environments}}
Look at the following code. What do you expect \texttt{g()} to return
when the code is run?
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{h <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{x <-}\StringTok{ }\DecValTok{10}
\NormalTok{function() \{}
\NormalTok{x}
\NormalTok{\}}
\NormalTok{\}}
\NormalTok{i <-}\StringTok{ }\KeywordTok{h}\NormalTok{()}
\NormalTok{x <-}\StringTok{ }\DecValTok{20}
\KeywordTok{i}\NormalTok{()}
\end{Highlighting}
\end{Shaded}
The top-level \texttt{x} is a red herring: using the regular scoping
rules, \texttt{g()} looks first where it is defined and finds that the
value associated with \texttt{x} is 10. However, it's still meaningful
to ask what value \texttt{x} is associated with in the environment where
\texttt{g()} is called: \texttt{x} is 10 in the environment where
\texttt{g()} is defined, but it is 20 in the environment where
\texttt{g()} is called.
We can access this environment using the unfortunately named
\texttt{parent.frame()}. This function returns the \textbf{environment}
where the function was called. We can also use this function to look up
the value of names in that environment:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{f2 <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{x <-}\StringTok{ }\DecValTok{10}
\NormalTok{function() \{}
\NormalTok{def <-}\StringTok{ }\KeywordTok{get}\NormalTok{(}\StringTok{"x"}\NormalTok{, }\KeywordTok{environment}\NormalTok{())}
\NormalTok{cll <-}\StringTok{ }\KeywordTok{get}\NormalTok{(}\StringTok{"x"}\NormalTok{, }\KeywordTok{parent.frame}\NormalTok{())}
\KeywordTok{list}\NormalTok{(}\DataTypeTok{defined =} \NormalTok{def, }\DataTypeTok{called =} \NormalTok{cll)}
\NormalTok{\}}
\NormalTok{\}}
\NormalTok{g2 <-}\StringTok{ }\KeywordTok{f2}\NormalTok{()}
\NormalTok{x <-}\StringTok{ }\DecValTok{20}
\KeywordTok{str}\NormalTok{(}\KeywordTok{g2}\NormalTok{())}
\CommentTok{#> List of 2}
\CommentTok{#> $ defined: num 10}
\CommentTok{#> $ called : num 20}
\end{Highlighting}
\end{Shaded}
In more complicated scenarios, there's not just one parent call, but a
sequence of calls which lead all the way back to the initiating
function, called from the top-level. The following code generates a call
stack three levels deep. The open-ended arrows represent the caling
environment of each execution environment.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x <-}\StringTok{ }\DecValTok{0}
\NormalTok{y <-}\StringTok{ }\DecValTok{10}
\NormalTok{f <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{x <-}\StringTok{ }\DecValTok{1}
\KeywordTok{g}\NormalTok{()}
\NormalTok{\}}
\NormalTok{g <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{x <-}\StringTok{ }\DecValTok{2}
\KeywordTok{h}\NormalTok{()}
\NormalTok{\}}
\NormalTok{h <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{x <-}\StringTok{ }\DecValTok{3}
\NormalTok{x +}\StringTok{ }\NormalTok{y}
\NormalTok{\}}
\KeywordTok{f}\NormalTok{()}
\CommentTok{#> [1] 13}
\end{Highlighting}
\end{Shaded}
\includegraphics[width=3.25in,height=1.77in]{diagrams/environments.png/calling.png}
Note that each execution environment has two parents: a calling
environment and an enclosing environment. R's regular scoping rules only
use the enclosing parent; \texttt{parent.frame()} allows you to access
the calling parent.
Looking up variables in the calling environment rather than in the
enclosing environment is called \textbf{dynamic scoping}. Few languages
implement dynamic scoping (Emacs Lisp is a
\href{http://www.gnu.org/software/emacs/emacs-paper.html\#SEC15}{notable
exception}). This is because dynamic scoping makes it much harder to
reason about how a function operates: not only do you need to know how
it was defined, you also need to know in what context it was called.
Dynamic scoping is primarily useful for developing functions that aid
interactive data analysis. It is one of the topics discussed in
\hyperref[nse]{non-standard evaluation}.
\subsubsection{Exercises}\label{exercises-2}
\begin{enumerate}
\def\labelenumi{\arabic{enumi}.}
\item
List the four environments associated with a function. What does each
one do? Why is the distinction between enclosing and binding
environments particularly important?
\item
Draw a diagram that shows the enclosing environments of this function:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{f1 <-}\StringTok{ }\NormalTok{function(x1) \{}
\NormalTok{f2 <-}\StringTok{ }\NormalTok{function(x2) \{}
\NormalTok{f3 <-}\StringTok{ }\NormalTok{function(x3) \{}
\NormalTok{x1 +}\StringTok{ }\NormalTok{x2 +}\StringTok{ }\NormalTok{x3}
\NormalTok{\}}
\KeywordTok{f3}\NormalTok{(}\DecValTok{3}\NormalTok{)}
\NormalTok{\}}
\KeywordTok{f2}\NormalTok{(}\DecValTok{2}\NormalTok{)}
\NormalTok{\}}
\KeywordTok{f1}\NormalTok{(}\DecValTok{1}\NormalTok{)}
\end{Highlighting}
\end{Shaded}
\item
Expand your previous diagram to show function bindings.
\item
Expand it again to show the execution and calling environments.
\item
Write an enhanced version of \texttt{str()} that provides more
information about functions. Show where the function was found and
what environment it was defined in.
\end{enumerate}
\hyperdef{}{explicit-environments}{\subsection{Explicit
environments}\label{explicit-environments}}
As well as powering scoping, environments are also useful data
structures in their own right because they have \textbf{reference
semantics}. Unlike most objects in R, when you modify an environment, it
does not make a copy. For example, look at this \texttt{modify()}
function.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{modify <-}\StringTok{ }\NormalTok{function(x) \{}
\NormalTok{x$a <-}\StringTok{ }\DecValTok{2}
\KeywordTok{invisible}\NormalTok{()}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
If you apply it to a list, the original list is not changed because
modifying a list actually modifies a copy.
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_l <-}\StringTok{ }\KeywordTok{list}\NormalTok{()}
\NormalTok{x_l$a <-}\StringTok{ }\DecValTok{1}
\KeywordTok{modify}\NormalTok{(x_l)}
\NormalTok{x_l$a}
\CommentTok{#> [1] 1}
\end{Highlighting}
\end{Shaded}
However, if you apply it to an environment, the original environment
\emph{is} modified:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x_e <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\NormalTok{x_e$a <-}\StringTok{ }\DecValTok{1}
\KeywordTok{modify}\NormalTok{(x_e)}
\NormalTok{x_e$a}
\CommentTok{#> [1] 2}
\end{Highlighting}
\end{Shaded}
This behaviour is often called reference semantics, because you can
think of every binding to an environment as a reference to the same
environment.
Just as you can use a list to pass data between functions, you can also
use an environment. When creating your own environment, note that you
should set its environment to the empty environment. This ensures you
don't accidentally inherit objects from somewhere else:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{x <-}\StringTok{ }\DecValTok{1}
\NormalTok{e1 <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{()}
\KeywordTok{get}\NormalTok{(}\StringTok{"x"}\NormalTok{, e1)}
\CommentTok{#> [1] 1}
\NormalTok{e2 <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{(}\DataTypeTok{parent =} \KeywordTok{emptyenv}\NormalTok{())}
\KeywordTok{get}\NormalTok{(}\StringTok{"x"}\NormalTok{, e2)}
\CommentTok{#> Error: object 'x' not found}
\end{Highlighting}
\end{Shaded}
Environments are useful data structures for solve three common problems:
\begin{itemize}
\itemsep1pt\parskip0pt\parsep0pt
\item
Avoiding copies of large data.
\item
Managing state within a package.
\item
Efficiently lookup values from names
\end{itemize}
These are described in turn below.
\subsubsection{Avoiding copies}\label{avoiding-copies}
Since environments have reference semantics, you'll never accidentally
create a copy. This makes it a useful vessel for large objects. It's a
common technique for bioconductor packages which often have to manage
large genomic objects. Changes to R 3.1.0 have made this use
substantially less important because modifying a list no longer makes a
deep copy. Previously, modifying a single element of a list would cause
every element to be copied, an expensive operation if some elements are
large. Now, modifying a list efficiently reuses existing vectors, saving
much time.
\subsubsection{Package state}\label{package-state}
Explicit environments are useful in packages because they allow you to
maintain state across function calls. A typical use case looks like
this:
\begin{Shaded}
\begin{Highlighting}[]
\NormalTok{my_env <-}\StringTok{ }\KeywordTok{new.env}\NormalTok{(}\DataTypeTok{parent =} \KeywordTok{emptyenv}\NormalTok{())}
\NormalTok{my_env$a <-}\StringTok{ }\DecValTok{1}
\NormalTok{get_a <-}\StringTok{ }\NormalTok{function() \{}
\NormalTok{my_env$a}
\NormalTok{\}}
\NormalTok{set_a <-}\StringTok{ }\NormalTok{function(value) \{}
\NormalTok{old <-}\StringTok{ }\NormalTok{my_env$a}
\NormalTok{my_env$a <-}\StringTok{ }\NormalTok{value}
\KeywordTok{invisible}\NormalTok{(old)}
\NormalTok{\}}
\end{Highlighting}
\end{Shaded}
Returning the old value from setter functions is a good pattern because
it makes it easier to reset the previous value in conjunction with
\texttt{on.exit()} (See more in \hyperref[on-exit]{on exit}. )
\subsubsection{As a hashmap}\label{as-a-hashmap}
A hashmap is a data structure that takes constant, O(1), time to find an
object based on its name. Environments provide this behaviour by
default, so can be used to simulate a hashmap. See the CRAN package
\texttt{hash} for a complete development of this idea.
\subsection{Explicit scoping with
\texttt{local}}\label{explicit-scoping-with-local}