输出闪亮的照片

时间:2017-09-19 06:46:52

标签: r shiny data-science

现在我需要使用R来练习和构建推荐系统。数据集来自MovieLens。我也想输出电影照片,但不知道该怎么办。如果有10000部电影,我应该如何保存它们并将它们输出到我闪亮的APP上?欢迎提出建议!

ui.R:

setwd("C:\\Users\\lili\\Movieshiny")
movies <- read.csv("movies.csv", header = TRUE, stringsAsFactors=FALSE)
movies <- movies[with(movies, order(title)), ]
ratings <- read.csv("ratings100k.csv", header = TRUE)

shinyServer(function(input, output) {

  # Text for the 3 boxes showing average scores
    formulaText1 <- reactive({
      paste(input$select)
    })
    formulaText2 <- reactive({
      paste(input$select2)
    })
    formulaText3 <- reactive({
      paste(input$select3)
    })

    output$movie1 <- renderText({
      formulaText1()
    })
    output$movie2 <- renderText({
      formulaText2()
    })
    output$movie3 <- renderText({
      formulaText3()
    })


    # Table containing recommendations
    output$table <- renderTable({

      # Filter for based on genre of selected movies to enhance recommendations
      cat1 <- subset(movies, title==input$select)
      cat2 <- subset(movies, title==input$select2)
      cat3 <- subset(movies, title==input$select3)

      # If genre contains 'Sci-Fi' then  return sci-fi movies 
      # If genre contains 'Children' then  return children movies
      if (grepl("Sci-Fi", cat1$genres) | grepl("Sci-Fi", cat2$genres) | grepl("Sci-Fi", cat3$genres)) {
        movies2 <- (movies[grepl("Sci-Fi", movies$genres) , ])
      } else if (grepl("Children", cat1$genres) | grepl("Children", cat2$genres) | grepl("Children", cat3$genres)) {
        movies2 <- movies[grepl("Children", movies$genres), ]
      } else {
        movies2 <- movies[grepl(cat1$genre1, movies$genres) 
                          | grepl(cat2$genre1, movies$genres)
                          | grepl(cat3$genre1, movies$genres), ]
      }

      movie_recommendation <- function(input,input2,input3){
        row_num <- which(movies2[,3] == input)
        row_num2 <- which(movies2[,3] == input2)
        row_num3 <- which(movies2[,3] == input3)
        userSelect <- matrix(NA,length(unique(ratings$movieId)))
        userSelect[row_num] <- 5 #hard code first selection to rating 5
        userSelect[row_num2] <- 4 #hard code second selection to rating 4
        userSelect[row_num3] <- 4 #hard code third selection to rating 4
        userSelect <- t(userSelect)

        ratingmat <- dcast(ratings, userId~movieId, value.var = "rating", na.rm=FALSE)
        ratingmat <- ratingmat[,-1]
        colnames(userSelect) <- colnames(ratingmat)
        ratingmat2 <- rbind(userSelect,ratingmat)
        ratingmat2 <- as.matrix(ratingmat2)

        #Convert rating matrix into a sparse matrix
        ratingmat2 <- as(ratingmat2, "realRatingMatrix")

        #Create Recommender Model
        recommender_model <- Recommender(ratingmat2, method = "UBCF",param=list(method="Cosine",nn=30))
        recom <- predict(recommender_model, ratingmat2[1], n=30)
        recom_list <- as(recom, "list")
        recom_result <- data.frame(matrix(NA,30))
        recom_result[1:30,1] <- movies2[as.integer(recom_list[[1]][1:30]),3]
        recom_result <- data.frame(na.omit(recom_result[order(order(recom_result)),]))
        recom_result <- data.frame(recom_result[1:10,])
        colnames(recom_result) <- "User-Based Collaborative Filtering Recommended Titles"
        return(recom_result)
      }

      movie_recommendation(input$select, input$select2, input$select3)

    })

    movie.ratings <- merge(ratings, movies)
    output$tableRatings1 <- renderValueBox({
      movie.avg1 <- summarise(subset(movie.ratings, title==input$select),
                              Average_Rating1 = mean(rating, na.rm = TRUE))
      valueBox(
        value = format(movie.avg1, digits = 3),
        subtitle = input$select,
        icon = if (movie.avg1 >= 3) icon("thumbs-up") else icon("thumbs-down"),
        color = if (movie.avg1 >= 3) "aqua" else "red"
      )

    })

    movie.ratings <- merge(ratings, movies)
    output$tableRatings2 <- renderValueBox({
      movie.avg2 <- summarise(subset(movie.ratings, title==input$select2),
                              Average_Rating = mean(rating, na.rm = TRUE))
      valueBox(
        value = format(movie.avg2, digits = 3),
        subtitle = input$select2,
        icon = if (movie.avg2 >= 3) icon("thumbs-up") else icon("thumbs-down"),
        color = if (movie.avg2 >= 3) "aqua" else "red"
      )
    })

    movie.ratings <- merge(ratings, movies)
    output$tableRatings3 <- renderValueBox({
      movie.avg3 <- summarise(subset(movie.ratings, title==input$select3),
                Average_Rating = mean(rating, na.rm = TRUE))
      valueBox(
        value = format(movie.avg3, digits = 3),
        subtitle = input$select3,
        icon = if (movie.avg3 >= 3) icon("thumbs-up") else icon("thumbs-down"),
        color = if (movie.avg3 >= 3) "aqua" else "red"
      )
    })


    # Generate a table summarizing each players stats
    output$myTable <- renderDataTable({
      movies[c("title", "genres")]
    })

}
)

server.R:

ui <- fluidPage(
  titlePanel("Look at the image below"),

  sidebarLayout(sidebarPanel(),

                mainPanel(htmlOutput("picture"))))

例如,我想将其插入到我的代码中: 库(有光泽)

使用外部图像调用定义UI

server <- function(input, output) {
  output$picture <-
    renderText({
      c(
        '<img src="',
        "http://www.google.com.tw/search?biw=1536&bih=759&tbm=isch&sa=1&q=notebook+movie&oq=notebook+movie&gs_l=psy-ab.3..0l4.5729.7315.0.7708.6.6.0.0.0.0.223.623.4j1j1.6.0....0...1.1.64.psy-ab..0.6.622...0i67k1.0.P-BZX3u-bzo#imgrc=S0E91gxvZcgeMM:",
        '">'
      )
    })
}

shinyApp(ui = ui, server = server)

使用热链接图像

所需的信息定义服务器
{{1}}

每部电影都有不同的海报图片。

0 个答案:

没有答案