放大时没有渲染的Shiny中的Leaflet地图;保持静止

时间:2016-04-01 16:50:58

标签: r shiny leaflet

如上所述,我正在使用闪亮的仪表板。放大时传单地图不会渲染。这是代码。它正在绘制给定纬度上的标记,长。放大时,它无法正常工作。并且看起来是静态的,放大时不会进行渲染。需要尽快提供帮助。

server.R

 mapPlot <- function(searchTerm, maxTweets, lang, lat, long, rad){


    mapTweets <- searchTwitter(searchString =  searchTerm, n = maxTweets, lang = "en", geocode = paste(lat,long,paste0(rad, "mi"),sep=","))

    mapTweets.df <- twListToDF(mapTweets)

    return(mapTweets.df)
  }

  entity13 <- eventReactive(input$mapit,{

    progress <- shiny::Progress$new(session, min=1, max=15)
    on.exit(progress$close())

    progress$set(message = 'Rendering the leaflet map to visualize')

    for (i in 1:15) {
      progress$set(value = i)
      Sys.sleep(0.5)
    }

    print("Calling..")
    entity13 <- mapPlot(input$k, input$n, lang = "en", input$lat, input$long, input$rad)
    entity13
      })


  output$mymap <- renderLeaflet({

    m <- leaflet(entity13()) %>% addTiles() %>%

    addMarkers(entity13()$longitude, entity13()$latitude, popup = entity13()$screenName) 

    m %>% setView(entity13()$longitude, entity13()$latitude, zoom = 4)

    m
})

ui.R (只是一段重要的代码片段)

    column(width = 9,
   box(width = NULL, solidHeader = TRUE,
     leafletOutput("mymap", height = 500)
             ))

1 个答案:

答案 0 :(得分:1)

我认为你的lat / long列中的NA导致了这个问题。在绘图之前尝试删除它们 - 请参阅output$mymap中的我的行。请注意,twitter不会自动为其所有推文添加地理编码,有时它们是空白/ NA。

以下是您可以运行的示例(使用您自己的twitteR授权密钥等)。

请注意,在您的问题中,您应该使其可重现,以便有人可以复制&amp;粘贴您的代码,它将运行,而无需对您的意思进行重大编辑或猜测

library(shiny)
library(shinydashboard)
library(leaflet)
library(twitteR)

ui <- dashboardPage(
  dashboardHeader(),
  dashboardSidebar(),
  dashboardBody(column(width = 9,
                       box(width = NULL, solidHeader = TRUE,
                           leafletOutput("mymap", height = 500)
                       )))
)

server <- shinyServer(function(input, output) {

  # consumerKey <- "...xxx..."
  # consumerSecret <- "...xxx..."
  # accessToken <- "...xxx..."
  # accessSecret <- "...xxx..."

  searchTerm <- "twitter"
  maxTweets <- 50
  lat <- -37.8278185
  long <- 144.9666907
  rad <- 100

  mapPlot <- function(searchTerm, maxTweets, lang, lat, long, rad){

    # setup_twitter_oauth(consumer_key = consumerKey, consumer_secret = consumerSecret,
    #                     access_token = accessToken, access_secret = accessSecret)

    mapTweets <- searchTwitteR(searchString =  searchTerm, 
                               n = maxTweets, 
                               lang = "en", 
                               geocode = paste(lat, long, paste0(rad, "mi"),sep=","))

    mapTweets.df <- twListToDF(mapTweets)

    return(mapTweets.df)
  }

  output$mymap <- renderLeaflet({

    entity13 <- mapPlot(searchTerm, maxTweets, lang = "en", lat, long, rad)
    ## Remove NAs
    entity13 <- entity13[!is.na(entity13$longitude), ]

    leaflet() %>% 
      addTiles() %>%
      addMarkers(data = entity13, lng = entity13$longitude, lat = entity13$latitude, popup = entity13$screenName) %>%
      setView(lng = long, lat = lat, zoom = 4)

  })
})

shinyApp(ui = ui, server = server)

enter image description here

为了说服自己,请考虑:

df <- structure(list(longitude = c("145.21366882", "144.97520704", 
NA, NA, "144.929263"), latitude = c("-37.951828", "-37.7963", 
NA, NA, "-37.78712")), .Names = c("longitude", "latitude"), row.names = c(1L, 
2L, 5L, 6L, 7L), class = "data.frame")

## rows 5 & 6 have NA
df
#      longitude   latitude
# 1 145.21366882 -37.951828
# 2 144.97520704   -37.7963
# 5         <NA>       <NA>
# 6         <NA>       <NA>
# 7   144.929263  -37.78712

lat <- -37.8278185
long <- 144.9666907

## plotting all rows - zoom doesn't work and only first two markers shown
leaflet(data = df) %>% 
  addTiles() %>%
  addMarkers(lng = ~longitude, lat = ~latitude) %>%
  setView(lng = long, lat = lat, zoom = 4)


## only include non-NA rows - works as desired
leaflet(data = df[c(1,2,5),]) %>% 
  addTiles() %>%
  addMarkers(lng = ~longitude, lat = ~latitude) %>%
  setView(lng = long, lat = lat, zoom = 4)