Exemple #1
0
        public static SymbolicExpression ExecFunction(this REngine engine, SymbolicExpressionParamList expressionParams, string functionName)
        {
            var paramValue = engine.GetFunction(expressionParams);
            var function   = $"{functionName}({paramValue})";

            return(engine.Evaluate(function));
        }
Exemple #2
0
        private static string GetFunction(this REngine engine, SymbolicExpressionParamList expressionParams)
        {
            var paramValue = string.Empty;

            if (expressionParams != null && expressionParams.Count > 0)
            {
                var isUseName = expressionParams.IsUseParamName;
                var sb        = new StringBuilder();
                var paramName = string.Empty;

                for (int i = 0; i < expressionParams.Count; i++)
                {
                    var item = expressionParams[i];
                    paramName = item.ParamName;

                    var paramIsNull = string.IsNullOrWhiteSpace(paramName);

                    if (isUseName)
                    {
                        if (paramIsNull)
                        {
                            throw new Exception("参数名称不能为空,");
                        }

                        if (item.SymbolicExpression != null && string.IsNullOrWhiteSpace(item.Where))
                        {
                            sb.Append($"{paramName}={paramName},");
                        }

                        if (item.SymbolicExpression == null && !string.IsNullOrWhiteSpace(item.Where))
                        {
                            sb.Append($"{paramName}={item.Where},");
                        }
                    }
                    else
                    {
                        if (item.SymbolicExpression != null && string.IsNullOrWhiteSpace(item.Where))
                        {
                            if (paramIsNull)
                            {
                                paramName = "x" + i;
                            }
                            sb.Append($"{paramName},");
                        }

                        if (item.SymbolicExpression == null && !string.IsNullOrWhiteSpace(item.Where))
                        {
                            sb.Append($"{item.Where},");
                        }
                    }
                    if (string.IsNullOrWhiteSpace(item.Where))
                    {
                        engine.SetSymbol(paramName, item.SymbolicExpression);
                    }
                }
                paramValue = sb.ToString().TrimEnd(',');
            }

            return(paramValue);
        }
Exemple #3
0
        static void Main(string[] args)
        {
            REngine.SetEnvironmentVariables();
            // There are several options to initialize the engine, but by default the following suffice:
            REngine engine = REngine.GetInstance();

            //engine.Birth_of_age();

            //engine.Age_title_colours();

            //engine.Barchart();
            //engine.barchart_months_revenue();
            //engine.Barchart_stacked();

            engine.SetWd();
            engine.GetWd();
            //engine.Print("开始安装exec组件");
            //engine.Evaluate("install.packages('xlsx')");
            //engine.Print("结束安装exec组件");


            engine.Print("读取文件开始");

            //var csvName = engine.ReadCsv("InputName.csv");

            //var param = new SymbolicExpressionParamList() { IsUseParamName = false };
            //param.Add(new SymbolicExpressionParam(csvName));
            //param.Add(new SymbolicExpressionParam(where: " as.Date(start_date) > as.Date('2014-01-01') "));
            ////param.Add(new SymbolicExpressionParam(where: "salary>600&dept=='IT'"));
            //var subset = engine.ExecFunction(param, "subset");

            //var paramA = new SymbolicExpressionParamList() { IsUseParamName = false };
            //paramA.Add(new SymbolicExpressionParam(subset.AsDataFrame()));
            //paramA.Add(new SymbolicExpressionParam(where: "'output1.csv'"));
            //paramA.Add(new SymbolicExpressionParam(where: "row.names = FALSE"));
            //engine.ExecFunction(paramA, "write.csv");
            //engine.ReadCsv("output1.csv");

            engine.Evaluate("library(xlsx) ");
            var paramA = new SymbolicExpressionParamList()
            {
                IsUseParamName = false
            };

            paramA.Add(new SymbolicExpressionParam(where : "'input.xlsx'"));
            paramA.Add(new SymbolicExpressionParam(engine.CreateNumeric(1), paramName: "sheetIndex"));
            engine.ExecFunction(paramA, "read.xlsx");
            //engine.ReadCsv("output1.csv");

            engine.Print("读取文件结束");

            engine.Dispose();

            Console.ReadKey();
        }
Exemple #4
0
        /// <summary>
        /// 传入函数名称就可以执行函数
        /// </summary>
        /// <param name="engine"></param>
        /// <param name="symbolicExpression"></param>
        /// <param name="functionName"></param>
        /// <returns></returns>
        public static SymbolicExpression ExecFunction(this REngine engine, SymbolicExpression symbolicExpression, string functionName)
        {
            var expressionPar = new SymbolicExpressionParamList()
            {
                IsUseParamName = false,
            };
            var param = new SymbolicExpressionParam(symbolicExpression, "");

            expressionPar.Add(param);
            return(engine.ExecFunction(expressionPar, functionName));
        }