0

I'm running into quite a strange problem in r markdown & r leaflet. R markdown table of content (TOC) is including the 'addMeasure' function in the TOC as 'Measure distances and areas', not once, but twice. Whilst also moving within the document, the highlighter on the TOC stays on the Leaflet map section with the addMeasure function within it, or if in another section, TOC highlighter jumps back to it. I have removed the addMeasure function within all maps (5 so far), and readded to one to try and work out where the problem lies, with no success.

TOC works perfectly without element 'addMeasure'.

Code:

---
title: "My Report"
author: "My name"
date: "`r Sys.Date()`"
output:
  html_document:
    toc: true
    toc_float: true
    toc_collapsed: true
    smooth_scroll: false
    toc_depth: 3
    number_sections: true
    theme: cerulean
    fig_caption: true
    df_print: paged
editor_options: 
  markdown: 
    wrap: 72
---
setwd("~/file_path")
knitr::opts_chunk$set(echo = TRUE)
library(leaflet)
library(leaflet.extras)
library(leaflet.extras2)
library(rgdal)
library(sf)
library(dplyr)
library(RColorBrewer)
library(htmlwidgets)
library(htmltools)
library(webshot)
library(pandoc)
library(rmarkdown)
# Summary

xxx

# QGIS

xxx

# R Leaflet

Explain leaflet maps

# Wave data

## Data sources & details

xxx

<br>
Data collected: XXX

## QGIS Layers

xxx

## Shapefile data

xxx

## GIS data manipulation

xxx

## Leaflet map

xxx


wave_data <- read_sf("~/file_path")

wave_data <- st_transform(wave_data, crs = '+proj=longlat +datum=WGS84')

wave_data$An_mn_P_OD <- round(wave_data$An_mn_P_OD, 0)

wave_data_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)

binsDepth <- c(0, 25, 50, 100, 150, 200, 300, 400, 500, 1000, 3100)
palDepth <- colorBin("RdYlBu", domain = range(wave_data$Ave_Depth), bins = binsDepth)

binsEnergy <- c(0, 5, 10, 15, 20, 30, 40, 50, 60, 70, 75)
palEnergy <- colorBin("RdYlBu", domain = wave_data$An_mn_P_OD, bins = binsEnergy)

wave_data_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
   addPolygons(data = wave_data, 
            weight = 1,
            smoothFactor = 0.5,
            color = "white", 
            fillOpacity = 0.5, 
            fillColor = ~palDepth(Ave_Depth),
            group = "Average Depth") %>% 
   addPolygons(data = wave_data, 
            weight = 1,
            smoothFactor = 0.5,
            color = "white", 
            fillOpacity = 0.5, 
            fillColor = ~palEnergy(An_mn_P_OD),
            group = "Wave Power") %>% 
   addLayersControl(
      overlayGroups = c("Average Depth", "Wave Power"),
      options = layersControlOptions(collapsed = FALSE)) %>% 
   addScaleBar(position = "bottomright",
      options = scaleBarOptions(imperial = FALSE)) %>% 
   addLegend(pal = palDepth, 
          values = wave_data$Ave_Depth, 
          title = "Average depth (m)",
          opacity = 0.7, 
          position = "bottomright",
          layerId = "Ave_Depth") %>% 
   addLegend(pal = palEnergy, 
          values = wave_data$An_mn_P_OD, 
          title = "Wave Power (kW/m)",
          opacity = 0.7, 
          position = "topleft", 
          layerId = "An_mn_P_OD")

wave_data_map
# Grid

## Data sources & details:

xxx

Data collected: XXX

## QGIS Layers:

xxx

## Shapefile data

xxx

## GIS data manipulation:

xxx

## Leaflet map

xxx

cables <- read_sf("~/file_path")

cables <- st_transform(cables, crs = '+proj=longlat +datum=WGS84')

cables$An_mn_P_OD <- round(cables$An_mn_P_OD, 0)

binsEnergy <- c(0, 5, 10, 15, 20, 30, 40, 50, 60)
palEnergyCables <- colorBin("RdYlBu", domain = cables$An_mn_P_OD, bins = binsEnergy)

substations <- read_sf("~/file_path")

substations <- st_transform(substations, crs = '+proj=longlat +datum=WGS84')

substations$An_mn_P_OD <- round(substations$An_mn_P_OD, 0)

binsEnergy <- c(0, 5, 10, 15, 20, 30, 40, 50, 60)
palEnergySubstations <- colorBin("RdYlBu", domain = substations$An_mn_P_OD, bins = 
binsEnergy)

cable_labels <- read_sf("~/file_path")

cable_labels <- st_transform(cable_labels, crs = '+proj=longlat +datum=WGS84')

cable_labels_df <- st_as_sf(cable_labels) # convert to spatial data frame

cable_labels_df_popup <- paste("Cable",  "<br>",
                            "Name: ", ifelse(is.na(cable_labels_df$name) | 
is.null(cable_labels_df$name), "N/A", cable_labels_df$name), "<br>", 
                            "Operator: ", ifelse(is.na(cable_labels_df$operator) | 
is.null(cable_labels_df$operator), "N/A", cable_labels_df$operator), "<br>",
                            "Location: ", ifelse(is.na(cable_labels_df$location) | 
is.null(cable_labels_df$location), "N/A", cable_labels_df$location), "<br>",
                            "Voltage: ", ifelse(is.na(cable_labels_df$voltage) | 
is.null(cable_labels_df$voltage), "N/A", cable_labels_df$voltage), "<br>")


