示例#1
0
        public bool RemovePackage(string packageName)
        {
            var logger        = DependencyFactory.Resolve <ILogger>();
            var userDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "DEARPackages").Replace("\\", "/");

            if (!Directory.Exists(userDirectory))
            {
                Directory.CreateDirectory(userDirectory);
            }
            _engine.Evaluate($@".libPaths(""{userDirectory}"")");
            _engine.Evaluate(@".libPaths()");
            _engine.Evaluate(string.Format(CultureInfo.InvariantCulture, @"remove.packages(""{0}"")", packageName));

            logger.LogInformation(string.Format(CultureInfo.InvariantCulture, @"Removed R package ""{0}""", packageName));

            return(true);
        }
示例#2
0
        public bool InstallPackage(string packagePath)
        {
            var logger        = DependencyFactory.Resolve <ILogger>();
            var userDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "DEARPackages").Replace("\\", "/");

            if (!Directory.Exists(userDirectory))
            {
                Directory.CreateDirectory(userDirectory);
            }
            _engine.Evaluate($@".libPaths(""{userDirectory}"")");
            _engine.Evaluate(@".libPaths()");
            _engine.Evaluate(string.Format(CultureInfo.InvariantCulture,
                                           @"install.packages(""{0}"", dependencies = TRUE, repos = NULL, verbose = TRUE, type = ""win.binary"")",
                                           packagePath.Replace(@"\", @"\\")));

            logger.LogInformation(string.Format(CultureInfo.InvariantCulture, @"Installed R package ""{0}""", packagePath));

            return(true);
        }
        public static bool ParseArguments(string[] args, CommandLineOptions options)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }

            var logger = DependencyFactory.Resolve <ILogger>();

            if (!Parser.Default.ParseArguments(args, options))
            {
                return(false);
            }

            if (!File.Exists(options.SchemaBinaryPath))
            {
                logger.LogInformation(string.Format(CultureInfo.InvariantCulture, "Schema binary {0} not found",
                                                    options.SchemaBinaryPath));
                return(false);
            }

            if (!string.IsNullOrEmpty(options.RPackagePath) && !File.Exists(options.RPackagePath))
            {
                logger.LogInformation(string.Format(CultureInfo.InvariantCulture, "R Package {0} not found",
                                                    options.RPackagePath));
                return(false);
            }

            if (!string.IsNullOrEmpty(options.TypeMapJsonPath) && !File.Exists(options.TypeMapJsonPath))
            {
                logger.LogInformation(string.Format(CultureInfo.InvariantCulture, "Type Map JSON file {0} not found",
                                                    options.TypeMapJsonPath));
                return(false);
            }

            return(true);
        }
示例#4
0
        public void Start()
        {
            try
            {
                using (ServiceHost host = new ServiceHost(typeof(R)))
                {
                    host.Closed  += HostOnClosed;
                    host.Faulted += HostOnFaulted;
                    host.Opened  += HostOnOpened;

                    host.Open();
                    Console.ReadLine();
                    host.Close();
                }
            }
            catch (Exception e)
            {
                DependencyFactory.Resolve <ILogger>().LogInformation(string.Format(CultureInfo.InvariantCulture,
                                                                                   "RInterop hit an unexpected exception. Exception: {0}", e));
            }
        }
示例#5
0
        public void Start(string[] args)
        {
            if (args == null)
            {
                throw new ArgumentNullException(nameof(args));
            }

            _logger.LogInformation(string.Format(CultureInfo.InvariantCulture, "Working directory: {0}",
                                                 Environment.CurrentDirectory));
            _logger.LogInformation(string.Format(CultureInfo.InvariantCulture, "Arguments: {0}", string.Join(" ", args)));

            var options = new CommandLineOptions();

            if (!CommandLineOptions.ParseArguments(args, options))
            {
                _logger.LogError(string.Format(CultureInfo.InvariantCulture, "Invalid arguments {0}",
                                               string.Join(" ", args)));
                LogUsage();
                return;
            }

            Config.SchemaBinaryPath = options.SchemaBinaryPath;
            Assembly assembly = Assembly.LoadFrom(Config.SchemaBinaryPath);

            if (!string.IsNullOrEmpty(options.RPackagePath))
            {
                try
                {
                    var filename = Path.GetFileName(options.RPackagePath);
                    if (!string.IsNullOrEmpty(filename))
                    {
                        Config.RPackageName = filename.Substring(0, filename.IndexOf("_", StringComparison.Ordinal));
                    }
                }
                catch (Exception exception)
                {
                    _logger.LogError(string.Format(CultureInfo.InvariantCulture,
                                                   "Exiting. Exception while extracting R package name: {0}", exception));
                    LogUsage();
                    return;
                }
            }

            if (!string.IsNullOrEmpty(options.TypeMapJsonPath))
            {
                Config.SerializationTypeMap = new SerializationTypeMap();
                if (!string.IsNullOrEmpty(options.TypeMapJsonPath))
                {
                    TypeMap o =
                        JsonConvert.DeserializeObject <TypeMap>(new StreamReader(options.TypeMapJsonPath).ReadToEnd());

                    foreach (Map i in o.Mapping)
                    {
                        DependencyFactory.Resolve <ILogger>()
                        .LogInformation(string.Format(CultureInfo.InvariantCulture,
                                                      "Got input type mapping: {0} > {1} > {2}", i.Function, i.InputType, i.OutputType));
                        Config.SerializationTypeMap.InputTypeMap[i.Function] = assembly
                                                                               .GetTypes()
                                                                               .First(a => a.FullName.Equals(i.InputType));
                        Config.SerializationTypeMap.OutputTypeMap[i.Function] = assembly
                                                                                .GetTypes()
                                                                                .First(a => a.FullName.Equals(i.OutputType));
                    }
                }
            }

            using (var service = new Service())
            {
                service.Start();
                service.StartedEvent.WaitOne();
            }
        }
