-
Notifications
You must be signed in to change notification settings - Fork 129
/
REngine.cs
1237 lines (1124 loc) · 49.5 KB
/
REngine.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using RDotNet.Devices;
using RDotNet.Internals;
using RDotNet.NativeLibrary;
using RDotNet.Utilities;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using System.Text;
using System.Text.RegularExpressions;
namespace RDotNet
{
/// <summary>
/// REngine handles R environment through evaluation of R statement.
/// </summary>
/// <example>This example generates and outputs five random numbers from standard normal distribution.
/// <code>
/// Environment.SetEnvironmentVariable("PATH", @"C:\Program Files\R\R-2.12.0\bin\i386");
/// using (REngine engine = REngine.CreateInstance("RDotNet"))
/// {
/// engine.Initialize();
/// NumericVector random = engine.Evaluate("rnorm(5, 0, 1)").AsNumeric();
/// foreach (double r in random)
/// {
/// Console.Write(r + " ");
/// }
/// }
/// </code>
/// </example>
[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public class REngine : DynamicInterop.UnmanagedDll
{
/// <summary>
/// Flag for working on pre or post R 3.5 and its ALTREP mode.
/// </summary>
public enum CompatibilityMode
{
/// <summary>
/// Pre ALTREP includes all versions before R 3.5. This uses a 32-bit sxpinfo structure.
/// </summary>
PreALTREP = 0,
/// <summary>
/// ALTREP includes all versions R 3.5 and above. Core header structures were introduced in R 3.5 with
/// the ALTREP feature which required introducing the compability mode. It uses a 64-bit sxpinfo structure.
/// </summary>
ALTREP = 1
}
private static readonly ICharacterDevice DefaultDevice = new ConsoleDevice();
private readonly string id;
private CharacterDeviceAdapter adapter;
private bool isRunning;
private StartupParameter parameter;
private static bool environmentIsSet = false;
private static NativeUtility nativeUtil = null;
private static REngine engine = null;
// Type cache to allow faster dynamic casting
private static Type sexprecType = null;
private static Type symsxpType = null;
private static Type vectorSexprecType = null;
private static readonly char[] RDllVersionDelimiter = new[] {'.'};
/// <summary>
/// Create a new REngine instance
/// </summary>
/// <param name="id">The identifier of this object</param>
/// <param name="dll">The name of the file that is the shared R library, e.g. "R.dll"</param>
protected REngine(string id, string dll)
: base(dll)
{
this.id = id;
this.isRunning = false;
this.Disposed = false;
this.EnableLock = true; // See https://rdotnet.codeplex.com/workitem/113; it seems wise to enable it by default.
this.AutoPrint = false; // 2019-05 changing to false by default, as this impacts the default performance drastically. There was an argument for a true default, but now I things this is superseded.
}
/// <summary>
/// Gets/sets whether the call to Preserve and Unpreserve on symbolic expressions
/// should be using a lock to prevent thread concurrency issues. Default is false;
/// </summary>
/// <remarks>Thanks to gchapman for proposing the fix. See https://rdotnet.codeplex.com/workitem/67 for details</remarks>
public bool EnableLock { get; set; }
/// <summary>
/// Gets whether this instance is running.
/// </summary>
public bool IsRunning
{
get { return this.isRunning; }
}
/// <summary>
/// Gets the version of R.DLL.
/// </summary>
public string DllVersion
{
get
{
// As R's version definitions are defined in #define preprocessor,
// C# cannot access them dynamically.
// But, on Win32 platform, we can get the version string via getDLLVersion function.
if (Environment.OSVersion.Platform != PlatformID.Win32NT)
{
throw new NotImplementedException();
}
var getVersion = GetFunction<_getDLLVersion>("getDLLVersion");
return Marshal.PtrToStringAnsi(getVersion());
}
}
/// <summary>
/// Gets the ID of this instance.
/// </summary>
public string ID
{
get { return this.id; }
}
/// <summary>
/// Gets the R compatibility mode, based on the version of R used.
/// </summary>
public CompatibilityMode Compatibility { get; private set; }
/// <summary>
/// Gets the global environment.
/// </summary>
public REnvironment GlobalEnvironment
{
get
{
CheckEngineIsRunning();
return GetPredefinedSymbol("R_GlobalEnv").AsEnvironment();
}
}
private void CheckEngineIsRunning()
{
if (!IsRunning)
{
throw new InvalidOperationException("This engine is not running. You may have forgotten to call Initialize");
}
}
/// <summary>
/// Gets the root environment.
/// </summary>
public REnvironment EmptyEnvironment
{
get
{
CheckEngineIsRunning();
return GetPredefinedSymbol("R_EmptyEnv").AsEnvironment();
}
}
/// <summary>
/// Gets the base environment.
/// </summary>
public REnvironment BaseNamespace
{
get
{
CheckEngineIsRunning();
return GetPredefinedSymbol("R_BaseNamespace").AsEnvironment();
}
}
/// <summary>
/// Gets the <c>NULL</c> value.
/// </summary>
public SymbolicExpression NilValue
{
get
{
CheckEngineIsRunning();
return GetPredefinedSymbol("R_NilValue");
}
}
/// <summary>
/// Gets the unbound value.
/// </summary>
public SymbolicExpression UnboundValue
{
get
{
CheckEngineIsRunning();
return GetPredefinedSymbol("R_UnboundValue");
}
}
/// <summary>
/// Gets the name of the R engine instance (singleton).
/// </summary>
public static string EngineName { get { return "R.NET"; } }
/// <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>
/// Creates a new instance that handles R.DLL.
/// </summary>
/// <param name="id">ID.</param>
/// <param name="dll">The file name of the library to load, e.g. "R.dll" for Windows. You should usually not provide this optional parameter</param>
/// <returns>The engine.</returns>
private static REngine CreateInstance(string id, string dll = null)
{
if (id == null)
{
throw new ArgumentNullException("id", "Empty ID is not allowed.");
}
if (id == string.Empty)
{
throw new ArgumentException("Empty ID is not allowed.", "id");
}
dll = ProcessRDllFileName(dll);
var engine = new REngine(id, dll);
DetermineCompatibility(engine);
return engine;
}
private static void DetermineCompatibility(REngine engine)
{
if (engine == null)
{
return;
}
// If there is no DLL version information, we are going to start with an arbitrary default
// compatibility version to support R 3.5+
engine.Compatibility = CompatibilityMode.ALTREP;
if (NativeLibrary.NativeUtility.IsUnix)
// engine.DllVersion is not implemented because the R native library has no entry point to getDllVersion which is Windows only.
// Not sure yet if there is a way to programatically query the R version on Linux, without bumping in a chicken and egg problem.
return;
if (string.IsNullOrWhiteSpace(engine.DllVersion))
{
return;
}
var versionParts = engine.DllVersion.Split(RDllVersionDelimiter);
if (versionParts.Length < 2)
{
return;
}
int major = 0;
int minor = 0;
if (int.TryParse(versionParts[0], out major) && int.TryParse(versionParts[1], out minor))
{
// Pre-ALTREP is <= 3.4
if (major <= 3 && minor <= 4)
{
engine.Compatibility = CompatibilityMode.PreALTREP;
}
else
{
engine.Compatibility = CompatibilityMode.ALTREP;
}
}
}
/// <summary>
/// Gets the type of SEXPREC pre or post ALTREP
/// </summary>
/// <returns></returns>
public Type GetSEXPRECType()
{
if (sexprecType == null)
{
switch (Compatibility)
{
case CompatibilityMode.ALTREP:
sexprecType = typeof (RDotNet.Internals.ALTREP.SEXPREC);
break;
case CompatibilityMode.PreALTREP:
sexprecType = typeof (RDotNet.Internals.PreALTREP.SEXPREC);
break;
default:
throw new InvalidCastException("No SEXPREC type is available for this compatibility level");
}
}
return sexprecType;
}
/// <summary>
/// Gets the type of symsxp pre or post ALTREP
/// </summary>
/// <returns></returns>
public Type GetSymSxpType()
{
if (symsxpType == null)
{
switch (Compatibility)
{
case CompatibilityMode.ALTREP:
symsxpType = typeof(RDotNet.Internals.ALTREP.symsxp);
break;
case CompatibilityMode.PreALTREP:
symsxpType = typeof(RDotNet.Internals.PreALTREP.symsxp);
break;
default:
throw new InvalidCastException("No symsxp type is available for this compatibility level");
}
}
return symsxpType;
}
/// <summary>
/// Gets the type of VECTOR_SEXPREC pre or post ALTREP
/// </summary>
/// <returns></returns>
public Type GetVectorSexprecType()
{
if (vectorSexprecType == null)
{
switch (Compatibility)
{
case CompatibilityMode.ALTREP:
vectorSexprecType = typeof(RDotNet.Internals.ALTREP.VECTOR_SEXPREC);
break;
case CompatibilityMode.PreALTREP:
vectorSexprecType = typeof(RDotNet.Internals.PreALTREP.VECTOR_SEXPREC);
break;
default:
throw new InvalidCastException("No symsxp type is available for this compatibility level");
}
}
return vectorSexprecType;
}
/// <summary>
/// if the parameter is null or empty string, return the default names of the R shared library file depending on the platform
/// </summary>
/// <param name="dll">The name of the library provided, possibly null or empty</param>
/// <returns>A candidate for the file name of the R shared library</returns>
protected static string ProcessRDllFileName(string dll)
{
if (!string.IsNullOrEmpty(dll)) return dll;
return NativeUtility.GetRLibraryFileName();
}
static private string EncodeNonAsciiCharacters(string value)
{
StringBuilder sb = new StringBuilder();
foreach (char c in value)
{
if (c > 127)
{
string encodedValue = "\\u" + ((int)c).ToString("x4");
sb.Append(encodedValue);
}
else
{
sb.Append(c);
}
}
return sb.ToString();
}
/// <summary>
/// Perform the necessary setup for the PATH and R_HOME environment variables.
/// </summary>
/// <param name="rPath">The path of the directory containing the R native library.
/// If null (default), this function tries to locate the path via the Windows registry, or commonly used locations on MacOS and Linux</param>
/// <param name="rHome">The path for R_HOME. If null (default), the function checks the R_HOME environment variable. If none is set,
/// the function uses platform specific sensible default behaviors.</param>
/// <remarks>
/// This function has been designed to limit the tedium for users, while allowing custom settings for unusual installations.
/// </remarks>
public static void SetEnvironmentVariables(string rPath = null, string rHome = null)
{
environmentIsSet = true;
REngine.nativeUtil = new NativeUtility();
nativeUtil.SetEnvironmentVariables(rPath: rPath, rHome: rHome);
}
private static void resetCachedEnvironmentVariables()
{
if (environmentIsSet != true)
throw new Exception("resetCachedEnvironmentVariables cannot be called if the R environment variables were not first set");
nativeUtil.SetCachedEnvironmentVariables();
}
/// <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 ?? this.DefaultStartupParameter();
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);
if (NativeUtility.GetPlatform() == PlatformID.Win32NT)
// also workaround for https://github.com/rdotnet/rdotnet/issues/127 : R.dll is intent on overriding R_HOME and PATH even if --no-environ is specified...
resetCachedEnvironmentVariables();
// 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);
// also workaround for https://github.com/rdotnet/rdotnet/issues/127 : R.dll is intent on overriding R_HOME and PATH even if --no-environ is specified...
resetCachedEnvironmentVariables();
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"));
if (NativeUtility.GetPlatform() == PlatformID.Win32NT)
{
// also workaround for https://github.com/rdotnet/rdotnet/issues/127 : R.dll is intent on overriding R_HOME and PATH even if --no-environ is specified...
resetCachedEnvironmentVariables();
// Partial Workaround (hopefully temporary) for https://rdotnet.codeplex.com/workitem/110
Evaluate(string.Format("invisible(memory.limit({0}))", (this.parameter.MaxMemorySize / 1048576UL)));
}
}
private StartupParameter DefaultStartupParameter()
{
var p = new StartupParameter();
// to avoid https://github.com/rdotnet/rdotnet/issues/127 ?
p.NoRenviron = true;
p.LoadInitFile = false;
p.LoadSiteFile = false;
return p;
}
private static void currentEnvVars(out string path, out string rhome)
{
path = Environment.GetEnvironmentVariable("PATH");
rhome = Environment.GetEnvironmentVariable("R_HOME");
}
private void SetCstackChecking()
{
// Don't do any stack checking, see R Exts, '8.1.5 Threading issues',
// https://rdotnet.codeplex.com/discussions/462947
// https://rdotnet.codeplex.com/workitem/115
WriteInt32("R_CStackLimit", -1);
switch (NativeUtility.GetPlatform())
{
case PlatformID.MacOSX:
case PlatformID.Unix:
WriteInt32("R_SignalHandlers", 0);
// RInside does this for non-WIN32.
break;
}
}
/// <summary>
/// Creates the command line arguments corresponding to the specified startup parameters
/// </summary>
/// <param name="parameter"></param>
/// <returns></returns>
/// <remarks>While not obvious from the R documentation, it seems that command line arguments need to be passed
/// to get the startup parameters taken into account. Passing the StartupParameter to the API seems not to work as expected.
/// While this function may appear like an oddity to a reader, it proved necessary to the initialisation of the R engine
/// after much trial and error.</remarks>
public static string[] BuildRArgv(StartupParameter parameter)
{
var platform = NativeUtility.GetPlatform();
var argv = new List<string>();
argv.Add("rdotnet_app");
// Not sure whether I should add no-readline
//[MarshalAs(UnmanagedType.Bool)]
//public bool R_Quiet;
if (parameter.Quiet && !parameter.Interactive) argv.Add("--quiet"); // --quite --interactive to R embedded crashed...
//[MarshalAs(UnmanagedType.Bool)]
//public bool R_Slave;
if (parameter.Slave) argv.Add("--slave");
//[MarshalAs(UnmanagedType.Bool)]
//public bool R_Interactive;
if (platform != PlatformID.Win32NT) // RTerm.exe --help shows no such option; Unix only.
if (parameter.Interactive) argv.Add("--interactive");
//[MarshalAs(UnmanagedType.Bool)]
//public bool R_Verbose;
if (parameter.Verbose) argv.Add("--verbose");
//[MarshalAs(UnmanagedType.Bool)]
//public bool LoadSiteFile;
if (!parameter.LoadSiteFile) argv.Add("--no-site-file");
//[MarshalAs(UnmanagedType.Bool)]
//public bool LoadInitFile;
if (!parameter.LoadInitFile) argv.Add("--no-init-file");
//[MarshalAs(UnmanagedType.Bool)]
//public bool DebugInitFile;
//if (parameter.Quiet) argv.Add("--quiet");
//public StartupRestoreAction RestoreAction;
//public StartupSaveAction SaveAction;
//internal UIntPtr vsize;
//internal UIntPtr nsize;
//internal UIntPtr max_vsize;
//internal UIntPtr max_nsize;
//internal UIntPtr ppsize;
//[MarshalAs(UnmanagedType.Bool)]
//public bool NoRenviron;
if (parameter.NoRenviron) argv.Add("--no-environ");
switch (parameter.SaveAction)
{
case StartupSaveAction.NoSave:
argv.Add("--no-save");
break;
case StartupSaveAction.Save:
argv.Add("--save");
break;
}
switch (parameter.RestoreAction)
{
case StartupRestoreAction.NoRestore:
argv.Add("--no-restore-data");
break;
case StartupRestoreAction.Restore:
argv.Add("--restore");
break;
}
if (parameter.MaxMemorySize == (Environment.Is64BitProcess ? ulong.MaxValue : uint.MaxValue))
{
// This creates a nasty crash if using the default MaxMemorySize. found out in Rdotnet workitem 72
// do nothing
}
else
{
if (platform == PlatformID.Win32NT) // On unix, otherwise led to https://rdotnet.codeplex.com/workitem/137
argv.Add("--max-mem-size=" + parameter.MaxMemorySize);
}
argv.Add("--max-ppsize=" + parameter.StackSize);
return argv.ToArray();
}
/// <summary>
/// Forces garbage collection.
/// </summary>
public void ForceGarbageCollection()
{
GetFunction<R_gc>()();
}
/// <summary>
/// Gets a symbol defined in the global environment.
/// </summary>
/// <param name="name">The name.</param>
/// <returns>The symbol.</returns>
public SymbolicExpression GetSymbol(string name)
{
CheckEngineIsRunning();
return GlobalEnvironment.GetSymbol(name);
}
/// <summary>
/// Gets a symbol defined in the global environment.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
/// <returns>The symbol.</returns>
public SymbolicExpression GetSymbol(string name, REnvironment environment)
{
CheckEngineIsRunning();
if (environment == null)
{
environment = GlobalEnvironment;
}
return environment.GetSymbol(name);
}
/// <summary>
/// Assign a value to a name in the global environment.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="expression">The symbol.</param>
public void SetSymbol(string name, SymbolicExpression expression)
{
CheckEngineIsRunning();
GlobalEnvironment.SetSymbol(name, expression);
}
/// <summary>
/// Assign a value to a name in a specific environment.
/// </summary>
/// <param name="name">The name.</param>
/// <param name="expression">The symbol.</param>
/// <param name="environment">The environment. If <c>null</c> is passed, <see cref="GlobalEnvironment"/> is used.</param>
public void SetSymbol(string name, SymbolicExpression expression, REnvironment environment)
{
CheckEngineIsRunning();
if (environment == null)
{
environment = GlobalEnvironment;
}
environment.SetSymbol(name, expression);
}
/// <summary>
/// Evaluates a statement in the given string.
/// </summary>
/// <param name="statement">The statement.</param>
/// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
/// <returns>Last evaluation.</returns>
public SymbolicExpression Evaluate(string statement, REnvironment environment = null)
{
CheckEngineIsRunning();
return Defer(EncodeNonAsciiCharacters(statement), environment).LastOrDefault();
}
/// <summary>
/// Evaluates a statement in the given stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
/// <returns>Last evaluation.</returns>
public SymbolicExpression Evaluate(Stream stream, REnvironment environment = null)
{
CheckEngineIsRunning();
return Defer(stream, environment).LastOrDefault();
}
/// <summary>
/// Evaluates a statement in the given string.
/// </summary>
/// <param name="statement">The statement.</param>
/// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
/// <returns>Each evaluation.</returns>
private IEnumerable<SymbolicExpression> Defer(string statement, REnvironment environment = null)
{
CheckEngineIsRunning();
if (statement == null)
{
throw new ArgumentNullException();
}
using (TextReader reader = new StringReader(statement))
{
var incompleteStatement = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
foreach (var segment in Segment(line))
{
var result = Parse(segment, incompleteStatement, environment);
if (result != null)
{
yield return result;
}
}
}
}
}
/// <summary>
/// Evaluates a statement in the given stream.
/// </summary>
/// <param name="stream">The stream.</param>
/// <param name="environment">The environment in which to evaluate the statement. Advanced feature.</param>
/// <returns>Each evaluation.</returns>
public IEnumerable<SymbolicExpression> Defer(Stream stream, REnvironment environment = null)
{
CheckEngineIsRunning();
if (stream == null)
{
throw new ArgumentNullException();
}
if (!stream.CanRead)
{
throw new ArgumentException();
}
using (TextReader reader = new StreamReader(stream))
{
var incompleteStatement = new StringBuilder();
string line;
while ((line = reader.ReadLine()) != null)
{
foreach (var segment in Segment(line))
{
var result = Parse(segment, incompleteStatement, environment);
if (result != null)
{
yield return result;
}
}
}
}
}
private static IEnumerable<string> Segment(string line)
{
var segments = processInputString(line);
for (var index = 0; index < segments.Length; index++)
{
if (index == segments.Length - 1)
{
if (segments[index] != string.Empty)
{
yield return segments[index] + "\n";
}
}
else
{
yield return segments[index] + ";";
}
}
}
private static string[] processInputString(string input)
{
// Fixes for
// https://rdotnet.codeplex.com/workitem/165
// https://github.com/jmp75/rdotnet/issues/14
string[] lines = splitOnNewLines(input);
List<string> statements = new List<string>();
for (int i = 0; i < lines.Length; i++)
{
statements.AddRange(processLine(lines[i]));
}
return statements.ToArray();
}
private static string[] splitOnNewLines(string input)
{
input = input.Replace("\n\r", "\n");
return input.Split('\n');
}
private static string[] processLine(string line)
{
var trimmedLine = line.Trim();
if (trimmedLine == string.Empty)
return new string[] { };
if (trimmedLine.StartsWith("#"))
return new string[] { line };
string theRest;
string statement = splitOnFirst(line, out theRest, ';');
var result = new List<string>();
if (!statement.Contains("#"))
{
result.Add(statement);
result.AddRange(processLine(theRest));
}
else
{
// paste('this contains ### characters', " this too ###", 'Oh, and this # one too') # but "this" 'rest' is commented
// Find the fist # character such that before that, there is an
// even number of " and an even number of ' characters
int[] whereHash = IndexOfAll(statement, "#");
int firstComment = EvenStringDelimitors(statement, whereHash);
if (firstComment < 0)
// incomplete statement??? such as:
// paste('this is the # ', ' start of an incomplete # statement
{
result.Add(statement);
result.AddRange(processLine(theRest));
}
else
{
result.Add(statement.Substring(0, firstComment));
// firstComment is a valid comment marker - not need to process "the rest"
}
string restFirstStatement;
string beforeComment = splitOnFirst(statement, out restFirstStatement, '#');
}
return result.ToArray();
}
private static int EvenStringDelimitors(string statement, int[] whereHash)
{
for (int i = 0; i < whereHash.Length; i++)
{
var s = statement.Substring(0, whereHash[i]);
if (IsClosedString(s))
return whereHash[i];
}
return -1;
}
private static bool IsClosedString(string s)
{
// paste("#hashtag")
// paste("#hashtag''''")
// paste('#hashtag""""')
// paste('#hashtag""#""')
// paste('#hashtag""#""', "#hash ''' ")
bool inSingleQuote = false, inDoubleQuotes = false;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == '\'')
{
if (i > 0)
if (s[i - 1] == '\\')
continue;
if (inDoubleQuotes)
continue;
inSingleQuote = !inSingleQuote;
}
if (s[i] == '"')
{
if (i > 0)
if (s[i - 1] == '\\')
continue;
if (inSingleQuote)
continue;
inDoubleQuotes = !inDoubleQuotes;
}
}
return (!inSingleQuote) && (!inDoubleQuotes);
}
private static string splitOnFirst(string statement, out string rest, char sep)
{
var split = statement.Split(new[] { sep }, 2);
if (split.Length == 1)
rest = string.Empty;
else
rest = split[1];
return split[0];
}
/// <summary> Searches for the first all.</summary>
///
/// <param name="sourceString"> Source string.</param>
/// <param name="matchString"> The match string.</param>
///
/// <returns> The zero-based index of the found all, or -1 if no match was found.</returns>
private static int[] IndexOfAll(string sourceString, string matchString)
{
matchString = Regex.Escape(matchString);
var res = (from Match match in Regex.Matches(sourceString, matchString) select match.Index);
return res.ToArray();
}
private static string splitOnStatementSeparators(string line, out string theRest)
{
throw new NotImplementedException();
}
private SymbolicExpression Parse(string statement, StringBuilder incompleteStatement, REnvironment environment = null)
{
incompleteStatement.Append(statement);
var s = GetFunction<Rf_mkString>()(InternalString.NativeUtf8FromString(incompleteStatement.ToString()));
string errorStatement;
using (new ProtectedPointer(this, s))
{
ParseStatus status;
var vector = new ExpressionVector(this, GetFunction<R_ParseVector>()(s, -1, out status, NilValue.DangerousGetHandle()));
switch (status)
{
case ParseStatus.OK:
incompleteStatement.Clear();
if (vector.Length == 0)
{
return null;
}
using (new ProtectedPointer(vector))
{
SymbolicExpression result;
if (!vector.First().TryEvaluate((environment == null) ? GlobalEnvironment : environment, out result))
{
throw new EvaluationException(LastErrorMessage);
}
if (AutoPrint && !result.IsInvalid && GetVisible())
{
GetFunction<Rf_PrintValue>()(result.DangerousGetHandle());
}
return result;
}
case ParseStatus.Incomplete:
return null;
case ParseStatus.Error:
// TODO: use LastErrorMessage if below is just a subset
var parseErrorMsg = this.GetAnsiString("R_ParseErrorMsg");
errorStatement = incompleteStatement.ToString();
incompleteStatement.Clear();
throw new ParseException(status, errorStatement, parseErrorMsg);
default:
errorStatement = incompleteStatement.ToString();
incompleteStatement.Clear();
throw new ParseException(status, errorStatement, "");
}