Skip to content
Prev 175696 / 398503 Next

scope of variables in R

Stavros Macrakis wrote:
indeed, i too was thinking about something like perl's local:

    print $$;
    # 123456
    { local $$;
       print $$; }
    # (empty string)

or fluid-let in scheme.

i don't know of any construct in r that would localize global variables
in this manner, but for options, par, etc., you do have an option, the
(arguably ugly) idiom with storing the previous value and restoring it
on exit:

    print(getOption('digits'))
    # 7

    local({
       options = options(digits=1)
       on.exit(options(options))
       print(getOption('digits')) })
    # 1

    print(getOption('digits'))
    # 7

it's like manually localizing a global variable.

vQ