示例#6
0
 public R() : this(DependencyFactory.Resolve <ILogger>())
 {
     _engine = REngineWrapper.REngine;
 }
示例#7
0
        public static void InstallPackages(string rPackagePath)
        {
            var engine        = REngine;
            var logger        = DependencyFactory.Resolve <ILogger>();
            var userDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "DEARPackages").Replace("\\", "/");

            if (!Directory.Exists(userDirectory))
            {
                Directory.CreateDirectory(userDirectory);
            }
            engine.Evaluate($@".libPaths(""{userDirectory}"")");
            engine.Evaluate(@".libPaths()");
            engine.Evaluate(string.Format(CultureInfo.InvariantCulture,
                                          @"install.packages(""{0}"", verbose = TRUE, dependencies = TRUE, type = ""win.binary"")",
                                          rPackagePath.Replace(@"\", @"\\")));

            logger.LogInformation(string.Format(CultureInfo.InvariantCulture, @"Installed R package ""{0}""", rPackagePath));

            List <string> imports;

            try
            {
                imports = engine
                          .Evaluate(string.Format(CultureInfo.InvariantCulture,
                                                  @"packageDescription(""{0}"")$Imports",
                                                  Config.RPackageName))
                          .AsCharacter()
                          .FirstOrDefault()?
                          .Replace(" ", string.Empty)
                          .Split(',')
                          .ToList();
            }
            catch (Exception e)
            {
                logger.LogInformation(string.Format(CultureInfo.InvariantCulture,
                                                    @"Could not get the dependent packages from provided package. Please manually install package using the ""install.packages"" command. Exception: {0}",
                                                    e));
                throw;
            }

            if (imports == null)
            {
                return;
            }

            foreach (string packageName in imports)
            {
                logger.LogInformation(string.Format(CultureInfo.InvariantCulture,
                                                    @"Installing dependent R package ""{0}""",
                                                    packageName));

                try
                {
                    engine.Evaluate(string.Format(CultureInfo.InvariantCulture,
                                                  @"if (!require(""{0}"")) 
install.packages(""{0}"", repo = ""https://cran.rstudio.com/"", dependencies = TRUE)",
                                                  packageName));
                    engine.Evaluate(string.Format(CultureInfo.InvariantCulture,
                                                  @"if (!require(""{0}"")) stop(""Package {0} did not install correctly."")",
                                                  packageName));
                }
                catch (Exception e)
                {
                    logger.LogInformation(string.Format(CultureInfo.InvariantCulture,
                                                        @"Could not automatically install dependent package ""{0}"". Please manually install R package using the following command: install.packages(""{0}"")
Exception: {1}",
                                                        packageName,
                                                        e));
                    throw;
                }

                logger.LogInformation(string.Format(CultureInfo.InvariantCulture, @"Installed dependent R package ""{0}""", packageName));
            }
        }
示例#8
0
 private void HostOnOpened(object sender, EventArgs eventArgs)
 {
     DependencyFactory.Resolve <ILogger>().LogInformation("Service is available. Press <ENTER> to exit.");
     DependencyFactory.Resolve <ILogger>().LogInformation(string.Format(CultureInfo.InvariantCulture, @"R Path : ""{0}""", new RDotNet.NativeLibrary.NativeUtility().FindRPath()));
     _startedEvent.Set();
 }
示例#9
0
 private void HostOnFaulted(object sender, EventArgs eventArgs)
 {
     DependencyFactory.Resolve <ILogger>().LogInformation("RInterop is in faulted state.");
 }
示例#10
0
 private void HostOnClosed(object sender, EventArgs eventArgs)
 {
     DependencyFactory.Resolve <ILogger>().LogInformation("RInterop has shutdown.");
 }
示例#11
0
 private void HostOnOpened(object sender, EventArgs eventArgs)
 {
     DependencyFactory.Resolve <ILogger>().LogInformation("Service is available. Press <ENTER> to exit.");
     _startedEvent.Set();
 }