library(flexdashboard) library(shiny) library(dplyr) library(XML) library(DBI) library(RPostgres) library(RPostgreSQL) library(dplyr) library(leaflet) library(leaflet.extras) library(leafpm) library(mapedit) library(pacman) library(sf) library(sp) library(ggplot2) library(plyr) library(plotly)
####################### ##Database Identifiers: source(file = "~/Desktop/CODES/IRDTunaAtlas/credentials.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i4_SpeciesMonthByOcean.R")
wkt <- reactiveVal('POLYGON((-180 -90, 180 -90, 180 90, -180 90, -180 -90))') new_wkt <- 'POLYGON((-180 -90, 180 -90, 180 90, -180 90, -180 -90))' query_i4 <- "select species, year, month, ocean, value from public.i4 ;" df_i4 <- st_read(con, query=query_i4) species = unique(df_i4$species) year = unique(df_i4$year) # selectInput widget selectInput("species_choice", label = h3("Select species for i4 "), choices = species, selected = c("SKJ","YFT","BET"),multiple = TRUE) selectInput("year_choice", label = h3("Select year for i4"), choices = year, selected = c("2000","2001","2002"),multiple = TRUE) # textOutput(wkt()) renderPrint({ paste0("Your current selection for i4: ", input$species_choice," and ",input$year_choice) query_i4 <- paste0("SELECT species, ocean, year, month, sum(value) as value, unit, st_collect(geom) FROM public.i4_spatial WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) GROUP BY species, ocean, year, month,unit;") })
To be done : add all WMS layers of input datasets
output$mymap <- renderLeaflet( leaflet() %>% addTiles() %>% # addPmToolbar( # toolbarOptions = pmToolbarOptions(drawMarker = FALSE, position = "topright"), # drawOptions = pmDrawOptions(snappable = FALSE, allowSelfIntersection = FALSE), # editOptions = pmEditOptions(preventMarkerRemoval = TRUE, draggable = FALSE), # cutOptions = pmCutOptions(snappable = FALSE, allowSelfIntersection = FALSE) # ) %>% addDrawToolbar( targetGroup = "draw", editOptions = editToolbarOptions( selectedPathOptions = selectedPathOptions() ) ) %>% setView(lng =48, lat =-8, zoom = 5 ) %>% addWMSTiles( # "http://thredds.oreme.org:8080/thredds/wms/MARBEC/Seychelles/global-reanalysis-phy-001-030-monthly_SEY_t55_199301-201906.nc", "https://geoserver-sdi-lab.d4science.org/geoserver/sdilab_geoflow/ows?service=WMS", layers = "global_catch_5deg_1m_ird_level0", # layers = "thetao", options = WMSTileOptions(format = "image/png", transparent = TRUE), group ="Seatizen", attribution = "Seatizen WMS" ) %>% addLayersControl( overlayGroups = c("draw"), options = layersControlOptions(collapsed = FALSE) ) ) leafletOutput('mymap') observe({ #use the draw_stop event to detect when users finished drawing feature <- input$mymap_draw_new_feature req(input$mymap_draw_stop) print(feature) polygon_coordinates <- input$mymap_draw_new_feature$geometry$coordinates[[1]] # see https://rstudio.github.io/leaflet/shiny.html bb <- input$mymap_bounds geom_polygon <- input$mymap_draw_new_feature$geometry # drawn_polygon <- Polygon(do.call(rbind,lapply(polygon_coordinates,function(x){c(x[[1]][1],x[[2]][1])}))) geoJson <- geojsonio::as.json(feature) # spdf <- geojsonio::geojson_sp(feature) geom <- st_read(geoJson) wkt(st_as_text(st_geometry(geom[1,]))) new_wkt <- st_as_text(st_geometry(geom[1,])) north <- polygon_coordinates[[1]][[1]] south <- polygon_coordinates[[2]][[1]] east <- polygon_coordinates[[1]][[2]] west <- polygon_coordinates[[2]][[2]] if(is.null(polygon_coordinates)) return() text<-paste("North ", north, "South ", east) mymap_proxy = leafletProxy("mymap") %>% clearPopups() %>% addPopups(south,west,new_wkt) textOutput("wkt") })
# renderPlotly({ renderPlot({ # https://mrjoh3.github.io/2018/12/26/shiny-image-gallery-examples/ # https://mrjoh3.github.io/p/shiny-image-gallery-examples/ if(wkt()!=new_wkt){ df_i4 = st_read(con, query = paste0("SELECT species, ocean, year, month, sum(value) as value, unit, st_collect(geom) FROM public.i4_spatial WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) GROUP BY species, ocean, year, month, unit ;")) } #Filtering input dataset with selected parameters df_i4_filtered <- df_i4 %>% filter(year %in% input$year_choice, species %in% input$species_choice) # df_i4_filtered <- df_i4 %>% filter(year %in% input$year_choice, ocean %in% input$ocean_choice, species %in% input$species_choice) i4 <- Atlas_i4_SpeciesMonthByOcean(df=as.data.frame(df_i4_filtered), oceanAttributeName="ocean", yearAttributeName="year", monthAttributeName="month", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) i4 }) # plotlyOutput("plot4")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.