-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp_server.R
138 lines (127 loc) · 3.81 KB
/
app_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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Load necessary libraries
library("ggplot2")
library("shiny")
library("dplyr")
library("plotly")
# Create a server function that takes in user's input and return output
server <- function(input, output) {
# Return output
# Output for first interactive page
output$map <- renderLeaflet({
# Make map
make_map <- function(df) {
map <- leaflet(df) %>%
addProviderTiles("CartoDB.Positron") %>%
setView(lng = -122.3321, lat = 47.6062, zoom = 11) %>%
addCircles(
lat = ~latitude,
lng = ~longitude,
popup = ~ paste(
paste0("<b><img src=", thumbnail_url, ">"),
name,
paste0("Rating: ", review_scores_rating),
paste0("<b><a href=", listing_url, ">Open in Airbnb</a></b>"),
sep = "<br/>"
),
radius = 30,
fillOpacity = 0.5,
stroke = FALSE
)
return(map)
}
data <- airbnb %>%
filter(grepl(input$name, name)) %>%
filter(input$instant_book == (instant_bookable == "t"))
if (input$property_type != "All") {
data <- filter(data, property_type == input$property_type)
}
if (input$room_type != "All") {
data <- filter(data, room_type == input$room_type)
}
make_map(data)
})
# Output for second interactive page
output$useful_correlations <- renderPlotly({
# Store the title
title <- paste(
"Correlation Between", input$x_var, "and", input$y_var
)
# Reorganize the data
data_needed <- airbnb %>%
select(
accommodates, bathrooms, bedrooms, beds, price,
number_of_reviews, review_scores_rating, reviews_per_month
)
colnames(data_needed) <- c(
"Accommodates", "Bathrooms",
"Bedrooms", "Beds", "Price", "Reviews", "Rating",
"ReviewsPerMonth"
)
data_needed <- data_needed %>%
mutate(Price = as.numeric(gsub("[\\$,]", "", Price)))
# Create a scatter plot that reveals the correlation
scatter_plot <- ggplot(data = data_needed) +
geom_point(
mapping = aes_string(x = input$x_var, y = input$y_var),
size = input$size
) +
labs(
x = input$x_var,
y = input$y_var,
title = title
)
# return the scatter plot
scatter_plot
})
# Output for third interactive page
output$barchart <- renderPlot({
if (input$neighbourhood != "All") {
airbnb <- airbnb %>%
filter(neighbourhood == input$neighbourhood)
}
data <- airbnb %>%
group_by(room_type) %>%
summarise(n = n())
# Bar chart doesn't have hover over function
chart <- ggplot(data, aes(x = reorder(room_type, n),
y = n, fill = room_type)) +
geom_col() +
coord_flip() +
scale_fill_brewer(palette = "Set1") +
labs(
title = "Room Distribution",
x = "Room Type",
y = "Number of Room"
)
return(chart)
})
output$number <- renderText({
if (input$neighbourhood != "All") {
airbnb <- airbnb %>%
filter(neighbourhood == input$neighbourhood)
}
return(nrow(airbnb))
})
output$price <- renderText({
if (input$neighbourhood != "All") {
airbnb <- airbnb %>%
filter(neighbourhood == input$neighbourhood)
}
data <- airbnb %>%
mutate(Price = as.numeric(gsub("[\\$,]", "", price)))
average_price <- data %>%
summarise(average_price = mean(Price, na.rm = TRUE)) %>%
pull(average_price)
return(round(average_price))
})
output$rating <- renderText({
if (input$neighbourhood != "All") {
airbnb <- airbnb %>%
filter(neighbourhood == input$neighbourhood)
}
average_rating <- airbnb %>%
summarise(average_rating = mean(review_scores_rating, na.rm = TRUE)) %>%
pull(average_rating)
return(round(average_rating))
})
}