R: disentangling scopes
Posted
by rescdsk
on Stack Overflow
See other posts from Stack Overflow
or by rescdsk
Published on 2010-04-14T02:28:35Z
Indexed on
2010/04/14
2:33 UTC
Read the original article
Hit count: 363
Hi,
Right now, in my R project, I have functions1.R
with doFoo()
and doBar()
, functions2.R
with other functions, and main.R
with the main program in it, which first does source('functions1.R'); source('functions2.R')
, and then calls the other functions.
I've been starting the program from the R GUI in Mac OS X, with source('main.R')
. This is fine the first time, but after that, the variables that were defined the first time through the program are defined for the second time functions*.R
are sourced, and so the functions get a whole bunch of extra variables defined.
I don't want that! I want an "undefined variable" error when my function uses a variable it shouldn't! Twice this has given me very late nights of debugging!
So how do other people deal with this sort of problem? Is there something like source()
, but that makes an independent namespace that doesn't fall through to the main one? Making a package seems like one solution, but it seems like a big pain in the butt compared to e.g. Python, where a source file is automatically a separate namespace.
Any tips? Thank you!
© Stack Overflow or respective owner