闪亮的应用程序没有生成地图

时间:2016-02-27 02:47:58

标签: r layout shiny

我有一个庞大的数据集,但即使我只使用5个数据点,我的地图也没有生成。下面显示的代码运行良好,但我没有得到地图。我可能错过了一个非常小的观点,并希望得到一些帮助。

library(shiny)
library(leaflet)
library(data.table)

dd<- data.table(entitynum=c(400, 201, 602, 304,401),
            londd=c(42.3, 42.4, 43.5, 42.6,42.2),
            latdd=c(-71.1, -71.2, -71.3, -71.4,-71),
            DAY_WEEK=c("Sunday", "Monday", "Tuesday", "Wednesday","Thursday"))

#Set up ui
ui <- shinyUI(fluidPage(title="",

#App title 
titlePanel(h3("My tool", align="left")),

#App layout
sidebarLayout(position="left",

#App sidePanel content and styles
sidebarPanel(h5("Hello", width=2),
checkboxGroupInput(inputId="InFlags", label=h4("DAYS"), 
choices=setNames(object=c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"),
 nm=c("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))),
position="left"),

#App mainPanel content and styles
 mainPanel(fluidRow(leafletOutput(outputId="lmap", width="100%",height = "100%")))

)
)
)



server <- function(input, output){


#Build leaflet map
lmap <- leaflet(data=dd)%>%
#addTiles()%>% 
#setView(lng="-71.094824",lat="42.343479",zoom=10) %>%
addProviderTiles(provider="MapQuestOpen.OSM")
fitBounds(~min(londd), ~min(latdd), ~max(londd), ~max(latdd))

 #Filter data
 datFilt <- reactive(dd[DAY_WEEK%in%input$InFlags])

#Add markers based on selected flags
observe({
 if(nrow(datFilt())==0) {
   print("Nothing selected")
   leafletProxy("lmap") %>% clearMarkerClusters()
 }
 else{
  print(paste0("Selected: ", unique(input$InFlags)))
  leafletProxy("lmap", data=datFilt()) %>%
    clearMarkerClusters() %>%
    addCircleMarkers(lng=~londd, lat=~latdd,
                     clusterOptions=markerClusterOptions(), weight=3,
                     color="#33CC33", opacity=1, fillColor="#FF9900", 
                     fillOpacity=0.8)
}
})

output$lmap <- renderLeaflet(lmap)
}

1 个答案:

答案 0 :(得分:1)

您的height参数不能是百分比。

来自the shiny website

  

请注意,对于高度,使用“auto”或“100%”通常不会按预期工作,因为使用HTML / CSS计算高度

使用类似:

leafletOutput(outputId="lmap", width="100%", height = 500)))

leafletOutput(outputId="lmap", width="100%", height = "500px")))

此外,您的地图代码需要调整。 setView中的纬度/经度需要为数字

lmap <- leaflet(data=dd)%>%
setView(lng=-71.094824,lat=42.343479,zoom=10) %>%
addProviderTiles(provider="MapQuestOpen.OSM") 

或者,如果您要使用fitBounds,则需要管道%>%

lmap <- leaflet(data=dd)%>%
    #addTiles()%>% 
#    setView(lng=-71.094824,lat=42.343479,zoom=10) %>%
    addProviderTiles(provider="MapQuestOpen.OSM") %>%
  fitBounds(~min(londd), ~min(latdd), ~max(londd), ~max(latdd))