Last active
September 22, 2016 22:22
-
-
Save annecool37/39c69c5fdaa7375ce6994dd93f7da13a to your computer and use it in GitHub Desktop.
museo_shiny_server
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# get the recommendation if the recommendation button is hit | |
museumInput = eventReactive(input$recommend_btn,{ | |
# get museum names and concatenate by ';' | |
museum_names = "" | |
for (m in input$selected_museum){ | |
museum_names = paste(museum_names, m, sep = ';') | |
} | |
# pass the selected museum names into python to compute consine similarity | |
command_line = paste0('python get_sorted_suggestion.py "', museum_names, '"') | |
system(command_line) | |
# read the sorted recommended museums generate from python | |
top_data = fromJSON(paste(readLines('sorted_suggestion.json'), collapse="")) | |
top_data | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This section is the server code for the map in "Museum Map" tab | |
# 1. output$map = renderLeaflet({ ....: create the defalt map | |
# 2. mapInput = eventReactive(input$go,... : filter the museums based on user selection | |
# 3. observe({... : re-create the map based on the observation above (filter options selected) | |
# create the default map without any options being selected | |
output$map = renderLeaflet({ | |
# get the complete cases in the museum where there are latitude values (or else the leaflet won't work) | |
museum_complete = museum[which(!is.na(museum$Latitude)),] | |
leaflet(museum_complete) %>% | |
addProviderTiles("Stamen.Toner") %>% # 31.910864, -71.433555 | |
setView(-71.433555,31.910864, zoom = 3) %>% | |
# customize the display for each marker on the amp | |
addCircleMarkers(~Langtitude, ~Latitude, clusterOptions = markerClusterOptions(), | |
fillColor = ~pal(Rating), fillOpacity = 0.7, stroke=FALSE, | |
popup = ~paste('<b><font color="#0a0a0a" size = "4">',MuseumName,'</font></b><font color="#595959"><br/>', | |
"<img src=", img_link, " width = 250px><br/>", | |
'Rating:',format(round(PreciseRating, 2), nsmall = 2),'<br/><br/>#',Rank, 'of ', | |
TotalThingsToDo,'things to do in this city<br/><br/>','Address:',Address, | |
'<br/><br/>','Description:','<br/>',Description, '</font>')) %>% | |
addLegend("bottomright", pal = pal, values = ~ museum_complete$Rating, | |
title = "Rating", opacity = 0.8) | |
}) | |
# read user input to filter the museums | |
mapInput = eventReactive(input$go,{ | |
# Note: if nothing is selected, just display the whole data | |
total_row = nrow(museum) | |
# get idx for museum type selected | |
if (is.null(input$museum_type)){ | |
type_idx = c(1:total_row) | |
} | |
else { | |
target_name_master = c() | |
for (t in input$museum_type) { | |
target_name_master = append(target_name_master, type_data[t][[1]]) | |
} | |
type_idx = which(as.character(museum$MuseumName) %in% target_name_master) | |
} | |
# get index for length of visit selected | |
if (is.null(input$visit_length)){ | |
length_idx = c(1:total_row) | |
} | |
else { | |
length_idx = which(museum$LengthOfVisit %in% input$visit_length) | |
} | |
# get index for 'good for' & 'other option' selected | |
if (is.null(input$good_for) & is.null(input$other_option)){ | |
idx_checked = c(1:total_row) | |
} | |
else { | |
idx_checked = c() | |
check_list = c(input$good_for, input$other_option) | |
for (item in check_list) { | |
idx = which(museum[,item] == 1) | |
idx_checked = append(idx_checked, idx) | |
} | |
if("free.admission" %in% input$other_option){ | |
idx = which(museum[, "free.entry"] == 1) | |
idx_checked = append(idx_checked, idx) | |
idx_to_add= which(museum$Fee == 'No ') | |
idx_checked = append(idx_checked, idx_to_add) | |
idx_to_remove = which(museum$Fee == 'Yes ') | |
idx_checked = idx_checked[! idx_checked %in% idx_to_remove] | |
} | |
} | |
# find intersect of idx | |
idx_final = intersect(type_idx, length_idx) | |
idx_final = intersect(idx_final, idx_checked) | |
# get new museum dataframe based on selected index | |
new_museum = museum[idx_final,] | |
new_museum | |
}) | |
# generate a new map based on user selection on the filter | |
observe({ | |
# observe the user selection to filter the museums that satisfy the criteria | |
new_museum = mapInput() | |
museum_complete = new_museum[which(!is.na(new_museum$Latitude)),] | |
leafletProxy("map", data = museum_complete) %>% | |
clearMarkerClusters() %>% | |
addCircleMarkers(~Langtitude, ~Latitude, clusterOptions = markerClusterOptions(), | |
fillColor = ~pal(Rating), fillOpacity = 0.7, stroke = FALSE, | |
popup = ~paste('<b><font color="#0a0a0a" size = "4">',MuseumName,'</font></b><font color="#595959"><br/>', | |
"<img src=", img_link, " width = 250px><br/>", | |
'Rating:',format(round(PreciseRating, 2), nsmall = 2),'<br/><br/>#',Rank, 'of ', | |
TotalThingsToDo,'things to do in this city<br/><br/>','Address:',Address, | |
'<br/><br/>','Description:','<br/>',Description, '</font>')) | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment