2016-08-22 135 views
2

虽然代码在闪亮之外自行工作,但我无法获取传单地图以在我的闪亮应用中渲染。我没有得到任何错误,所以我卡住了,任何帮助表示赞赏。Shiny/Leaflet map not rendering

样本数据:

cleanbuffalo <- data.frame(name = c("queen","toni","pepper"), 
          longitude = c(31.8,32,33), 
          latitude = c(-24,-25,-26)) 

闪亮UI:

vars <- c(
    "Pepper" = "pepper", 
    "Queen" = "queen", 
    "Toni" = "toni" 
) 

shinyUI(navbarPage("Buffalo Migration", id ="nav", 

    tabPanel("Interactive Map", 

div(class="outer", 

    leafletOutput("map", width = "100%", height = "100%"), 
    #Panel Selection 
    absolutePanel(id = "controls", class = "panel panel-default", fixed = TRUE, 
     draggable = TRUE, top = 60, left = "auto", right = 20, bottom = "auto", 
     width = 330, height = "auto", 

     h2("Buffalo Migration"), 
     #Buffalo Group selection 
     checkboxGroupInput(
     "checkGroup", label = h3("Select Buffalo to Follow"), 
     choices = vars, 
     selected = 1) 
     ) 
     ) 
    ) 
) 
) 

闪亮服务器:

library(shiny) 
library(dplyr) 
library(leaflet) 
library(scales) 
library(lattice) 

shinyServer(function(input, output, session) { 
    output$map <- renderLeaflet({ 
    leaflet() %>% addTiles() %>% setView(lng = 31.88, lat = -25.02, zoom=1) 
    }) 

回答

4

它不会因为leafletOutput的height参数的工作。奇怪的是,如果你在%中指定它,地图不会显示出来,但是如果你使用“px”(或者一个将被强制转换为字符串并且附加有“px”的数字),它确实可以正常工作。

leafletOutput("map", width = "75%", height = "500px")产量:

enter image description here

我不知道为什么会这样,但如果你想在%指定leafletOutput的高度,你可以把它包装成一个div,并给予适当的高度。


默认情况下,宽度设置为100%,高度设置为400px。所以,你不必指定这些参数 - 只有当我想改变输出的大小时才会这样做。

leafletOutput(outputId, width = "100%", height = 400) 
+1

这解决了我的问题,谢谢。我从来没有想到这是问题。 – josh453

+1

'?shiny :: plotOutput'中的文档给出了原因:“请注意,对于高度,使用”auto“或”100%“通常无法按预期工作,因为如何使用HTML/CSS计算高度。” – SymbolixAU

+1

加上几个github问题[这里](https://github.com/rstudio/shiny/issues/705)[和这里](https://github.com/rstudio/shiny/issues/650),for例子进一步解释 – SymbolixAU