Search code examples
rshinyr-leaflet

For loop for leaflet coordinates


I am having trouble with my for loop for adding markers to my leaflet map with a data frame of coordinates. Below is my code (as you can see the for loop at the end is not functioning, returning the error "4 arguments passed to 'for' which requires 3"):`

library(shiny)
library(shinydashboard)
library(devtools)
library(leaflet)
library(DT)
library(ggplot2)
library(dplyr)
library(tidyverse)
library(heatmaply)
library(shinyHeatmaply)
library(markdown)
library(ggthemes)

r_colors <- rgb(t(col2rgb(colors()) / 255))
names(r_colors) <- colors()

demodata <- read.csv("demodata.csv")
colnames(demodata)[1] <- "Region"

lpidata <- read.csv("LPIdata.csv")

tp2data <- demodata[24:25]


# Define UI for application that draws a histogram
ui <- dashboardPage(
  dashboardHeader(title = "NOAA Puerto Rico Coral Data", titleWidth = 2000),
  dashboardSidebar(sidebarMenu(
    menuItem("Visualization", tabName = "dashboard", icon = icon("line-chart")),
    menuItem("Data", tabName = "widgets", icon = icon("table")), 
    menuItem("Map", tabName = "map", icon = icon("map-marker")), 
    selectInput(inputId = "Lucifer", "X-axis", choices = c("MAXIMUM DIAMETER", "PERPENDICULAR DIAMETER", "HEIGHT")), 
    selectInput(inputId = "lucifer", "y-axis", choices = c("HEIGHT", "PERPENDICULAR DIAMETER", "MAXIMUM DIAMETER"))
  )),
  dashboardBody(
    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
              fluidRow(column(3), 
                box(plotOutput("plot5", height = 800, width = 800)))), 
      tabItem(tabName = "widgets", 
              fluidRow(
                box(dataTableOutput("dtbl"), width = "100%", height = 900, server = TRUE, div(style = 'overflow-x: scroll', DT::dataTableOutput('table'))))), 
      tabItem(tabName = "map", fluidRow(
        box(
              leafletOutput("mymap"), width = 12, height = "100%"))
      ))))



# Define server logic required to draw a histogram
server <- function(input, output) {

  output$plot5 <- renderPlot(ggplot(data = demodata) +
                               geom_smooth(mapping = aes(x = MAX_DIAMETER, y = HEIGHT), fill = "blue") +
                               xlab("Maximum Diameter") +
                               ylab("Height") +
                               theme_stata(base_size = 16)) 


  output$dtbl <- renderDataTable(demodata, width ="100%", options = list(scrollX = TRUE))
  latVector <- as.vector(demodata["LAT_DEGREES"])
  longVector<- as.vector(demodata["LON_DEGREES"])
  output$mymap <- renderLeaflet({
    leaflet() %>%
      addTiles() %>%
        for (i in 1:4308){
         addMarkers(lat = latVector[i, 1], lng = longVector[i, 1])
        }






  })


  }

# Run the application
shinyApp(ui = ui, server = server)`

Solution

  • I think the code is havig issues with the lack of a %>% statement after each addMarker statement. I would suggest removing the for loop and just having:

      output$mymap <- renderLeaflet({
        leaflet() %>%
          addTiles() %>%
          addMarkers(lat = latVector[1:4308, 1], lng = longVector[1:4308, 1])
      })