我在使用 for 循环使用坐标数据框向传单地图添加标记时遇到问题。下面是我的代码(如您所见,最后的 for 循环不起作用,返回错误“4 个参数传递给 'for',需要 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)`
我认为代码存在问题,因为每个
%>%
语句后面都缺少 addMarker
语句。我建议删除 for
循环并只具有:
output$mymap <- renderLeaflet({
leaflet() %>%
addTiles() %>%
addMarkers(lat = latVector[1:4308, 1], lng = longVector[1:4308, 1])
})