尝试将功能连接到R中的闪亮应用程序

时间:2019-03-03 01:29:54

标签: r ggplot2 shiny

我在kaggle上看到了这项出色的工作: https://www.kaggle.com/jonathanbouchet/airlines-route-tracker

,我尝试通过添加“ Shiny”应用来改善可视化效果。动机:通过选择机场,您将在交互式地图上看到连接。

功能是:

#-------------Routes display function------------------------
makeConnections <- function(IATA.start){
  d1 <- data.frame(routes %>% filter(source.airport == IATA.start))
  d1.iata.start <- data.frame(d1 %>% select(destination.airport) %>% 
    rename(IATA = destination.airport))
  #merge with the airport data
  d2 <- data.frame(merge(d1.iata.start,
                         airports %>% 
                           select(airport.name, city.name, country.name, IATA, lat, long),
                         by='IATA', sort=F))
  colnames(d2) <- c("IATA.end", "airport.name.end", "city.name.end", 
                    "country.name.end", "lat.end", "long.end")
  #get geo locations of source.airport
  lat.start <- rep(airports[airports$IATA==IATA.start, 'lat'], nrow(d1))
  long.start <- rep(airports[airports$IATA==IATA.start, 'long'], nrow(d1))
  d1$lat.start = lat.start
  d1$long.start = long.start
  #cbind all
  res<- data.frame(cbind(d1, d2))
  return(res)
}

#-------------Function for display connections on map--------------------
airport_connections<-function(airport_from){
start_from = makeConnections(airport_from)
start_from.reduced <- start_from %>% 
  select(airport.name.end, lat.start, long.start, lat.end, long.end) %>%
  group_by(airport.name.end) %>% 
  mutate(count=n()) %>% 
  distinct()
maxFlights <- max(start_from.reduced$count)

gplot <- world_map + 
  geom_curve(data = start_from.reduced, 
             aes(x = long.start, y = lat.start, xend = long.end, yend = lat.end,
                 color = factor(count)), 
                 curvature = 0.2, arrow = arrow(length = unit(0.005, "npc")), 
                 alpha = .75, size = 1) + 
  theme_fivethirtyeight() + 
  theme(
    panel.grid.major = element_blank(),
    axis.text=element_blank(),
    axis.ticks=element_blank()) + 
  scale_color_manual(name = "Airlines serving", 
                     values = rev(viridis::viridis(maxFlights))) +
  ggtitle(paste0('Departures from ', 
                 (airports %>% filter(IATA==airport_from))$airport.name))

return(gplot)
}

通过选择机场,您将看到连接。例如:

airport_connections('LAX')

image

现在,我尝试将其连接到Shiny应用程序。那就是我所做的:

#----------------User Interface-----------------------
ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100% ; height:100%}"),

  leafletOutput("map", width = "100%", height = "100%"),
  absolutePanel(top = 10, right = 10, draggable = F,
                selectInput("select", "Airport", 
                            choice = sort(airports[, 2], decreasing = F), 
                            multiple = F)),
  tags$div(id="cite", 'My Visualiztion')
)

#----------------Server------------------
# Server side codes and functions
server <- function(input, output, session) {
  # Create the map
  output$map <- renderLeaflet({
    leaflet(data) %>% 
      addTiles(options = providerTileOptions(minZoom=2, maxZoom=4)) %>% # in addTiles you can change maps
      setView(lng = 0, lat = 20, zoom = 2) %>% # set view to a predefined spot on the map  
      addProviderTiles(providers$Esri.WorldStreetMap)
    #airport_connections(input)

  })


  # Checks what the current boundaries of the map and return countries inside
  countriesInBounds <- reactive({
    if (is.null(input$map_bounds))
      return(filteredData()[FALSE,])
    bounds <- input$map_bounds
    latRng <- range(bounds$north, bounds$south)
    lngRng <- range(bounds$east, bounds$west)

    subset(filteredData(),
           latitude >= latRng[1] & latitude <= latRng[2] &
             longitude >= lngRng[1] & longitude <= lngRng[2])
  })


}#server

shinyApp(ui, server)

我不知道如何将以上功能连接到服务器。

0 个答案:

没有答案