[Rd] environment question
Paul Johnson
pauljohn32 at gmail.com
Sun Dec 26 22:30:08 CET 2010
Hello, everybody.
I'm putting together some lecture notes and course exercises on R
programming. My plan is to pick some R packages, ask students to read
through code and see why things work, maybe make some changes. As I
look for examples, I'm running up against the problem that packages
use coding idioms that are unfamiliar to me.
A difficult thing for me is explaining scope of variables in R
functions. When should we pass an object to a function, when should
we let the R system search about for an object? I've been puzzling
through ?environment for quite a while.
Here's an example from one of the packages that I like, called "ltm".
In the function "ltm.fit" the work of calculating estimates is sent to
different functions like "EM' and "loglikltm" and "scoreltm". Before
that, this is used:
environment(EM) <- environment(loglikltm) <- environment(scoreltm) <-
environment()
##and then EM is called
res.EM <- EM(betas, constraint, control$iter.em, control$verbose)
I want to make sure I understand this. The environment line gets the
current environment and then assigns it for those 3 functions, right?
All variables and functions that can be accessed from the current
position in the code become available to function EM, loglikltm,
scoreltm.
So, which options should be explicitly inserted into a function call,
which should be left in the environment for R to find when it needs
them?
1. I *think* that when EM is called, the variables "betas",
"constraint", and "control" are already in the environment.
The EM function is declared like this, using the same words "beta" and
"constraint"
EM <-
function (betas, constraint, iter, verbose = FALSE) {
It seems to me that if I wrote the function call like this (leave out
"betas" and "constraint")
res.EM <- EM(control$iter.em, control$verbose)
R will run EM and go find "betas" and "constraint" in the environment,
there was no need to name them as arguments.
2 Is a function like EM allowed to alter objects that it finds through
the environment, ones that are not passed as arguments? I understand
that a function cannot alter an object that is passed explicitly, but
what about the ones it grabs from the environment?
If you have ideas about packages that might be handy teaching
examples, please let me know.
pj
--
Paul E. Johnson
Professor, Political Science
1541 Lilac Lane, Room 504
University of Kansas
More information about the R-devel
mailing list