创建 R 部件

如何创建自定义 R 部件以在分析中使用。

创建 R 部件之前,请确保满足以下要求:
  • R 脚本以有效的 R 函数格式编写。
  • R 脚本在 R GUI 控制台中执行。
  • R 脚本至少包含一个主函数。
  • 在用户的计算机或 SAP HANA 服务器上安装运行 R 脚本所需的包。
  • 为数据库内分析编写的 R 脚本返回数据框架。
以下是编写 R 脚本时应考虑的最佳做法:
  • 为进程内分析编写的 R 脚本返回数据框架。
  • 建议对输出进行类型装换;例如,如果一个列的值为数值,请将其标注为“as.numeric(output)”
  • 对于 R 脚本中使用的类别变量,请使用 “as.factor” 命令指定变量。
  1. 在右侧部件列表下的“预测”空间中,选择 导航路径起点 add_component.gif 下一导航步骤 ““R 部件”” 导航路径终点
    将显示“创建新的自定义 R 部件”向导。
  2. “常规”页面上,输入以下信息:
    1. “部件名称”文本框中,输入 My component
    2. “部件类型”下拉列表中选择“算法”
    3. “部件说明”文本框中,键入 R component for Simple Linear Regression
  3. 选择“下一步”
    将出现“脚本”页面。
  4. “脚本”页面,选择“Load Script”(加载脚本)以选择要上载的文件。
    注意 可在文本框中编写或复制并粘贴以下示例 R 脚本。
    注意 请参阅以下 R 函数格式的注释,以帮助您了解和编写您自己的 R 脚本。
    #The following is a sample script for a simple linear regression component.
    #You must write the script in a valid R function format.
    #Note that the function name and variable name in R script can be user-defined, and are supported in R.
    #The following is the argument description for the primary function SLR:
    #InputDataFrame: Dataframe in R that contains the output of the parent component.
    #The following two parameters are received from the user through the property view:
    #IndependentColumn: Column name that you want to use as independent variable for the component.
    #DependentColumn; Column name that you want to use as a dependent variable for the component.
    
    SLR<-function(InputDataFrame, IndependentColumn, DependentColumn)
    {
      finalString<-paste(paste(DependentColumn,"~" ), IndependentColumn); 
    #Formatting the final string to
    #pass to "lm" function
    slr_model<-lm(finalString); # calling the "lm" function and storing the output model in "slr_model"
    #To get the predicted values for the Training data set, call the "predict" function with this model and
    #input dataframe, which is represented by "InputDataFrame".
    result<-predict(slr_model, InputDataFrame); # Storing the predicted values in the "result" variable.
    output<- cbind(InputDataFrame, result); # combining "InputDataFrame" and "result" to get the final table.
    plot(slr_model); #Plotting model visualization.
    #returnvalue: function must always return a list that contains results "out", and model variable
    #"slrmodel", if present.
    #The output variable stores the final result.
    #The model variable is used for model scoring.
    return (list(slrmodel=slr_model, out=output))
    }
    
    #The following is the argument description for the model scoring function "SLRModelScoring":
    #InputDataFrame: Dataframe in R that contains the output of the parent component.
    #IndependentColumn: Column name to be used as independent variables for the component.
    #Model: Model variable that is used for scoring.
    
    SLRModelScoring <- function (InputDataFrame, IndependentColumn, Model)
    {
    #Calling "predict" function to get the predictive value with "Model " and "InputDataFrame".
    predicted<-predict(Model, data.frame(InputDataFrame[,IndependentColumn]), level=0.95);
    # Combining “InputDataFrame” and “predicted” to get the final table.
    output <- cbind(InputDataFrame, predicted); 
    #returnvalue: function should always return a list that contains the result ("model result"),
    #The output variable stores the final result
    return(list(modelresult=output))
    }
    

    R 脚本转换为可被 Expert Analytics 识别的有效 R 函数格式的两个示例如下所示:

    R 脚本 R 函数格式(可被 Expert Analytics 识别)
     dataFrame<-read.csv("C:\\CSVs\\Iris.csv")
     attach(dataFrame)
     set.seed(4321)
     kmeans_model<-
     kmeans(data.frame(`SepalLength`,`SepalWidth`,
     `PetalLength`,`PetalWidth`),
     centers=5,iter.max=100,nstart=1,algorithm=
     "Hartigan-Wong")
     kmeans_model$cluster
    kmeansfunction<-function(dataFrame,independent,
     Clustersize,Iterations,algotype,numberofinitialdsets)
     { 
     set.seed(4321)
     kmeans_model<-kmeans(data.frame(dataFrame[,independent]),
     centers=Clustersize,iter.max=Iterations, nstart=numberofinitialdsets,
     algorithm= algotype)
     output<- cbind(dataFrame, kmeans_model$cluster);
     boxplot(output); return (list(out=output));
     }
    dataFrame<-
     read.csv("C:\\Datasets\\cnr\\Iris.csv") 
     attach(dataFrame) library(rpart)
     cnr_model<-rpart
     (Species~PetalLength+PetalWidth+SepalLength+
     SepalWidth, method="class") library(rpart) 
     predict(cnr_model, dataFrame,type = c("class"))
    cnrFunction<-function(dataFrame,IndependentColumns,dep)
     { 
     library(rpart); 
     formattedString<-paste(IndependentColumns, collapse = '+');
     finalString<-paste(paste(dep, "~" ),
     formattedString); cnr_model<-rpart(finalString, method="class");
     output<- predict(cnr_model, dataFrame,type=c("class")); 
     out<- cbind(dataFrame, output);
     return (list(result=out,modelcnr=cnr_model));
     } 
     cnrFunctionmodel<-function(dataFrame,ind,modelcnr,type)
     {
     output<-predict(modelcnr,data.frame(dataFrame[,ind]),type=type);
     out<- cbind(dataFrame, output); return (list(result=out));
    注意

    在主函数中声明模型评分函数的参数,“输入数据框架”和“输入模型变量名称”除外,用户从下拉列表中选择这两个参数。

  5. “主函数详细信息”区域中,输入以下信息:
    1. “主函数名称”下拉列表中,选择“SLR”
    2. “输入数据框架”下拉列表中,选择“InputDataFrame”
    3. “输出数据框架”框中,输入 out
    4. 选择“保存模型的选项”复选框。
      “模型变量名称”字段已启用,并出现“模型计分函数详细信息”
    5. “模型变量名称”字段中,输入 slrmodel
    6. 选择“显示汇总”“导出为 PMML 的选项”复选框。
  6. “模型计分函数详细信息”区域中,输入以下信息:
    1. “模型计分函数名称”中,选择“SLRModelScoring”
    2. “输入数据框架”下拉列表中,选择“MInputDataFrame”
    3. “输出数据框架”字段中,输入 modelresult
    4. “输入模型变量名称”下拉列表中,选择“模型”
  7. 选择“下一步”
    将出现“设置”页面。
  8. “主函数设置”“输出表定义”区域中,执行以下子步骤:
    1. 选择“不考虑任何列”
    2. “数据类型”下拉列表中,选择“整数”
    3. “新预测列的名称”框中,输入 Predicted column
  9. “属性视图定义”区域中,执行以下子步骤:
    1. “属性显示名称”“独立列”框中,输入 Independent Column
    2. “控件类型”下拉列表中,选择“列选择器”作为“独立列”的控件类型。
    3. “属性显示名称”“独立列”框中,输入 Dependent Column
    4. “控件类型”下拉列表中,选择“列选择器”作为“从属列”的控件类型。
  10. “模型计分设置”区域的“输出表定义”中,选择“考虑先前部件的所有列”
  11. “数据类型”下拉列表中,选择“整数”
  12. “新预测列的名称”中,输入“输出列”
  13. “属性视图定义”区域中,执行以下子步骤:
    1. “属性显示名称”中,输入 Independent column
    2. “控件类型”下拉列表中,选择“列选择器”作为“独立列”的控件类型。
  14. 选择“完成”
根据已执行的分析类型,可像其他任何部件一样创建模型。