StoredProcedure: SQL Server ストアド プロシージャ: クラス ジェネレーター

StoredProcedure: SQLServer ストアド プロシージャ オブジェクトと、必要に応じてストアド プロシージャを作成するクエリを含む .sql ファイルを生成します。 StoredProcedure$registrationVec には、ストアド プロシージャの作成に必要なクエリを表す文字列が含まれます

使用方法

  StoredProcedure (func, spName, ..., filePath = NULL ,dbName = NULL,
  connectionString = NULL, batchSeparator = "GO")

引数

func

有効な R 関数または有効な R 関数の文字列名: 1) 関数が依存する変数はすべて、関数内で定義するか、入力パラメーターとして入力する必要があります。 入力パラメーターの中には、最大 1 つのデータ フレームを含めることができます 2) 関数では、データ フレーム、名前付きリスト、または NULL のいずれかを返す必要があります。 リスト内には、最大 1 つのデータ フレームを含めることができます。

spName

ストアド プロシージャの名前を指定する文字列。

...

ストアド プロシージャの省略可能な入力パラメーターと出力パラメーター。クラス InputData、InputParameter、または outputParameter のオブジェクトである必要があります。

filePath

.sql を作成するディレクトリへのパスを指定する文字列。 NULL の場合、.sql ファイルは生成されません。

dbName

使用するデータベースの名前を指定する文字列。

connectionString

接続文字列を指定する文字列。

batchSeparator

必要な SQL バッチ区切り記号 (filePath が定義されている場合にのみ関係します)

値

SQLServer ストアド プロシージャ オブジェクト

使用例


 ## Not run:

 ############# Example 1 #############
 # etl1 - reads from and write directly to the database
 etl1 <- function() {
   # The query to get the data
   qq <- "select top 10000 ArrDelay,CRSDepTime,DayOfWeek from AirlineDemoSmall"
   # The connection string
   conStr <- paste("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                 "Trusted_Connection=Yes;", sep = "")
   # The data source - retrieves the data from the database
   dsSqls <- RxSqlServerData(sqlQuery=qq, connectionString=conStr)
   # The destination data source
   dsSqls2 <- RxSqlServerData(table ="cleanData",  connectionString = conStr)
   # A transformation function
   transformFunc <- function(data) {
     data$CRSDepHour <- as.integer(trunc(data$CRSDepTime))
     return(data)
   }
   # The transformation variables
   transformVars <- c("CRSDepTime")
   rxDataStep(inData = dsSqls,
              outFile = dsSqls2,
              transformFunc=transformFunc,
              transformVars=transformVars,
              overwrite = TRUE)
   return(NULL)
 }
 # Create a StoredProcedure object
 sp_ds_ds <- StoredProcedure(etl1, "spTest",
                        filePath = ".", dbName ="RevoTestDB")
 # Define a connection string
 conStr <- paste("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                 "Trusted_Connection=Yes;", sep = "")
 # register the stored procedure with a database
 registerStoredProcedure(sp_ds_ds, conStr)
 # execute the stored procedure
 executeStoredProcedure(sp_ds_ds, connectionString = conStr)


 ############# Example 2 #############
 # train 1 takes a data frame with clean data and outputs a model
 train1 <- function(in_df) {
   in_df[,"DayOfWeek"] <- factor(in_df[,"DayOfWeek"], levels=c("Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"))
   # The model formula
   formula <- ArrDelay ~ CRSDepTime + DayOfWeek + CRSDepHour:DayOfWeek
   # Train the model
   rxSetComputeContext("local")
   mm <- rxLinMod(formula, data=in_df)
   mm <- rxSerializeModel(mm)
   return(list("mm" = mm))
 }
 # create InputData Object for an input parameter that is a data frame
 # note: if the input parameter is not a data frame use InputParameter object
 id <- InputData(name = "in_df",
                defaultQuery = paste0("select top 10000 ArrDelay,CRSDepTime,",
                                      "DayOfWeek,CRSDepHour from cleanData"))
 # create an OutputParameter object for the variable inside the return list
 # note: if that variable is a data frame use OutputData object
 out <- OutputParameter("mm", "raw")

 # connections string
 conStr <- paste0("Driver={ODBC Driver 13 for SQL Server};Server=.;Database=RevoTestDB;",
                  "Trusted_Connection=Yes;")
 # create the stored procedure object
 sp_df_op <- StoredProcedure("train1", "spTest1", id, out,
                        filePath = ".")
 # register the stored procedure with the database
 registerStoredProcedure(sp_df_op, conStr)

 # get the linear model
 model <- executeStoredProcedure(sp_df_op, connectionString = conStr)
 mm <- rxUnserializeModel(model$params$op1)
## End(Not run)