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(plotly) library(plyr) library(streamgraph)
####################### ##Database Identifiers: tunaatlas / db-tunaatlas # source(file = "~/Desktop/CODES/IRDTunaAtlas/credentials.R") source(file = "~/Bureau/CODES/IRDTunaAtlas/credentials.R") codes_github_repository <- "https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/" codes_directory=codes_github_repository # query <- paste(readLines(paste0(codes_directory,"SQL/select_annotated_images_attributes.sql")), collapse=" ") #################################################################################################################################################################################################################################### source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i1_SpeciesByOcean.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i2_SpeciesByGear.R")
new_wkt <- 'POLYGON((-180 -90, 180 -90, 180 90, -180 90, -180 -90))' wkt <- reactiveVal(new_wkt) query_i1i2 <- ("select * from i1i2 ;") df_nominal <- st_read(con, query = query_i1i2) species = unique(df_nominal$species) gear_group = unique(df_nominal$gear_group) ocean = unique(df_nominal$ocean) # selectInput widget selectInput("species_choice", label = h3("Select species for i1, i2 and i3 "), choices = species, selected = c("SKJ","BET","BFT", "YFT"),multiple = TRUE) selectInput("ocean_choice", label = h3("Select ocean for i1"), choices = ocean, selected = c("ICCAT","IOTC","IATTC", "CCSBT"),multiple = TRUE) selectInput("gear_choice", label = h3("Select gear(s) for i2"), choices = gear_group, selected = c("PS","BB"),multiple = TRUE) # textOutput(wkt()) renderPrint({ paste0("Your current selection for i1 and i2: ", input$species_choice," and ", input$gear_choice," and ", input$ocean_choice) paste0("The area you have selected is (WKT format): ", wkt()) query_i1i2 <- paste0("SELECT sum(value) as value, unit, ocean, gear_group, year, species, st_collect(geom) FROM public.i1i2_spatial WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) GROUP BY unit, ocean, gear_group, year, species ;") })
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") })
output$plot1 <- renderPlotly({ #unique(df_nominal$species) # nrow(df_nominal) # colnames(df_nominal) # unique(df_nominal$ocean) # Setting parameters (hard coded here) with selectInput widget if(wkt()!=new_wkt){ df_nominal = st_read(con, query = paste0("SELECT sum(value) as value, unit, ocean, gear_group, year, species, st_collect(geom) FROM public.i1i2_spatial WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) GROUP BY unit, ocean, gear_group, year, species ;")) } #Filtering input dataset with selected parameters df_i1 <- df_nominal %>% filter(species %in% input$species_choice,ocean %in% input$ocean_choice) i1 <- Atlas_i1_SpeciesByOcean(as.data.frame(df_i1), yearAttributeName="year", oceanAttributeName="ocean", speciesAttributeName="species", valueAttributeName="value") }) plotlyOutput("plot1")
output$plot2 <- renderPlotly({ # target_species <- c("SKJ") # target_gear <- c("PS") #Filtering input dataset with selected parameters # df_i2 <- df_nominal %>% filter(gear_group %in% target_gear,species %in% target_species) %>% dplyr::rename(gear_type=gear_group) if(wkt()!=new_wkt){ df_nominal = st_read(con, query = paste0("SELECT sum(value) as value, unit, ocean, gear_group, year, species, st_collect(geom) FROM public.i1i2_spatial WHERE ST_Within(geom,ST_GeomFromText('",wkt(),"',4326)) GROUP BY unit, ocean, gear_group, year, species ;")) } #Filtering input dataset with selected parameters df_i2 <- df_nominal %>% filter(gear_group %in% input$gear_choice,species %in% input$species_choice) %>% dplyr::rename(gear_type=gear_group) i2 <- Atlas_i2_SpeciesByGear(as.data.frame(df_i2), yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", gearTypeAttributeName="gear_type", withSparql=FALSE) }) plotlyOutput("plot2")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.