Ejemplo n.º 1
0
def test_call_in_context_nested(use_rlock):
    ls = rinterface.baseenv['ls']
    get = rinterface.baseenv['get']
    assert 'foo' not in ls()
    with rinterface.local_context() as lc_a:
        lc_a['foo'] = 123
        assert tuple(get('foo')) == (123, )
        with rinterface.local_context(use_rlock=use_rlock) as lc_b:
            lc_b['foo'] = 456
            assert tuple(get('foo')) == (456, )
        assert tuple(get('foo')) == (123, )
    assert 'foo' not in ls()
Ejemplo n.º 2
0
def test_call_in_context(give_env, use_rlock):
    ls = rinterface.baseenv['ls']
    get = rinterface.baseenv['get']
    if give_env:
        env = rinterface.baseenv['new.env']()
    else:
        env = None
    assert 'foo' not in ls()
    with rinterface.local_context(env=env, use_rlock=use_rlock) as lc:
        lc['foo'] = 123
        assert tuple(get('foo')) == (123, )
    assert 'foo' not in ls()
Ejemplo n.º 3
0
def local_context(env: typing.Optional[sexp.SexpEnvironment] = None,
                  use_rlock: bool = True) -> typing.Iterator[Environment]:
    """Local context for the evaluation of R code.

    This is a wrapper around the rpy2.rinterface function with the
    same name.

    Args:
    - env: an environment to use as a context. If not specified (None, the
      default), a child environment to the current context is created.
    - use_rlock: whether to use a threading lock (see the documentation about
      "rlock". The default is True.

    Returns:
    Yield the environment (passed to env, or created).
    """
    with rinterface.local_context(env=env, use_rlock=use_rlock) as lc:
        yield Environment(lc)