0

I am writing a Leaflet map in R and integrating it with shiny. I have three questions to ask and the code will be at the bottom with the problems highlighted:

  1. On this map, I have random markers, each representing an aquatic environment. I also have a drop-down list allowing you to select the specific environment you want, which will only select those markers corresponding to the environment. I have created the absolutePanel which allows you to do this but cannot get the script to select for the markers using the reactive function.

  2. Not an important factor, but will be useful. I have highlighted the countries that contain the markers, but when you move the slider to select for the years and corresponding markers you want to view, "empty" countries still remain. As the markers are removed based on the year, I want the countries no longer containing markers to be highlighted. Also it seems very slow.

  3. Only for interest sake, but is there a map like "OpenStreetMap.Mapink" that is completely in English?

Below is the data file linked, as well as the script for the map:

https://drive.google.com/drive/folders/10anPY-I-B13zTQ7cjUsjQoJDcMK4NCXb?usp=sharing

library(shiny)
library(leaflet)
library(maps)
library(htmltools)
library(htmlwidgets)
library(dplyr)


###############################

map_data  <- read.csv("example1.csv", header = TRUE)

countries <- map_data %>%
  distinct(DOI, Country.s., .keep_all = TRUE)

area_data <- map_data %>%
  filter(Area.Site == "Area")

site_data <- map_data %>% 
  filter(Area.Site == "Site")

sampling_count <- count(site_data, "Country.s.")
country_count <- count(countries, "Country.s.")

bounds <- map("world", area_data$Country.s., fill = TRUE, plot = FALSE)

bounds$studies <- country_count$freq[match(gsub("\\:.*", "", bounds$names), country_count$Country.s.)]
bounds$sampling_points <- sampling_count$freq[match(gsub("\\:.*", "", bounds$names), sampling_count$Country.s.)]
bounds$year <- site_data$Publication_Year[match(gsub("\\:.*", "", bounds$names), site_data$Country.s.)]


ui <- bootstrapPage(
  tags$style(type = "text/css", "html, body {width:100%;height:100%}"),
  leafletOutput("map", 
                width = "100%",
                height = "100%"),
  ################################
  #Question 1 
  ################################
  absolutePanel(top = 5, right = 320,
                selectInput("environment", "Sampling Source: ",
                            c("All" = "P&C",
                              "Surface Water" = "SW",
                              "Wastewater" = "WW",
                              "Sea Water" = "Sea"))),
  ################################
  #Question 1 
  ################################
  absolutePanel(bottom = 5, right = 320,
                sliderInput("year", "Publication Year(s)", min(site_data$Publication_Year), max(site_data$Publication_Year),
                            value = range(site_data$Publication_Year), step = 1, sep = "", width = 500))
)


server <- function(input, output, session) {

  marker_data <- reactive({
    site_data[site_data$Publication_Year >= input$year[1] & site_data$Publication_Year <= input$year[2],]
  })

  area_s_data <- reactive({
    area_data[area_data$Publication_Year >= input$year[1] & area_data$Publication_Year <= input$year[2],]
  })

  border_data <- reactive({
    bounds[bounds$year >= input$year[1] & bounds$year  <= input$year[2],]
  })



  output$map <- renderLeaflet({
    leaflet(map_data, options = leafletOptions(worldCopyJump = TRUE)) %>%
################################
#Question 3
################################
      addProviderTiles("OpenStreetMap.Mapnik")
################################
#Question 3
################################

  })

  observe({

    leafletProxy("map", data = marker_data()) %>%
      clearMarkers() %>%
      addAwesomeMarkers(lat = ~Latitude,
                        lng = ~Longitude,
                        label = ~paste(Aquatic_Environment_Type))

  })
  ################################
  #Question 2
  ################################
  observe({

    leafletProxy("map", data = area_s_data()) %>%
      clearShapes() %>%
      addCircles(lat = ~Latitude, 
                 lng = ~Longitude,
                 radius = ~as.numeric(Area_Radius_Meter),
                 color = "blue",
                 weight = 1,
                 highlightOptions = highlightOptions(color = "red",
                                                     weight = 2,
                                                     bringToFront = TRUE)) %>%
      addPolygons(data = bounds,
                  color = "red", 
                  weight = 2, 
                  fillOpacity = 0.1,
                  highlightOptions = highlightOptions(color = "black", 
                                                      weight = 2,
                                                      bringToFront = TRUE))
    ################################
    #Question 2
    ################################

  })

}

shinyApp(ui, server)
5
  • In addPolygons(data = bounds should not be addPolygons(data = border_data()?
    – MLavoie
    May 11, 2018 at 14:07
  • You are correct, however then the script won't run!
    – user8944445
    May 11, 2018 at 14:07
  • Yes you need to fix border_data first.
    – MLavoie
    May 11, 2018 at 14:10
  • How exactly would I achieve this?
    – user8944445
    May 11, 2018 at 14:11
  • for Q2, you could use border_data <- reactive({ bounds <- map("world", area_s_data()$Country.s., fill = TRUE, plot = FALSE) })
    – MLavoie
    May 12, 2018 at 8:57

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.