/// <summary> /// Dispose of this REngine, including using the native R API to clean up, if the parameter is true /// </summary> /// <param name="disposing">if true, release native resources, using the native R API to clean up.</param> protected override void Dispose(bool disposing) { this.isRunning = false; OnDisposing(EventArgs.Empty); if (disposing && !Disposed) { GetFunction <R_RunExitFinalizers>()(); GetFunction <Rf_CleanEd>()(); GetFunction <R_CleanTempDir>()(); Disposed = true; } if (disposing && this.adapter != null) { this.adapter.Dispose(); this.adapter = null; } if (Disposed) { return; } GC.KeepAlive(this.parameter); base.Dispose(disposing); }
/// <summary> /// Dispose of this REngine, including using the native R API to clean up, if the parameter is true /// </summary> /// <param name="disposing">if true, release native resources, using the native R API to clean up.</param> protected override void Dispose(bool disposing) { this.isRunning = false; OnDisposing(EventArgs.Empty); if (disposing && !Disposed) { GetFunction<R_RunExitFinalizers>()(); GetFunction<Rf_CleanEd>()(); GetFunction<R_CleanTempDir>()(); Disposed = true; } if (disposing && this.adapter != null) { this.adapter.Dispose(); this.adapter = null; } if (Disposed) return; GC.KeepAlive(this.parameter); base.Dispose(disposing); }
/// <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> /// 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))); } }