在Shiny

时间:2015-04-30 19:41:45

标签: r leaflet shiny polygons shiny-server

我正在努力在一个Shiny应用程序中绘制多个多边形 - 基于传单包。

这是我希望的非应用输出:

data <- list(
    beam1 = data.frame(lat = c(-115,-125, -125, -115),
               lon = c(32, 32, 45,45)),
    beam2 =     data.frame(lat = c(-100, -111, -111, -100),
                           lon = c(42, 42, 50,50))
)
dataTemp <- do.call(rbind, lapply(data, function(x) rbind(x, NA)))

library(leaflet)
m = leaflet() %>% addTiles()
m %>%
    addPolygons(
        dataTemp[,"lat"],
        dataTemp[,"lon"],
        color = c('red', 'green'), weight = 3
    )

然而,从中创建一个Shiny应用程序被证明是困难的 - 多边形被绘制然后在绘制新多边形后逐个消失。这是我正在使用的代码。你需要点击&#39; Draw&#39;绘制多边形 请注意: 1 - 绘制绿色时红色多边形消失(3秒后) 2 - 多边形在这里是三角形,而在上面的代码中它们是矩形。 3 - 如果你看下面server.R中的代码,我实际上不得不翻转&#39; lat&#39;和&#39; lon&#39;列甚至可以远程校正图片。

ui.R:

library(shiny)
library(leaflet)

shinyUI(navbarPage("Beams", id="nav",

    tabPanel("Interactive map",
            div(class="outer",

                leafletMap("map", "100%", 650,
                           options = list(center = c(37.45, -93.85), zoom = 4)),
                actionButton("drawPoints", "Draw")
            )
    )
))

server.R:

library(shiny)
library(leaflet)

data <- list(
    beam1 = data.frame(lat = c(-115,-125, -125, -115),
                       lon = c(32, 32, 45,45)),
    beam2 =     data.frame(lat = c(-100, -111, -111, -100),
                           lon = c(42, 42, 50,50))
)

# server activity
shinyServer(function(input, output, session) {

    map <- createLeafletMap(session, "map")

    observe({

            if(input$drawPoints == 0) {
                return(NULL)
            } else {

                map$clearShapes()

                for (i in seq_along(data)) {
                    map$addPolygon(
                        data[[i]][,"lon"],  # - notice, i had to change lat and lon
                        data[[i]][,"lat"],
                        layerId=c("1"),
                        list( fillOpacity=0.4),
                        list(color = c('red','green')[i])

                    )
                    Sys.sleep(3)  # - this is to see first (red) polygon
                }
            }
        })


})

任何想法都将不胜感激!

1 个答案:

答案 0 :(得分:0)

我已更新您的代码以使用传单代理,这是更新现有地图的一种更简洁的方法,并且是在leaflet包的持续更新中执行此操作的当前维护方法(据我所知) )。我在data.frame中切换了“lat”和“lon”以使它们正确。我还在坐标的末尾再次添加了原点以完成形状 - 这就是为什么你有三角形而不是矩形的原因。最后,我将多边形的layerId更改为i以使它们不同 - 这就是它们相互擦除的原因。

我假设您希望对形状进行硬编码,但有多种方法可以向用户打开多边形图选择(甚至绘图)。有趣的说明:在解决此问题时,了解deferUntilFlush中的leafletProxy参数 - 如果设置为FALSE,则只有在Sys.sleep计数完成后才会绘制两个形状。

ui.R

library(shiny)
library(leaflet)

shinyUI(navbarPage("Beams", id="nav",

                   tabPanel("Interactive map",
                            div(class="outer",
                                leafletOutput("map", "100%", 650),
                                actionButton("drawPoints", "Draw")
                            )
                   )
))

server.R

library(shiny)
library(leaflet)

data <- list(
  beam1 = data.frame(lon = c(-115,-125, -125, -115, -115),
                     lat = c(32, 32, 45, 45, 32)),
  beam2 =     data.frame(lon = c(-100, -111, -111, -100, -100),
                         lat = c(42, 42, 50, 50, 42))
)

shinyServer(function(input, output, session) {

  map <- leaflet() %>% addTiles() %>% setView(-93.85, 37.45, zoom = 4)
  output$map <- renderLeaflet(map)
  proxy <- leafletProxy("map", deferUntilFlush = FALSE)

  observeEvent(input$drawPoints, {
    proxy %>% clearShapes()
    for (i in seq_along(data)) {
      proxy %>% addPolygons(
        data[[i]][,"lon"],
        data[[i]][,"lat"],
        layerId=i,
        opacity=0.4,
        color = c('red','green')[i]
      )
      Sys.sleep(2)  # - this is to see first (red) polygon
    }
  })
})