R小册子如何单击地图并添加圆圈

R小册子如何单击地图并添加圆圈,r,shiny,leaflet,R,Shiny,Leaflet,嗨,伙计们,我已经试了好几个星期了,但是我没能成功。网上的R传单资源也不够。我真的需要完成这件事 请帮忙,非常感谢 ui.R--> server.R--> 库(闪亮) 图书馆(ggmap) 图书馆(单张) shinyServer(功能(输入、输出、会话){ 输出$map% addTiles(选项=providerTileOptions(noWrap=TRUE)) } 否则{ 地址%setView(lng=p$lng,纬度=p$lat,缩放=16)%>% addTiles(选项=providerT

嗨,伙计们,我已经试了好几个星期了,但是我没能成功。网上的R传单资源也不够。我真的需要完成这件事

请帮忙,非常感谢

ui.R-->

server.R-->

库(闪亮)
图书馆(ggmap)
图书馆(单张)
shinyServer(功能(输入、输出、会话){
输出$map%
addTiles(选项=providerTileOptions(noWrap=TRUE))
}
否则{
地址%setView(lng=p$lng,纬度=p$lat,缩放=16)%>%
addTiles(选项=providerTileOptions(noWrap=TRUE))%>%
添加圆(p$lng,p$lat,重量=1,半径=100,颜色=“黑色”,
fillColor=“橙色”,弹出窗口=地址,fillOpacity=0.5,不透明度=1)
}
})

输出$outTL;DR

  • 创建初始映射,使其不依赖于用户输入
  • 使用响应用户单击的观察者更新地图
  • 使用
    proxy
    更新地图,而无需重新渲染所有内容
  • 我会制作您的原始地图,并在用户单击位置时使用
    proxy
    功能更新地图。Rstudio网站上有一个教程,在那里他们展示了如何做到这一点。它有望节省一些计算,因为不会在每次添加圆时重新渲染贴图

    我还添加了一些我会考虑的其他事项:将圆数据放入反应式数据集中,并在组中维护圆,从而允许您使用附加的观察者/按钮轻松隐藏/显示它们

    这里是一个工作示例。为了记录在案,我使用的是github的传单版本(由于这个软件包正在积极开发中,所以我建议这样做)。您可以通过
    devtools::install\u github('rstudio/传单')
    获得它。至少有两个我认为CRAN上还没有的新特性——比如可以轻松创建自定义标记

    library(shiny)
    library(ggmap)
    library(leaflet)
    
    ui <- shinyUI(bootstrapPage(
      leafletOutput("map")
    ))
    
    server <- shinyServer(function(input, output, session) {
      ## One alternative: store circles data?
      ## I dont actually implement this, but you would do this in the observer as well
      dat <- reactiveValues(circs = data.frame(lng=numeric(), lat=numeric()))
    
      ## Make your initial map
      output$map <- renderLeaflet({
        leaflet() %>%
          setView(lng = -43.1729, lat = -22.9068, zoom = 11) %>%
            addTiles(options = providerTileOptions(noWrap = TRUE)) 
      })
    
      ## Observe mouse clicks and add circles
      observeEvent(input$map_click, {
        ## Get the click info like had been doing
        click <- input$map_click
        clat <- click$lat
        clng <- click$lng
        address <- revgeocode(c(clng,clat))
    
        ## Add the circle to the map proxy
        ## so you dont need to re-render the whole thing
        ## I also give the circles a group, "circles", so you can
        ## then do something like hide all the circles with hideGroup('circles')
        leafletProxy('map') %>% # use the proxy to save computation
          addCircles(lng=clng, lat=clat, group='circles',
                     weight=1, radius=100, color='black', fillColor='orange',
                     popup=address, fillOpacity=0.5, opacity=1)
      })
    
    })
    
    shinyApp(ui=ui, server=server)
    
    库(闪亮)
    图书馆(ggmap)
    图书馆(单张)
    
    请创建一个用户界面。展示你尝试过的代码,并描述它是如何不起作用的。嗨,Flick先生,我已经发布了代码。这是有效的,但我想知道有没有更好的方法来改进这个投票,现在你已经展示了你的尝试,但正如Flick先生所建议的,也描述一下你正在尝试做什么,以及你的困境。嗨,伙计,你的例子很有魅力。这正是我想做的。愿上帝保佑你,祝你未来一切顺利
    library(shiny)
    library(ggmap)
    library(leaflet)
    
    
    shinyServer(function(input, output, session) {
    
    
    output$map <- renderLeaflet({
    
     p <- input$map_click
    
     if(is.null(p)){
       leaflet() %>% setView(lng = -43.1729, lat = -22.9068, zoom = 11) %>%
         addTiles(options = providerTileOptions(noWrap = TRUE)) 
     }
    
     else{
       address <- revgeocode(c(p$lng,p$lat))
    
       leaflet() %>% setView(lng = p$lng, lat = p$lat, zoom = 16) %>%
       addTiles(options = providerTileOptions(noWrap = TRUE)) %>%
       addCircles(p$lng, p$lat, weight = 1, radius = 100, color =  "black",
                        fillColor = "orange", popup = address, fillOpacity=0.5, opacity=1)
     }
    
    
    })
    
    output$out <- renderPrint({
    validate(need(input$map_click, FALSE))
    click <- input$map_click
    clat <- click$lat
    clng <- click$lng
    address <- revgeocode(c(clng,clat))
    print(clat)
    print(clng)
    print(address)
    
    })
    
    })
    
    library(shiny)
    library(ggmap)
    library(leaflet)
    
    ui <- shinyUI(bootstrapPage(
      leafletOutput("map")
    ))
    
    server <- shinyServer(function(input, output, session) {
      ## One alternative: store circles data?
      ## I dont actually implement this, but you would do this in the observer as well
      dat <- reactiveValues(circs = data.frame(lng=numeric(), lat=numeric()))
    
      ## Make your initial map
      output$map <- renderLeaflet({
        leaflet() %>%
          setView(lng = -43.1729, lat = -22.9068, zoom = 11) %>%
            addTiles(options = providerTileOptions(noWrap = TRUE)) 
      })
    
      ## Observe mouse clicks and add circles
      observeEvent(input$map_click, {
        ## Get the click info like had been doing
        click <- input$map_click
        clat <- click$lat
        clng <- click$lng
        address <- revgeocode(c(clng,clat))
    
        ## Add the circle to the map proxy
        ## so you dont need to re-render the whole thing
        ## I also give the circles a group, "circles", so you can
        ## then do something like hide all the circles with hideGroup('circles')
        leafletProxy('map') %>% # use the proxy to save computation
          addCircles(lng=clng, lat=clat, group='circles',
                     weight=1, radius=100, color='black', fillColor='orange',
                     popup=address, fillOpacity=0.5, opacity=1)
      })
    
    })
    
    shinyApp(ui=ui, server=server)