As I understand, Renjin context contains variables with their values.
Suppose, I invoked this once:
engine.eval("df <- data.frame(x=1:10, y=(1:10)+rnorm(n=10))");
engine.eval("print(df)");
engine.eval("print(lm(y ~ x, df))");
How can I clear context so that invoking engine.eval("print(df)");
produces exception about unknown variable after clearing context?
I want to eval different calculations without mixing context and recreating engine.
The "context" you mean is I think the R global environment. Each instance of RenjinScriptEngine has its own, independent global environment.
You can inspect, modify, and clear the values set it in the global environment using R base functions, like so:
engine.eval("print(ls())"); // print all symbols in the global environment
engine.eval("rm(ls())"); // remove all symbols in the global environment
// (that don't start with a "." )
engine.eval("rm(ls(all.names=TRUE))"); // remove everything
You can also use the javax.script API to do the same:
engine.getBindings(ScriptContext.ENGINE).clear();
Depending on your use case, clearing the global environment may be sufficient, but keep in mind that a RenjinScriptEngine contains additional state.
If you load packages and/or their namespaces via library()
, require()
or mypackage::fun()
, that will affect the internal state of the RenjinScriptEngine.
Other things that affect the Session-level (RenjinScriptEngine) state:
options()