The Art of R Programming
The fix is to passdandyas arguments: f function(y) { d<-8 return(h(d,y)) } h function(dee,yyy) { return(dee*(w+yyy)) } f(2) ...
Here’s an example: >f function(y) { d<-8 return(h(d,y)) } >h function(dee,yyy) { print(ls()) print(ls(envir=parent.fram ...
>t<-4 > f(t) [1] 13 [1] 120 >w [1] 12 >t [1] 4 So,wat the top level did not change, even though it appeared to ch ...
When we callf(), it in turn callsg(), which then callsh(). In the debug- ging setting, say we are currently about to execute the ...
[1] 1 b: [1] 2 a: [1] 1 To see how this works, we’ll first look at theget()function, one of the most useful utilities in R. Its ...
cases. (As of this writing, the current version of R has an experimental fea- ture calledreference classes, which may reduce the ...
# set x and y lxy$x <- ... lxy$y <- ... lxy <- f(lxy) # use new x and y ... <- lxy$x ... <- lxy$y However, this m ...
[1] 1 >z [1] 3 >u [1] 2 Let’s look at the impact (or not) on the three top-level variablesx,z, andu: x: Even thoughxwas t ...
7.8.2 Writing to Nonlocals with assign()................................ You can also use theassign()function to write to upper- ...
7.8.3 Extended Example: Discrete-Event Simulation in R.................. Discrete-event simulation (DES)is widely used in busine ...
(2.3,“arrival”). This event is pulled off the list, simulated time is updated to 2.3, and we react to the arrival event as follo ...
our call is as follows: dosim(mm1initglbls,mm1reactevnt,mm1prntrslts,10000.0, list(arrvrate=0.5,srvrate=1.0)) Here’s the library ...
42 # binary search of insertion point of y in the sorted vector x; returns 43 # the position in x before which y should be inser ...
89 sim$dbg <<- dbg 90 initglbls(apppars) 91 while(sim$currtime < maxsimtime) { 92 head <- getnextevnt() 93 sim$currt ...
33 if (length(mm1glbls$srvq) == 0) { 34 mm1glbls$srvq <<- head$arrvtime 35 srvdonetime <- sim$currtime + rexp(1,mm1glbl ...
mm1glbls$srvq <<- mm1glbls$srvq[-1] # more still in the queue? if (length(mm1glbls$srvq) > 0) { # schedule new service ...
time via a binary search operation by event time. This is done in line 31 withinschedevnt(), the function that inserts a newly c ...
# set x and y lxy$x <- ... lxy$y <- ... lxy <- f(lxy) # use new x and y ... <- lxy$x ... <- lxy$y As noted earlie ...
So, usingsimas a global seems justified. Nevertheless, if we were bound and determined to avoid using globals, we could have pla ...
Withindosim(), the line sim <<- list() would be replaced by assign("simenv",new.env(),envir=.GlobalEnv) This would create ...
«
5
6
7
8
9
10
11
12
13
14
»
Free download pdf