Example #1
0
        static public void Execute()
        {
            Console.WriteLine("AnonProjectExecuteScript - start");

            //
            // 1. Connect to the DeployR Server
            //
            RClient rClient = Utility.Connect();

            //
            // 2. Execute a public analytics Web service as an anonymous
            // user based on a repository-managed R script:
            // /testuser/root/DeployR - Hello World.R
            //
            // Create the AnonymousProjectExecutionOptions object
            // to specify inputs and output to the script
            // The R object that is an input to the script is 'input_randomNum'
            // The R object that we want to retrieve after script execution is 'x'
            //

            AnonymousProjectExecutionOptions options = new AnonymousProjectExecutionOptions();

            options.rinputs.Add(RDataFactory.createNumeric("input_randomNum", 100));
            options.routputs.Add("x");

            exec = rClient.executeScript("DeployR - Hello World",
                                         "root",
                                         "testuser",
                                         "",
                                         options);

            Console.WriteLine("AnonProjectExecuteScript: public repository-managed " +
                              "script execution completed, exec=" + exec);

            //
            // 3. Retrieve script execution results.
            //
            console = exec.about().console;
            plots   = exec.about().results;
            files   = exec.about().artifacts;
            objects = exec.about().workspaceObjects;

            RNumericVector xVec = (RNumericVector)objects[0];

            Console.WriteLine("AnonProjectExecuteScript - end");
        }
Example #2
0
        static public void Execute()
        {
            Console.WriteLine("AuthProjectWorkspace - start");

            //
            // 1. Connect to the DeployR Server
            //
            RClient rClient = Utility.Connect();

            //
            // 2. Authenticate the user
            //
            RUser rUser = Utility.Authenticate(rClient);

            //
            //  3. Create a temporary project (R session).
            //
            //  Optionally:
            //  ProjectCreationOptions options = new ProjectCreationOptions();
            //
            //  Populate options as needed, then:
            //
            //  rProject = rUser.createProject(options);
            //
            RProject rProject = rUser.createProject();

            Console.WriteLine("AuthProjectWorkspace: created temporary " +
                              "R session, rProject=" + rProject);

            //
            // 4. Execute a block of R code to create an object
            // in the R session's workspace.
            //
            String rCode = "x <- T";

            exec = rProject.executeCode(rCode);

            //
            // 5. Retrieve the object "x" from the R session's workspace.
            //
            RData encodedX = rProject.getObject("x");

            if (encodedX is RBoolean)
            {
                Console.WriteLine("retrieved object x from workspace, x=" + (Boolean)encodedX.Value);
            }

            //
            // 6. Create R object data in the R sesssion's workspace
            // by pushing DeployR-encoded data from the client application.
            //
            // - Prepare sample R object vector data.
            // - Use RDataFactory to encode the sample R object vector data.
            // - Push encoded R object into the workspace.
            //
            List <Double?> vectorValues = new List <Double?>();

            vectorValues.Add(10.0);
            vectorValues.Add(11.1);
            vectorValues.Add(12.2);
            vectorValues.Add(13.3);
            vectorValues.Add(14.4);

            RData encodedY = RDataFactory.createNumericVector("y", vectorValues);

            rProject.pushObject(encodedY);

            //
            // 7. Retrieve the DeployR-encoding of the R object
            // from the R session's workspace.
            //
            encodedY = rProject.getObject("y");

            if (encodedY is RNumericVector)
            {
                List <Double?> numVectorValues = (List <Double?>)encodedY.Value;
                StringBuilder  str             = new StringBuilder();
                foreach (Double?val in numVectorValues)
                {
                    str.Append(val + " ");
                }
                Console.WriteLine("retrieved object y from workspace, encodedY=" + str.ToString());
            }

            //
            // 8. Retrieve a list of R objects in the R session's workspace.
            //
            // Optionally:
            // ProjectWorkspaceOptions options = new ProjectWorkspaceOptions();
            //
            // Populate options as needed, then:
            //
            // objs = rProject.listObjects(options);
            //
            ///
            objs = rProject.listObjects();

            //
            // 9. Cleanup
            //
            rProject.close();
            Utility.Cleanup(rUser, rClient);

            Console.WriteLine("AuthProjectWorkspace - end");
        }
        /*
         * RTaskAppSimulator method.
         */

        public void simulateApp(RBroker rBroker)
        {
            /*
             * 2. Submit task(s) to RBroker for execution.
             */

            Console.WriteLine("About to simulate " +
                              SIMULATE_TOTAL_TASK_COUNT + " tasks at a rate of " +
                              SIMULATE_TASK_RATE_PER_MINUTE + " tasks per minutes.");

            simulationStartTime = System.Environment.TickCount;

            for (int tasksPushedToBroker = 0;
                 tasksPushedToBroker < SIMULATE_TOTAL_TASK_COUNT;
                 tasksPushedToBroker++)
            {
                try {
                    /*
                     * 1. Prepare RTask for real-time scoring.
                     *
                     * In this example, we pass along a unique
                     * customer ID with each RTask. In a real-world
                     * application the input parameters on each RTask
                     * will vary depending on need, such as customer
                     * database record keys and supplimentary parameter
                     * data to facilitate the scoring.
                     */

                    PooledTaskOptions taskOptions = new PooledTaskOptions();
                    taskOptions.routputs.Add("score");
                    taskOptions.rinputs.Add(RDataFactory.createNumeric("customerid", tasksPushedToBroker));

                    RTask rTask = RTaskFactory.pooledTask(Program.TUTORIAL_RTSCORE_SCRIPT,
                                                          Program.TUTORIAL_REPO_DIRECTORY,
                                                          Program.TUTORIAL_REPO_OWNER,
                                                          null, taskOptions);

                    RTaskToken taskToken = rBroker.submit(rTask);
                    Console.WriteLine("Submitted task " + rTask + "\n");

                    /*
                     * If further tasks need to be pushed to broker
                     * then delay for staggeredLoadInterval to simulate
                     * control of task flow rate.
                     */
                    if (tasksPushedToBroker < (SIMULATE_TOTAL_TASK_COUNT - 1))
                    {
                        try {
                            if (SIMULATE_TASK_RATE_PER_MINUTE != 0L)
                            {
                                int staggerLoadInterval = 60 / SIMULATE_TASK_RATE_PER_MINUTE;
                                Thread.Sleep(staggerLoadInterval * 1000);
                            }
                        }
                        catch (Exception iex)
                        {
                            Console.WriteLine("Runtime exception=" + iex.ToString());
                        }
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Runtime exception=" + ex.ToString());
                }
            }
        }