/// <summary> /// Creates an instance. /// </summary> /// <param name="device">The implementation.</param> public CharacterDeviceAdapter(ICharacterDevice device) { if (device == null) { throw new ArgumentNullException("device"); } this.device = device; }
/// <summary> /// Initialize this REngine object. Only the first call has an effect. Subsequent calls to this function are ignored. /// </summary> /// <param name="parameter">The optional startup parameters</param> /// <param name="device">The optional character device to use for the R engine</param> /// <param name="setupMainLoop">if true, call the functions to initialise the embedded R</param> public void Initialize(StartupParameter parameter = null, ICharacterDevice device = null, bool setupMainLoop = true) { // Console.WriteLine("REngine.Initialize start"); if (this.isRunning) return; // Console.WriteLine("REngine.Initialize, after isRunning checked as false"); this.parameter = parameter ?? new StartupParameter(); this.adapter = new CharacterDeviceAdapter(device ?? DefaultDevice); // Disabling the stack checking here, to try to avoid the issue on Linux. // The disabling used to be here around end Nov 2013. Was moved later in this // function to cater for disabling on Windows, @ rev 305, however this may have // re-broken on Linux. so we may need to call it twice. SetCstackChecking(); // Console.WriteLine("Initialize-SetCstackChecking; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); if (!setupMainLoop) { this.isRunning = true; return; } string[] R_argv = BuildRArgv(this.parameter); //string[] R_argv = new[]{"rdotnet_app", "--interactive", "--no-save", "--no-restore-data", "--max-ppsize=50000"}; //rdotnet_app --quiet --interactive --no-save --no-restore-data --max-mem-size=18446744073709551615 --max-ppsize=50000 GetFunction<R_setStartTime>()(); int R_argc = R_argv.Length; // Console.WriteLine("Initialize-R_setStartTime; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); if (NativeUtility.GetPlatform() == PlatformID.Win32NT) { // Attempted Fix for https://rdotnet.codeplex.com/workitem/110; not working // Tried to make sure that the command line options are taken into account. They are NOT effectively so via Rf_initialize_R only. // The problem is that cmdlineoptions assumes it is called by RGui.exe or RTerm.exe, and overrides R_HOME // GetFunction<R_set_command_line_arguments>()(R_argc, R_argv); // GetFunction<cmdlineoptions>()(R_argc, R_argv); } var status = GetFunction<Rf_initialize_R>()(R_argc, R_argv); if (status != 0) throw new Exception("A call to Rf_initialize_R returned a non-zero; status=" + status); // Console.WriteLine("Initialize-Rf_initialize_R; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); SetCstackChecking(); // following in RInside: may not be needed. //GetFunction<R_ReplDLLinit> () (); //this.parameter.Interactive = true; this.adapter.Install(this, this.parameter); //Console.WriteLine("Initialize-adapter installation; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); switch (NativeUtility.GetPlatform()) { case PlatformID.Win32NT: GetFunction<R_SetParams_Windows>("R_SetParams")(ref this.parameter.start); break; case PlatformID.MacOSX: case PlatformID.Unix: GetFunction<R_SetParams_Unix>("R_SetParams")(ref this.parameter.start.Common); //Console.WriteLine("Initialize-R_SetParams_Unix; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); break; } GetFunction<setup_Rmainloop>()(); //Console.WriteLine("Initialize-after setup_Rmainloop; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); // See comments in the first call to SetCstackChecking in this function as to why we (may) need it twice. SetCstackChecking(); this.isRunning = true; //Console.WriteLine("Initialize-just before leaving; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); // Partial Workaround (hopefully temporary) for https://rdotnet.codeplex.com/workitem/110 if (NativeUtility.GetPlatform() == PlatformID.Win32NT) { Evaluate(string.Format("invisible(memory.limit({0}))", (this.parameter.MaxMemorySize / 1048576UL))); } }
/// <summary> /// Gets a reference to the R engine, creating and initializing it if necessary. In most cases users need not provide any parameter to this method. /// </summary> /// <param name="dll">The file name of the library to load, e.g. "R.dll" for Windows. You usually do not need need to provide this optional parameter</param> /// <param name="initialize">Initialize the R engine after its creation. Default is true</param> /// <param name="parameter">If 'initialize' is 'true', you can optionally specify the specific startup parameters for the R native engine</param> /// <param name="device">If 'initialize' is 'true', you can optionally specify a character device for the R engine to use</param> /// <returns>The engine.</returns> /// <example> /// <p>A minimalist approach is to just call GetInstance</p> /// <code> /// REngine.SetEnvironmentVariables(); /// var engine = REngine.GetInstance(); /// engine.Evaluate("letters[1:26]"); /// </code> /// <p>In unusual circumstances you may need to elaborate on the initialization in a separate method call</p> /// <code> /// REngine.SetEnvironmentVariables(rPath=@"c:\my\peculiar\path\to\R\bin\x64"); /// var engine = REngine.GetInstance(initialize=false); /// StartupParameter sParams=new StartupParameter(){NoRenviron=true;}; /// ICharacterDevice device = new YourCustomDevice(); /// engine.Initialize(parameter: sParams, device: device); /// engine.Evaluate("letters[1:26]"); /// </code> /// </example> public static REngine GetInstance(string dll = null, bool initialize = true, StartupParameter parameter = null, ICharacterDevice device = null) { if (!environmentIsSet) // should there be a warning? and how? SetEnvironmentVariables(); if (engine == null) { engine = CreateInstance(EngineName, dll); if (initialize) engine.Initialize(parameter, device); } if (engine.Disposed) throw new InvalidOperationException("The single REngine instance has already been disposed of (i.e. shut down). Multiple engine restart is not possible."); return engine; }
/// <summary> /// Initialize this REngine object. Only the first call has an effect. Subsequent calls to this function are ignored. /// </summary> /// <param name="parameter">The optional startup parameters</param> /// <param name="device">The optional character device to use for the R engine</param> /// <param name="setupMainLoop">if true, call the functions to initialise the embedded R</param> public void Initialize(StartupParameter parameter = null, ICharacterDevice device = null, bool setupMainLoop = true) { // Console.WriteLine("REngine.Initialize start"); if (this.isRunning) { return; } // Console.WriteLine("REngine.Initialize, after isRunning checked as false"); this.parameter = parameter ?? new StartupParameter(); this.adapter = new CharacterDeviceAdapter(device ?? DefaultDevice); // Disabling the stack checking here, to try to avoid the issue on Linux. // The disabling used to be here around end Nov 2013. Was moved later in this // function to cater for disabling on Windows, @ rev 305, however this may have // re-broken on Linux. so we may need to call it twice. SetCstackChecking(); // Console.WriteLine("Initialize-SetCstackChecking; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); if (!setupMainLoop) { this.isRunning = true; return; } string[] R_argv = BuildRArgv(this.parameter); //string[] R_argv = new[]{"rdotnet_app", "--interactive", "--no-save", "--no-restore-data", "--max-ppsize=50000"}; //rdotnet_app --quiet --interactive --no-save --no-restore-data --max-mem-size=18446744073709551615 --max-ppsize=50000 GetFunction <R_setStartTime>()(); int R_argc = R_argv.Length; // Console.WriteLine("Initialize-R_setStartTime; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); if (NativeUtility.GetPlatform() == PlatformID.Win32NT) { // Attempted Fix for https://rdotnet.codeplex.com/workitem/110; not working // Tried to make sure that the command line options are taken into account. They are NOT effectively so via Rf_initialize_R only. // The problem is that cmdlineoptions assumes it is called by RGui.exe or RTerm.exe, and overrides R_HOME // GetFunction<R_set_command_line_arguments>()(R_argc, R_argv); // GetFunction<cmdlineoptions>()(R_argc, R_argv); } var status = GetFunction <Rf_initialize_R>()(R_argc, R_argv); if (status != 0) { throw new Exception("A call to Rf_initialize_R returned a non-zero; status=" + status); } // Console.WriteLine("Initialize-Rf_initialize_R; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); SetCstackChecking(); // following in RInside: may not be needed. //GetFunction<R_ReplDLLinit> () (); //this.parameter.Interactive = true; this.adapter.Install(this, this.parameter); //Console.WriteLine("Initialize-adapter installation; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); switch (NativeUtility.GetPlatform()) { case PlatformID.Win32NT: GetFunction <R_SetParams_Windows>("R_SetParams")(ref this.parameter.start); break; case PlatformID.MacOSX: case PlatformID.Unix: GetFunction <R_SetParams_Unix>("R_SetParams")(ref this.parameter.start.Common); //Console.WriteLine("Initialize-R_SetParams_Unix; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); break; } GetFunction <setup_Rmainloop>()(); //Console.WriteLine("Initialize-after setup_Rmainloop; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); // See comments in the first call to SetCstackChecking in this function as to why we (may) need it twice. SetCstackChecking(); this.isRunning = true; //Console.WriteLine("Initialize-just before leaving; R_CStackLimit value is " + GetDangerousInt32("R_CStackLimit")); // Partial Workaround (hopefully temporary) for https://rdotnet.codeplex.com/workitem/110 if (NativeUtility.GetPlatform() == PlatformID.Win32NT) { Evaluate(string.Format("invisible(memory.limit({0}))", (this.parameter.MaxMemorySize / 1048576UL))); } }
/// <summary> /// Gets a reference to the R engine, creating and initializing it if necessary. In most cases users need not provide any parameter to this method. /// </summary> /// <param name="dll">The file name of the library to load, e.g. "R.dll" for Windows. You usually do not need need to provide this optional parameter</param> /// <param name="initialize">Initialize the R engine after its creation. Default is true</param> /// <param name="parameter">If 'initialize' is 'true', you can optionally specify the specific startup parameters for the R native engine</param> /// <param name="device">If 'initialize' is 'true', you can optionally specify a character device for the R engine to use</param> /// <returns>The engine.</returns> /// <example> /// <p>A minimalist approach is to just call GetInstance</p> /// <code> /// REngine.SetEnvironmentVariables(); /// var engine = REngine.GetInstance(); /// engine.Evaluate("letters[1:26]"); /// </code> /// <p>In unusual circumstances you may need to elaborate on the initialization in a separate method call</p> /// <code> /// REngine.SetEnvironmentVariables(rPath=@"c:\my\peculiar\path\to\R\bin\x64"); /// var engine = REngine.GetInstance(initialize=false); /// StartupParameter sParams=new StartupParameter(){NoRenviron=true;}; /// ICharacterDevice device = new YourCustomDevice(); /// engine.Initialize(parameter: sParams, device: device); /// engine.Evaluate("letters[1:26]"); /// </code> /// </example> public static REngine GetInstance(string dll = null, bool initialize = true, StartupParameter parameter = null, ICharacterDevice device = null) { if (!environmentIsSet) // should there be a warning? and how? { SetEnvironmentVariables(); } if (engine == null) { engine = CreateInstance(EngineName, dll); if (initialize) { engine.Initialize(parameter, device); } } if (engine.Disposed) { throw new InvalidOperationException("The single REngine instance has already been disposed of (i.e. shut down). Multiple engine restart is not possible."); } return(engine); }
public virtual bool Close() { bool result; if (result = this.backend.NotNull() && this.backend.Close()) this.backend = null; return result; }
protected CharacterDevice(ICharacterDevice backend) { this.backend = backend; }
public CharacterDevice(ICharacterDevice backend, Action<char> onRead, Action<char> onWrite) : this(backend) { this.OnRead = onRead; this.OnWrite = onWrite; }
public static CharacterDevice Open(ICharacterDevice backend, Action<char> onRead, Action<char> onWrite) { CharacterDevice result = CharacterDevice.Open(backend); if (result.NotNull()) { result.OnRead += onRead; result.OnWrite += onWrite; } return result; }
public static CharacterDevice Open(ICharacterDevice backend) { return backend.NotNull() ? new CharacterDevice(backend) : null; }