leafletR map在启动时不会加载闪亮

时间:2015-04-28 16:29:00

标签: leaflet shiny

我有以下一小段代码(更少描述HERE) - 我想控制滑块中闪烁显示的点数。您可以看到初始地图在一段时间后加载(监视控制台输出),但只有在您使用滑块一次后才会显示。

但是我希望地图能够在启动闪亮应用程序之后显示出来 - 任何提示如何做到这一点?

## app.R ##
library(shiny)
library(shinydashboard)
library(httr)
library(leafletR)

data(quakes)

# dest_dir=tempdir()
dest_dir="foo_map"

dest_file = paste(dest_dir,"quakes","quakes.html",sep="\\")


dat = quakes

createMapHTML <- function(inputFreq=1) {

  q.dat <- toGeoJSON(data=dat[seq(from = 1, to = nrow(dat), by=inputFreq), ], 
                     dest=dest_dir, name="quakes")

  sty <- styleSingle(col="darkblue", fill="darkblue", rad=6)

  # create map
  q.map <- leaflet(data=q.dat, dest=dest_dir,  size = c(1200, 600), incl.data=TRUE,
                   base.map=list("osm"), style=sty, popup="*", controls="all")
}

# createMapHTML()

runApp(list(
ui = dashboardPage(
  dashboardHeader(title = "quakes"),
  dashboardSidebar(
    sliderInput("slider", "#observations frequency:", 1, 100, 1)
  ),
  dashboardBody(
    htmlOutput("inc")
  )
),
server = function(input, output, session) {

  createMap <- reactive({
    createMapHTML(input$slider) 
    return(includeHTML(dest_file))
  })

  output$inc<-renderUI({ createMap() })
}
))

1 个答案:

答案 0 :(得分:1)

因此leafletR包的瓶颈是转换为GeoJson。此外,用于嵌入html的“includeHTML&amp; htmlOutput”解决方法很简单..

为了避免这两种情况,我只是切换到传单包装:

## app.R ##
library(shiny)
library(shinydashboard)
library(leaflet)

data(quakes)
dat = quakes


runApp(list(
  ui = dashboardPage(
    dashboardHeader(title = "quakes"),
    dashboardSidebar(
      sliderInput("slider", "#observations frequency:", 1, 100, 1)
    ),
    dashboardBody(
        leafletOutput("map", height = 600)
    )
  ),
  server = function(input, output) {

    output$map <- renderLeaflet({

      map <- leaflet() %>% addTiles()

      map %>% addCircles(data=dat[seq(from = 1, to = nrow(dat), by=input$slider), ],   #input$slider
                         lat = ~lat, lng = ~long, fillOpacity = 1.0)
    })
  }
))