Using R - recommenderlab and can't get results into dataframe for Microsoft SQL Server

353 Views Asked by At

This is my first post here. I'm very much a newbie when it comes to R, so please keep that in mind. I'm building a recommender system (User Content Based Filtering) using the recommenderlab library from CRAN.

I'm trying to use sp_execute_external_script with SQL Server 2016, but can't get the results into a dataframe (required by the stored procedure) to return the results.

Everything works up until that point. I've tried as.matrix, as.data.frame, using a variety of methods.

Here's my code:

EXEC sp_execute_external_script
  @language =N'R',
  @input_data_1 =N'select * from matrix_table',
  @input_data_1_name = N'rentaldata',
  @output_data_1_name = N'Sales',
  @script=N'library(recommenderlab);

  rentaldata2 <- as.data.frame(rentaldata);

  rentaldata2 <- rxImport(rentaldata2);

  rentaldata2$enduser <- factor(rentaldata2$enduser);

  Sales_Interim <- as(rentaldata2, "realRatingMatrix"); 

  as(Sales_Interim, "list")

  makemeUBCF <- Recommender(Sales_Interim[1:75], method = "UBCF")

  #give me top 5 recommendations for two users that were not part of 
  training set          
  recomUBCF <- predict(makemeUBCF, Sales_Interim[76:77], type="ratings", n=5)

  Sales <- as.data.frame(recomUBCF)
  '

Can anyone help?

I've tried using as.data.frame(), as.matrix(), writing to a file instead using write.table, lapply, writeLines....

Thanks!

1

There are 1 best solutions below

0
On

I've tried on my server and using the following works for me:

Sales <- data.frame(as(recomUBCF,"matrix"))

Since recomUBCF should be a realRatingMatrix it should be working.

Hope it help, cheers, Arnaud