substation_labels <- read_sf("~/file_path")

substation_labels <- st_transform(substation_labels, crs = '+proj=longlat 
+datum=WGS84')

substation_labels <- st_geometry(st_centroid(substation_labels))

substation_labels_popup <- paste("Substation",  "<br>",
                              "Name: ", ifelse(is.null(substation_labels$name), "N/A", 
substation_labels$name), "<br>", 
                              "Substation: ", 
ifelse(is.null(substation_labels$substation), "N/A", substation_labels$substation), " 
<br>",
                              "Voltage: ", ifelse(is.null(substation_labels$voltage), 
"N/A", substation_labels$voltage), "<br>")

Ports <- read_sf("~/file_path")

Ports <- st_transform(Ports, crs = '+proj=longlat +datum=WGS84')

Ports_points <- st_point_on_surface(Ports)

Ports_points <- st_set_geometry(Ports_points, Ports$geometry)

Ports <- st_sf(Name = Ports$Name, 
            descriptio = ifelse(is.na(Ports$descriptio), "Port", Ports$descriptio),
            label = paste("Port", "<br>", 
                          "Name: ", ifelse(is.na(Ports$Name), "N/A", Ports$Name), " 
<br>", 
                          "Description: ", ifelse(is.na(Ports$descriptio), "Port", 
Ports$descriptio), "<br>"),
            geometry = st_geometry(Ports_points))

Ports$longitude <- st_coordinates(Ports_points)[, "X"]
Ports$latitude <- st_coordinates(Ports_points)[, "Y"]

Port_labels <- paste("Port", "<br>", 
                  "Name: ", ifelse(is.na(Ports$Name), "N/A", Ports$Name), "<br>", 
                  "Description: ", ifelse(is.na(Ports$descriptio), "Port", 
Ports$descriptio), "<br>")

grid_map <- leaflet() %>% 
   addProviderTiles(providers$Esri.WorldTopoMap) %>% 
   setView(lng = -4.2026458, lat = 56.4906712, zoom = 5)
grid_map

grid_map <- leaflet() %>% 
      addProviderTiles(providers$Esri.WorldTopoMap) %>% 
      setView(lng = -4.2026458, lat = 56.4906712, zoom = 5) %>% 
   addPolygons(data = cables, 
            fillOpacity = 0.5, 
            fillColor = ~palEnergyCables(An_mn_P_OD),
            stroke = FALSE,
            group = "Cables") %>% 
   addPolygons(data = substations, 
            fillOpacity = 0.5, 
            fillColor = ~palEnergySubstations(An_mn_P_OD),
            stroke = FALSE,
            group = "Substations") %>% 
   addPolylines(data = cable_labels_df, 
             fillOpacity = 1, 
             fillColor = "red",
             weight = 1,
             stroke = TRUE,
             group = "Cable_labels", 
             label = lapply(cable_labels_df_popup, HTML)) %>% 
   addCircleMarkers(data = substation_labels,
                 radius = 2,
                 fillOpacity = 1, 
                 fillColor = "red",
                 weight = 1,
                 stroke = FALSE,
                 group = "Subtation_labels", 
                 label = lapply(substation_labels_popup, HTML)) %>% 
   addCircleMarkers(data = Ports, 
                 radius = 2, 
                 fillOpacity = 1, 
                 fillColor = "black",
                 weight = 1,
                 stroke = FALSE,
                 label = lapply(Port_labels, HTML)) %>% 
   addLayersControl(
      overlayGroups = c("Substations", "Cables"),
      options = layersControlOptions(collapsed = FALSE)) %>%
   addScaleBar(position = "bottomright",
            options = scaleBarOptions(imperial = FALSE)) %>%
   addMeasure(position = "topright",
           primaryLengthUnit = "meters",
           secondaryLengthUnit = "kilometers",
           primaryAreaUnit = "sqmeters") %>%
   addDrawToolbar(position = "topright") %>% 
   addLegend(pal = palEnergyCables, 
          values = cables$An_mn_P_OD, 
          title = "Wave Power (kW/m)",
          opacity = 0.7, 
          position = "topleft", 
          layerId = "An_mn_P_OD")
grid_map
grid_map
# Renewable energy infrastucture

## Data sources & details:

xxx

Data collected: XXX

## QGIS Layers:

xxx

## Shapefile data

xxx

## GIS data manipulation:

xxx

## Leaflet map

xxx

# OGTC infrastructure

## Data sources & details:

xxx

Data collected: XXX

## QGIS Layers:

xxx

## Shapefile data

xxx

## GIS data manipulation:

xxx

## Leaflet map

xxx

# Vessel traffic

## Data sources & details:

xxx

Data collected: XXX

## QGIS Layers:

xxx

## Shapefile data

xxx

## GIS data manipulation:

xxx

## Leaflet map

xxx

# Fishing

## Data sources & details:

xxx

Data collected: XXX

## QGIS Layers:

xxx

## Shapefile data

xxx

## GIS data manipulation:

xxx

## Leaflet map

xxx

# Discussion

# Conclusions

# Bibliography
  • OK, have removed the extra TOC sections using js code: $(document).ready(function() { setTimeout(function() { $("#TOC li:contains('Measure distances and areas')").remove(); }, 1000); // 1000 milliseconds (1 second) delay }); However, the scroll function in R markdown, which highlights the section that you are in within the document, is stuck on section 5.5 - Leaflet map, within protected areas. How also can I get the document when it loads to start from the beginning of the document? – AlexaQuinoa Mar 19 '23 at 19:39

0 Answers0