2016-08-30 5 views
3

私はここで新しいです、助けて助けてくれることを願っています。Microsoft SQLとR、ストアドプロシージャとk-手段

しかし、Rとの統合を暗示する新しい機能を使用して、新しいMicrosoft SQL Server Management Studio(2016)に取り組んでいます。 まず、私の目標は、K- xとyの列でクラスタリングすることを意味します。

問題は、私のケースにオンラインドキュメントを拒否できないため、私は真ん中に詰まっているということです。ここで

ようにそこにスクリプト

CREATE TABLE [dbo].[ModelTable] 
    (
    column_name1 varchar(8000) 
    ) 
    ; 

    CREATE TABLE [dbo].[ResultTable] 
    (
    column_name1 varchar(8000), 
    column_name2 varchar(8000), 
    column_name3 varchar(8000), 
    column_name4 varchar(8000) 
    ) 
    ; 

    CREATE PROCEDURE [dbo].[kmean] 

    AS 
    BEGIN 
    DECLARE @inquery nvarchar(max) = N' 
       select name,x,y FROM [dbtable] 

    ' 
    -- then I decide to insert the model in a table: this is similar to the documentation, but I am not sure it fits well. 

    INSERT INTO [dbo].[ModelTable] 
    EXEC sp_execute_external_script @language = N'R', 
            @script = N' 

    ## Here I create model: this is one of the biggest problem, because I tried to create a data frame with the data, but I do not know if here, 
    ## in the R code, the data are read in this way. Generally in "pure" R, I write data.frame(sourcedata$x,sourcedata$y), but here, where is source of data? 
    ## In the documentation it is used ImputDataSet, so maybe I could do: 

    trained_model <- kmeans(data.frame(ImputDataSet$x,ImputDataSet$y),8) 

    -- If everything is ok (doubtfully) I should have the model. And here, the part that I really cannot handle. 
    -- I'd like to have a table [ResultTable] with name, variable x, variable y, and trainedmodel$cluster. 

    ', 
           @input_data_1 = @inquery, 
           @output_data_1_name = N'trained_model' 
    ; 


    END 
    GO 

    EXEC kmean 

まあ多くの問題として、これはMSSMSでは非常にブランドの新機能であるという事実により、インターネットでのヘルプなどの偉大な量がありません。事前

+0

他の方法、[データを読む](http://stackoverflow.com/questions/3932864/reading-data-from-microsoft-sql-server-into-r)をRに入れ、Rを[kmeans](https://stat.ethz.ch/R-manual/R-devel/library/stats/html/kmeans.html)? – zx8754

+0

@ zx8754 - 私はOPが構文で見られるようにRを実行していると考えていますが、SQL Serverストアドプロシージャを使用している可能性があります。[SQL Server R Services](https://msdn.microsoft.com/en-us/library) /mt604885.aspx)。 – Parfait

答えて

3

で おかげで私たちは次のことを試すことができます。

CREATE TABLE #tempData (x float not null, y float not null); 
INSERT INTO #tempData VALUES (0, 0), (0.1, 0.1), (1, 1), (1.1, 1.1); 

CREATE TABLE #output (x float, y float, Cluster int); 

INSERT INTO #output 
EXECUTE sp_execute_external_script 
       @language = N'R' 
       , @script = N'     
         trained_model <- kmeans(df[, c("x", "y")], 2) 
         df$cluster <- trained_model$cluster 
         ' 
       , @input_data_1 = N'SELECT * from #tempData' 
       , @output_data_1_name = N'df' 
       , @input_data_1_name = N'df'; 

SELECT * 
FROM #output 

出力で:私はdfであることを私の入力 - 出力データを指定し

x y Cluster 
0 0 1 
0.1 0.1 1 
1 1 2 
1.1 1.1 2 

注意を。デフォルトはInputDataSetOutputDataSetです。

Rスクリプトが長い場合:R環境で書いてテストすることをお勧めします。パッケージに入れたまま、ロードして呼び出してください。

+0

ありがとう、これまで動作します:私はそれをクエリに拒否するだけです。はい、私はRで試してみました。 – user6498650

+0

あなたはようこそ!大規模なスクリプトをRパッケージにラップすることは、ストアドプロシージャとしてのRのベストプラクティスを引き受けることでした。書き込み、デバッグ、メンテナンスが容易になります。私は、これが明示的に例で説明されていないのはなぜだろうか。 'sp_execute ...'コマンドは恐ろしいです。 –

関連する問題