-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.R
More file actions
92 lines (77 loc) · 3.13 KB
/
server.R
File metadata and controls
92 lines (77 loc) · 3.13 KB
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
# select specified columns
all_reviews <- df %>%
# filter(Reviews >= 10) %>%
select(id, name, city, state, rating, zip_code, review_count, phone, score,
latitude, longitude)
function(input, output, session) {
# Filter the reviews, returning a data frame
reviews <- reactive({
# Due to dplyr issue #318, we need temp variables for input values
# browser()
review.i <- input$review
score.i <- input$score
minrating.i <- input$rating[1]
maxrating.i <- input$rating[2]
# city.i <- input$city
# Apply filters
m <- all_reviews %>%
filter(
review_count >= review.i,
score >= score.i,
rating >= minrating.i,
rating <= maxrating.i
) %>%
arrange(rating)
m <- as.data.frame(m)
# Add column which says whether the doctor has 0 score
# Be a little careful in case we have a zero-row data frame
m$has_score <- character(nrow(m))
m$has_score[m$score < 0] <- "<= 0"
m$has_score[m$score > 1] <- "> 0"
m
})
# Function for generating tooltip text
review_tooltip <- function(x) {
if (is.null(x)) return(NULL)
if (is.null(x$id)) return(NULL)
# Pick out the review with this id
all_reviews <- isolate(reviews())
review <- all_reviews[all_reviews$id == x$id, ]
paste0("<b>", "Name: ", review$name, "</b><br>",
"Phone Number: ", review$phone, "<br>",
"City: ", review$city, "<br>",
"Score: ", review$score, "<br>",
"Rating: ", review$rating, "<br>",
"Review Count: ", review$review_count
)
}
# A reactive expression with the ggvis plot
# output$plot1 <-renderPlot({
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))
reviews %>%
ggvis(x = xvar, y = yvar) %>%
layer_points(size := 50, size.hover := 200,
fillOpacity := 0.2, fillOpacity.hover := 0.5,
stroke = ~has_score, key := ~id) %>%
add_tooltip(review_tooltip, "hover") %>%
add_axis("x", title = xvar_name) %>%
add_axis("y", title = yvar_name) %>%
add_legend("stroke", title = "Score", values = c("> 0", "<= 0")) %>%
scale_nominal("stroke", domain = c("> 0", "<= 0"),
range = c("orange", "#aaa")) %>%
set_options(width = 800, height = 300)
})
vis %>% bind_shiny("plot1")
output$n_reviews <- renderText({ paste0("The sentiment analysis is based on 969 online reviews",
" (as judged by the Yelp customers), and the rating is",
" a normalized 1-5 score of those reviews which have star ratings",
" (for example, 3 out of 4 stars).)")
})
}