Advertisement
Guest User

Untitled

a guest
Jul 21st, 2019
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. library(shiny)
  2. library(ggplot2)
  3. library(tidyr)
  4. library(dplyr)
  5. library(rlang)
  6. library(plotly)
  7. packageVersion('plotly')
  8. library(lubridate)
  9. library(base)
  10. library(leaflet)
  11. library(leaflet.extras)
  12. library(rdrop2)
  13. library(httr)
  14. library(rsconnect)
  15.  
  16. token <- drop_auth()
  17. saveRDS(token, "droptoken.rds")
  18.  
  19.  
  20.  
  21. ui <- fluidPage(
  22.  
  23. tags$style(type="text/css",
  24. ".recalculating {opacity: 1.0;}"
  25. ),
  26.  
  27. leafletOutput(outputId = "mainmap", height = 600)
  28.  
  29. )
  30.  
  31. server <- function(input, output, session){
  32.  
  33. rsconnect::setAccountInfo(name='xxxxx',
  34. token='xxxxxxx',
  35. secret='xxxxxxxxx')
  36.  
  37. drop_auth(key = "xxxxxx", secret = "xxxxxxx", rdstoken = "droptoken.rds")
  38.  
  39.  
  40. bw <- reactive({
  41. load_data_dropbox()
  42. })
  43.  
  44.  
  45. load_data_dropbox <- function() {
  46.  
  47. invalidateLater(600000, session)
  48. bw <- drop_read_csv("ShipData_NMEA.csv", dtoken=token)
  49.  
  50. }
  51.  
  52.  
  53. # Plot the latest position of the vessel
  54. ShipNames_DF <- reactive({
  55.  
  56.  
  57. bwtemp <- NULL
  58. ShipNames <- unique(as.character(bw()$ShipName))
  59. ShipNames_DF <- data.frame(ShipNames)
  60. ShipNames_DF$MaxDate <- as.POSIXct("1900-01-01 00:00:00")
  61. ShipNames_DF$Lat <- NA
  62. ShipNames_DF$Lon <- NA
  63. ShipNames_DF$GPSSpeed <- NA
  64. ShipNames_DF$WindSpeedTrue <- NA
  65. ShipNames_DF$WindDirectionTrue <- NA
  66.  
  67.  
  68.  
  69. for (i in 1:nrow(ShipNames_DF)){
  70. bwtemp<- NULL
  71. bwtemp <- bw() %>%
  72. filter(ShipName == ShipNames_DF$ShipNames[i])
  73.  
  74. bwtemp$DateTime <- as.POSIXct(bwtemp$DateTime)
  75.  
  76. bwtemp <- bwtemp[1,]
  77.  
  78. ShipNames_DF$MaxDate[i] <- bwtemp$DateTime[1]
  79. ShipNames_DF$Lat[i] <- bwtemp$Lat[1]
  80. ShipNames_DF$Lon[i] <- bwtemp$Lon[1]
  81. ShipNames_DF$GPSSpeed[i] <- bwtemp$GPSSpeed[1]
  82. ShipNames_DF$WindSpeedTrue[i] <- bwtemp$WindSpeedTrue[1]
  83. ShipNames_DF$WindDirectionTrue[i] <- bwtemp$WindDirectionTrue[1]
  84. }
  85. bwtemp <- NULL
  86. ShipNames_DF <- as.data.frame(ShipNames_DF)
  87.  
  88. return(ShipNames_DF)
  89.  
  90. })
  91.  
  92.  
  93.  
  94. # To plot the last 24 hours sailing position to give indication on the heading
  95. Direction_DF <- reactive({
  96.  
  97. bwtemp <- NULL
  98.  
  99. ShipNames_DFin <- ShipNames_DF()
  100.  
  101.  
  102. Direction_DF <- NA
  103. for (i in 1:nrow(ShipNames_DFin)){
  104. bwtemp<- NULL
  105. bwtemp <- bw() %>%
  106. filter(ShipName == ShipNames_DFin$ShipNames[i])
  107.  
  108. bwtemp <- bwtemp[1:144,]
  109.  
  110. Direction_DF <- rbind(Direction_DF, bwtemp)
  111.  
  112. }
  113. bwtemp <- NULL
  114. Direction_DF <- as.data.frame(Direction_DF)
  115.  
  116. return(Direction_DF)
  117.  
  118. list(df1)
  119.  
  120. })
  121.  
  122.  
  123.  
  124. output$mainmap <- renderLeaflet({
  125.  
  126.  
  127. ShipNames_DFin <- ShipNames_DF()
  128.  
  129. Direction_DFin <- Direction_DF()
  130.  
  131.  
  132. leaflet(data) %>%
  133. setView(lng = 120, lat = -40, zoom = 2.8) %>%
  134. addTiles() %>%
  135. addTerminator(group = "Day or Night") %>%
  136. addCircleMarkers(data = ShipNames_DFin, lat = ~Lat, lng = ~Lon, label = ~MaxDate, weight = 1,
  137. labelOptions = labelOptions(noHide = T,
  138. textOnly = TRUE,
  139. direction = "bottom",
  140. style = list("color" = "black",
  141. "font-size" = "12px"))) %>%
  142. addPolylines(data = subset(Direction_DFin, ShipName == "Black Pearl"), lat = ~Lat, lng = ~Lon, color = "black", weight = 1.4, dashArray = '5, 5', group = "48hr sailed") %>%
  143. addLayersControl(
  144. overlayGroups = c("48hr sailed", "Day or Night"),
  145. options = layersControlOptions(collapsed = FALSE)
  146. )
  147.  
  148. })
  149.  
  150. }
  151.  
  152. shinyApp(ui=ui, server=server)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement