对于R中的小叶坐标的循环

时间:2017-05-21 22:44:02

标签: r shiny leaflet dashboard

我在使用坐标数据框向我的传单地图添加标记的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)`

1 个答案:

答案 0 :(得分:1)

我认为代码是在每个%>%语句后缺少addMarker语句的问题。我建议删除for循环,然后只有:

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