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(plotly) library(ggplot2) library(plyr)
####################### ##Database Identifiers: DRV=RPostgres::Postgres() source(file = "~/Desktop/CODES/IRDTunaAtlas/credentials.R") source(file = "~/Bureau/CODES/IRDTunaAtlas/credentials.R") #################################################################################################################################################################################################################################### 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") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i3_SpeciesYearByGearMonth.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i4_SpeciesMonthByOcean.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i6_SpeciesMap.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i7_SpeciesMapRelativeCatches.R") source("https://raw.githubusercontent.com/juldebar/IRDTunaAtlas/master/R/TunaAtlas_i8_SpeciesMapRelativeCatchesOtherSpecies.R") ####################################################################################################################################################################################################################################
new_wkt <- 'POLYGON((-180 -90, 180 -90, 180 90, -180 90, -180 -90))' wkt <- reactiveVal(new_wkt) # Check http://jeffgoldsmith.com/p8105_f2017/shiny.html #Loading related dataset query_i1i2 <- ("select * from i1i2 ;") df_nominal <- dbGetQuery(con, query_i1i2) # head(df_nominal) # colnames(df_nominal) species = unique(df_nominal$species) gear_group = unique(df_nominal$gear_group) ocean = unique(df_nominal$ocean) query_i3 <- ("select year, month, c_esp AS species, c_g_engin AS gear_type, value, mean_prev_5_years, stddev_prev_5_years from i3 ;") df_i3 <- dbGetQuery(con, query_i3) gear_group_i3 = unique(df_i3$gear_type) year_i3 = unique(df_i3$year) # unique(df_i3$species) # unique(df_i3$year) # colnames(df_i3) # unique(df_i3$gear_type) # selectInput widget query_i4 <- ("select species, year, month, ocean, value from i4 ;") df_i4 <- dbGetQuery(con, query_i4) year_i4 = unique(df_i4$year) query_i6i7i8 <- ("select ogc_fid, geom_id, geom, year, species, country, value, count from i6i7i8;") df_i6i7i8 = st_read(con, query = query_i6i7i8) year_i6i7i8 = unique(df_i6i7i8$year) species_i6i7i8 = unique(df_i6i7i8$species) # selectInput widget selectInput("species_choice", label = h3("Select species for i1, i2 and i3 "), choices = species, selected = "SKJ") selectInput("species_choice_i8", label = h3("Select species for i8"), choices = species_i6i7i8, selected = c("SKJ","YFT"), multiple = TRUE) selectInput("ocean_choice", label = h3("Select ocean for i1"), choices = ocean, selected = "ICCAT",multiple = TRUE) selectInput("gear_choice", label = h3("Select gear(s) for i2"), choices = gear_group, selected = "PS",multiple = TRUE) selectInput("gear_choice_i3", label = h3("Select gear(s) for i3"), choices = gear_group_i3, selected = "PS",multiple = TRUE) selectInput("year_i3", label = h3("Select year for i3"), choices = year_i3, selected = "2000",multiple = FALSE) selectInput("year_choice_i6i7i8", label = h3("Select year for i3"), choices = year_i6i7i8, selected = c("2000","2001"),multiple = TRUE) # textOutput(wkt()) renderPrint({ paste0("Your current selection: ", input$species_choice," and ", input$gear_choice," and ", input$ocean_choice) paste0("Your current selection for i3 : ", input$species_choice," and ", input$gear_choice_i3," and ", input$year_i3) paste0("The area you have selected is (WKT format): ", wkt()) })
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,]))) coord <- 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,coord) textOutput("wkt") })
#unique(df_nominal$species) # nrow(df_nominal) # colnames(df_nominal) # unique(df_nominal$ocean) # Setting parameters (hard coded here) with selectInput widget 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({ # renderPlot({ target_species <- c("SKJ") target_gear <- c("PS") #Filtering input dataset with selected parameters colnames(df_nominal) # df_i2 <- df_nominal %>% filter(gear_group %in% target_gear,species %in% target_species) %>% dplyr::rename(gear_type=gear_group) 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(df_i2,yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", gearTypeAttributeName="gear_type", withSparql=FALSE) # i2 }) plotlyOutput("plot2")
# https://plotly.com/r/maps-sf/ # https://ggplot2.tidyverse.org/reference/ggsf.html output$plot8 <- renderPlotly({ # target_species <- c("BET","YFT","SKJ") # target_year <- c("2003","2004") # df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% target_year, species %in% target_species) df_i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_choice_i6i7i8, species %in% input$species_choice_i8) # colnames(df_i6i7i8) # df_i8_filtered <- as(df_i6i7i8_filtered, "Spatial") i8 <- plot_geo(df_i8_filtered) # i8 <- Atlas_i8_SpeciesMapRelativeCatchesOtherSpecies(df=df_i8_filtered, # targetedSpecies="YFT", # geomIdAttributeName="geom_id", # yearAttributeName="year", # speciesAttributeName="species", # valueAttributeName="value", # withSparql=FALSE # ) }) plotlyOutput("plot8")
output$plot7 <- renderPlotly({ df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_i3, species %in% input$species_choice) df_i7_filtered <- as(df_i6i7i8_filtered, "Spatial") i7 <- Atlas_i7_SpeciesMapRelativeCatches(df=df_i7_filtered, geomIdAttributeName="geom_id", yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) }) plotlyOutput("plot7")
output$plot6 <- renderPlotly({ df_i6i7i8_filtered <- df_i6i7i8 %>% filter(year %in% input$year_i3, species %in% input$species_choice) df_i6_filtered <- as(df_i6i7i8_filtered, "Spatial") i6 <- Atlas_i6_SpeciesMap(df=df_i6_filtered, geomIdAttributeName="geom_id", yearAttributeName="year", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) }) plotlyOutput("plot6")
# renderPlot({ output$plot4 <- renderPlotly({ df_i4_filtered <- df_i4 %>% filter(year %in% input$year_i3, ocean %in% input$ocean_choice, species %in% input$species_choice) i4 <- Atlas_i4_SpeciesMonthByOcean(df=df_i4_filtered, oceanAttributeName="ocean", yearAttributeName="year", monthAttributeName="month", speciesAttributeName="species", valueAttributeName="value", withSparql=FALSE ) # i4 }) plotlyOutput("plot4")
# target_species <- c("SKJ") # target_gear <- c("BB","PS","LL","Baitboat") # target_year <- c("2004") # Setting parameters (hard coded here) with selectInput widget # renderPlot({ output$plot3 <- renderPlotly({ # df_i3_filtered <- df_i3 %>% filter(gear_group %in% input$gear_choice,species %in% input$species_choice) %>% rename(gear_type=gear_group) # df_i3_filtered <- df_i3 %>% filter(year %in% target_year, gear_type %in% target_gear, species %in% target_species) df_i3_filtered <- df_i3 %>% filter(year %in% input$year_i3, gear_type %in% input$gear_choice_i3, species %in% input$species_choice) i3 <- Atlas_i3_SpeciesYearByGearMonth(df=df_i3_filtered, yearAttributeName="year", monthAttributeName="month", speciesAttributeName="species", gearTypeAttributeName="gear_type", valueAttributeName="value", meanPrev5YearsAttributeName="mean_prev_5_years", stddevPrev5YearsAttributeName="stddev_prev_5_years", withSparql=FALSE ) i3 }) plotlyOutput("plot3")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.