-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathshinyApp.R
87 lines (68 loc) · 2.19 KB
/
shinyApp.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
#SA 8905 Geovisualization Project - Part 2
#Using Mapdeck in R
#Author: Gregory Huang
#Reference: David Cooley, https://symbolixau.github.io/mapdeck/articles/mapdeck.html
#Current Version: 0.2
#Date: 21 November 2019
#This is the shiny app demo of the project. Assuming all required files are downloaded
#and are in the same repository(folder) as this R file, the code should run and produce a
#shiny app. If the initial popup menu shows blank, remember to click on "view in browser"
#to see the app.
install.packages("mapdeck")
library(mapdeck)
#shiny library
install.packages("shiny")
library(shiny)
#load in data
all_flights <- read.csv("flights_for_shiny.csv")
#mapdeck key: copy and paste your key in between the quotation marks
key <- ""
#create UI and define a slider input
ui <- fluidPage(
mapdeckOutput(outputId = 'myMap'),
sliderInput(
inputId = "longitudes",
label = "Longitudes",
min = -180,
max = 180,
value = c(-180,-90)),
verbatimTextOutput(outputId = "observed_click"))
#create server
server <- function(input, output) {
set_token(key)
#get data
slidertable <- all_flights
#initial map creation
output$myMap <- renderMapdeck({
mapdeck(style = 'mapbox://styles/gregoryhuang/ck33n2hav1vtn1cnkr16ls3uq',
pitch = 20,
height = 60)
})
#reactive slider table
slidertable_reactive <- reactive({
if(is.null(input$longitudes)) return(NULL)
lons <- input$longitudes
return(
slidertable[slidertable$destination_long >= lons[1] & slidertable$destination_long <= lons[2], ]
)
})
observeEvent({input$longitudes}, {
if(is.null(input$longitudes)) return()
mapdeck_update(map_id = 'myMap') %>%
add_arc(
data = slidertable_reactive(),
origin = c("origin_long", "origin_lat"),
destination = c("destination_long", "destination_lat"),
stroke_from = "From",
stroke_to = "To",
layer_id = "myArcLayer",
auto_highlight = TRUE,
tooltip = "RouteInfo",
stroke_width = 3,
update_view = FALSE,
focus_layer = FALSE
)
})
}
#run to launch the app (make sure all previous code ran without issues)
shinyApp(ui,server)