Skip to content

scope of variables in R

1 message · Wacek Kusnierczyk

#
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