-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.R
116 lines (98 loc) · 3.56 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
library(ggvis)
library(dplyr)
if (FALSE) {
library(RSQLite)
library(dbplyr)
}
# Set up handles to database tables on app start
db <- src_sqlite("movies.db")
omdb <- tbl(db, "omdb")
tomatoes <- tbl(db, "tomatoes")
# Join tables, filtering out those with <10 reviews, and select specified columns
all_movies <- inner_join(omdb, tomatoes, by = "ID") %>%
filter(Reviews >= 10) %>%
select(ID, imdbID, Title, Year, Rating_m = Rating.x, Runtime, Genre, Released,
Director, Writer, imdbRating, imdbVotes, Language, Country, Oscars,
Rating = Rating.y, Meter, Reviews, Fresh, Rotten, userMeter, userRating, userReviews,
BoxOffice, Production, Cast)
function(input, output, session) {
# Filter the movies, returning a data frame
movies <- reactive({
# Due to dplyr issue #318, we need temp variables for input values
reviews <- input$reviews
oscars <- input$oscars
minyear <- input$year[1]
maxyear <- input$year[2]
minboxoffice <- input$boxoffice[1] * 1e6
maxboxoffice <- input$boxoffice[2] * 1e6
# Apply filters
m <- all_movies %>%
filter(
Reviews >= reviews,
Oscars >= oscars,
Year >= minyear,
Year <= maxyear,
BoxOffice >= minboxoffice,
BoxOffice <= maxboxoffice
) %>%
arrange(Oscars)
# Optional: filter by genre
if (input$genre != "All") {
genre <- paste0("%", input$genre, "%")
m <- m %>% filter(Genre %like% genre)
}
# Optional: filter by director
if (!is.null(input$director) && input$director != "") {
director <- paste0("%", input$director, "%")
m <- m %>% filter(Director %like% director)
}
# Optional: filter by cast member
if (!is.null(input$cast) && input$cast != "") {
cast <- paste0("%", input$cast, "%")
m <- m %>% filter(Cast %like% cast)
}
m <- as.data.frame(m)
# Add column which says whether the movie won any Oscars
# Be a little careful in case we have a zero-row data frame
m$has_oscar <- character(nrow(m))
m$has_oscar[m$Oscars == 0] <- "No"
m$has_oscar[m$Oscars >= 1] <- "Yes"
m
})
# Function for generating tooltip text
movie_tooltip <- function(x) {
if (is.null(x)) return(NULL)
if (is.null(x$ID)) return(NULL)
# Pick out the movie with this ID
all_movies <- isolate(movies())
movie <- all_movies[all_movies$ID == x$ID, ]
paste0("<b>", movie$Title, "</b><br>",
movie$Year, "<br>",
"$", format(movie$BoxOffice, big.mark = ",", scientific = FALSE)
)
}
# A reactive expression with the ggvis plot
vis <- reactive({
# Lables for axes
xvar_name <- names(axis_vars)[axis_vars == input$xvar]
yvar_name <- names(axis_vars)[axis_vars == input$yvar]
# Normally we could do something like props(x = ~BoxOffice, y = ~Reviews),
# but since the inputs are strings, we need to do a little more work.
xvar <- prop("x", as.symbol(input$xvar))
yvar <- prop("y", as.symbol(input$yvar))
movies %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~has_oscar, key := ~ID) %>%
add_tooltip(movie_tooltip, "hover") %>%
add_axis("x", title = xvar_name) %>%
add_axis("y", title = yvar_name) %>%
add_legend("stroke", title = "Won Oscar", values = c("Yes", "No")) %>%
scale_nominal("stroke", domain = c("Yes", "No"),
range = c("orange", "#aaa")) %>%
set_options(width = 500, height = 500)
})
vis %>% bind_shiny("plot1")
output$n_movies <- renderText({ nrow(movies()) })
}