public static void InvokeR()

in SamplesV1/RunRScriptUsingADFSample/InvokeRScript.cs [78:226]


        public static void InvokeR(string experimentType, string snapShotFile, string timeSeriesFile, string churnTagFile, string blobPath, string outputFile, IActivityLogger logger)
        {

            const string accountName = "<Provide Storage Account Name>";
            const string accountKey = "<Provide Storage Account Key>";
            var connectionString = String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", accountName, accountKey);

            var process = new Process();

            try
            {
                string pathToRExecutable;
                string[] blobNames;
                const string containerName = "hdiclustertest";

                //gen_ts_features.r is one of the files the R script requires. train.r and score.r are the R scripts themselves.
                var sourceFile = "gen_ts_features.r";
                if (experimentType.ToUpper().Equals("TRAINING"))
                {
                    pathToRExecutable = "train.r";
                    blobNames = new[] { sourceFile, pathToRExecutable, snapShotFile, timeSeriesFile, churnTagFile };
                }
                else
                {
                    pathToRExecutable = "score.r";
                    blobNames = new[] { sourceFile, pathToRExecutable, snapShotFile, timeSeriesFile };
                }

                var resultBlobPath = String.Format("{0}/{1}", containerName, blobPath);

                logger.Write("Creating working directory");
                logger.Write(String.Format("Machine Name: {0}", Environment.MachineName));

                var workingDirectory = new FileInfo(typeof(RExecutionActivity).Assembly.Location).DirectoryName;
                logger.Write(String.Format("Directory Name : {0}", workingDirectory));

                //var workingDirectory = CreateWorkingDirectory();
                logger.Write(String.Format("Working directory created: {0}", workingDirectory));

                DirectoryCopy(@"C:\apps\dist\R", workingDirectory, true);

                logger.Write("Downloading input files used by this sample to the Working Directory");

                var inputFileNames = DownloadInputFiles(workingDirectory, connectionString, resultBlobPath, blobNames);

                var index = 0;
                for (; index < inputFileNames.Length; index++)
                {
                    var file = inputFileNames[index];
                    if (File.Exists(file))
                    {
                        logger.Write(String.Format("File : {0} exists", file));
                    }
                }

                logger.Write("Input Files Download completed");

                sourceFile = inputFileNames[0];
                pathToRExecutable = inputFileNames[1];
                var ssFile = inputFileNames[2];
                var tsFile = inputFileNames[3];
                string args;
                var outputFileName = String.Format("{0}\\{1}", workingDirectory, outputFile);

                logger.Write(String.Format("Output file name : {0}", outputFileName));
                
                if (experimentType.ToUpper().Equals("TRAINING"))
                {
                    var tagFile = inputFileNames[4];
                    args = String.Format("{0} {1} {2} {3} {4}", sourceFile, tsFile, ssFile, tagFile, outputFileName);
                    logger.Write(String.Format("Arguments in training are : {0}", args));
                }
                else
                {
                    args = String.Format("{0} {1} {2} {3}", sourceFile, tsFile, ssFile, outputFileName);
                }

                /////R execution/////

                ProcessStartInfo startInfo = new ProcessStartInfo
                {
                    WindowStyle = ProcessWindowStyle.Hidden,
                    UseShellExecute = false,
                    RedirectStandardError = true,
                    RedirectStandardOutput = true,
                    CreateNoWindow = true
                };

                logger.Write(File.Exists(String.Format("{0}{1}", workingDirectory, @"\R-3.2.2\bin\x64\Rscript.exe"))
                    ? "R File exists"
                    : "R file does not exist");

                startInfo.FileName = String.Format("{0}{1}", workingDirectory, @"\R-3.2.2\bin\x64\Rscript.exe");
                startInfo.Arguments = String.Format("{0} {1}", pathToRExecutable, args);
                if (workingDirectory != null) startInfo.WorkingDirectory = workingDirectory;
                logger.Write("R Execution started");
                process.StartInfo = startInfo;
                process.Start();

                logger.Write(String.Format("Process started with process id : {0} on machine : {1}", process.Id, process.MachineName));

                var errorReader = process.StandardError;
                var outputReader = process.StandardOutput;
                
                while (!outputReader.EndOfStream)
                {
                    var text = outputReader.ReadLine();
                    logger.Write(text);
                }

                logger.Write("output reader complete");

                while (!errorReader.EndOfStream)
                {
                    errorReader.ReadLine();
                }

                logger.Write(String.Format("Standard Output : {0}", process.StandardOutput.ReadToEnd()));
                logger.Write(String.Format("Standard Error: {0}", process.StandardError.ReadToEnd()));

                logger.Write("output reader end of stream complete");

                process.WaitForExit();

                while (!process.HasExited)
                {
                    logger.Write("R is still running");
                }

                logger.Write(String.Format("Process start time : {0}, end time : {1}", process.StartTime, process.ExitTime));

                /////Upload file/////
                if (File.Exists(outputFileName))
                {
                    logger.Write("Uploading file started");

                    UploadFile(connectionString, resultBlobPath, outputFileName, outputFile);
                }
                else
                {
                    logger.Write("output file not found");
                }
            }
            catch (Exception ex)
            {
                logger.Write(String.Format("Exception is : {0}", ex.Message));
            }

        }