inst/getRecommendations.R

#' Recommend movies
#'
#' Simple model to recommend movies
#'
#' @export
#' @importFrom recommenderlab predict
#' @importFrom reshape2 acast
#' @importFrom rjson fromJSON
#' @importFrom Matrix sparseMatrix
#'
#' @param jsonString data passed on as \code{newdata} to \code{\link{predict}}
#' @examples myJsonString <- "[[126,5], [156,3], [566,2], [1333,4], [1660,5],
#' [2126,3], [3126,2], [4126,5], [5126,3], [6126,1], [7126,5], [8000,2], [8007,3],
#' [8100,3], [8200,4], [8300,5], [8310,4], [8320,5], [8330,1]]"
#' getRecommendations(myJsonString)
#'
getRecommendations <- function(jsonString) {

  rjsonData <- fromJSON(jsonString)
  frame  <- as.data.frame(rjsonData)
  matrix <- as.matrix(frame)

  movieIds <- matrix[1,]
  ratings <- matrix[2,]
  numberMovieIds <- length(movieIds)
  i <- rep.int(1, numberMovieIds)

  sparseMatrix <- sparseMatrix(i, movieIds, x = ratings, dims=c(1,8552))
  r <- new("realRatingMatrix", data = sparseMatrix)

  # make a topN prediction for user1 and N = 10
  user1topN <- predict(recommender, r, n = 10)
  return(user1topN@items[[1]])
}
paulij/recommendMovies documentation built on May 24, 2019, 8:44 p.m.