Commit 627b9297 authored by peguerin's avatar peguerin
Browse files

shiny server

parent 0e731b8f
###############################################################################
# load libraries
library(shiny)
library(leaflet)
library(raster)
library(sp)
library(rgdal)
library(rgeos)
###############################################################################
# functions
## from center of square coordinate (h,k) and length L return corner coordinates
corners_of_square <- function(cells,L) {
corners=data.frame(
lng1=cells$x-L/2,
lat1=cells$y+L/2,
lng2=cells$x+L/2,
lat2=cells$y-L/2
)
return(corners)
}
## from (x,y) meters coordinates project to google map
change_projection_xy <- function(xy) {
colnames(xy) = c("x","y")
if(length(which(xy$y > 7300200 )) > 0) { xy$y[which(xy$y > 7300200)] = 7300100 }
if(length(which(xy$y < -7300200 )) > 0) { xy$y[which(xy$y < -7300200)] = -7300100 }
if(length(which(xy$x > 17084470 )) > 0) { xy$x[which(xy$x > 17084470)] = 17074470 }
if(length(which(xy$x < -17084470 )) > 0) { xy$x[which(xy$x < -17084470)] = -17074470 }
coordinates(xy) = c("x", "y")
proj4string(xy) = CRS("+proj=cea +lon_0=0 +lat_ts=30 +x_0=0 +y_0=0 +datum=WGS84 +units=m +no_defs +ellps=WGS84 +towgs84=0,0,0")
xy = spTransform(xy,CRS("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"))
xy_table=data.frame(x=as.double(xy@coords[,1]), y=as.double(xy@coords[,2]))
return(xy_table)
}
###############################################################################
# LOAD DATA
marine = read.table("donnees/figure_3a_underlying_data.tsv",header=T)
marineCorners=corners_of_square(data.frame(x=marine$x,y=marine$y),194000)
rect_1=change_projection_xy(data.frame(x=marineCorners$lng1,y=marineCorners$lat1))
rect_2=change_projection_xy(data.frame(x=marineCorners$lng2,y=marineCorners$lat2))
rect=cbind(rect_1,rect_2)
colnames(rect)=c("lng1","lat1","lng2","lat2")
marineRect=cbind(marine,rect)
###############################################################################
# AESTETHICS
gradiant_col_palette=c("#3333A2","#3333FF","#33CBFF","#33FFFF","#FFDF33","#FFA333","#FF3333")
###############################################################################
# SERVER
r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()
server <- function(input, output, session) {
points <- eventReactive(input$recalc, {
cbind(rnorm(40) * 2 + 13, rnorm(40) + 48)
}, ignoreNULL = FALSE)
output$mymap <- output$map <- renderLeaflet({
leaflet() %>%
addProviderTiles(providers$Hydda.Base,
options = providerTileOptions(minZoom = 2, maxZoom = 400)) %>%
clearBounds() %>%
setView(lng = 20, lat = 0, zoom = 2) %>%
addTiles() %>%
addRectangles(
lng1=marineRect$lng1, lat1=marineRect$lat1,
lng2=marineRect$lng2, lat2=marineRect$lat2,
color="#ffffff",
weight = 1, smoothFactor = 0.55,
opacity = 1.0, fillOpacity = 0.45,
fillColor = colorQuantile(gradiant_col_palette, marineRect$mean_genetic_diversity)(marineRect$mean_genetic_diversity)
)
})
}
###############################################################################
# load libraries
library(shiny)
library(leaflet)
###############################################################################
# UI
ui <- fluidPage(
leafletOutput("mymap",height=640,width=960),
p(),
actionButton("recalc", "New points")
)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment