代码之家  ›  专栏  ›  技术社区  ›  Slav

R无功值-添加到列表中的唯一值

  •  3
  • Slav  · 技术社区  · 10 年前

    被动值可能会成为来自不同编程语言背景的人的祸根;) 前面的任务(简化为SO)-每当新国家在地图边界内时,我都想下载新文件。在下面的示例中,您需要一个形状文件,例如这个: http://thematicmapping.org/downloads/TM_WORLD_BORDERS_SIMPL-0.3.zip 用户界面尽可能简单:

    library(leaflet)
    library(shiny)
    fluidPage(leafletOutput("mymap"))
    

    服务器并不太复杂(尚;)

    library(shiny)
    library(DT)
    library(leaflet)
    library(rgdal)
    library(maptools)
    library(rgeos)
    
    countries<- readShapeSpatial("TM_WORLD_BORDERS_SIMPL-0.3.shp")
    
    displayed<-c("United Kingdom")
    
    server <- function(input, output,session) {
    
     output$mymap <- renderLeaflet({
        leaflet(countries) %>%
          addTiles() %>%
          setView(lng=-0.1294984,lat=51.4992921,zoom=10)
    
    })
    
    pointsInBounds <- reactive({
        if (is.null(input$mymap_bounds))
          return(NULL)
        bounds <- input$mymap_bounds
        N<- bounds$north
        S<-bounds$south
        E<-bounds$east
        W<-bounds$west
        BB = matrix(c(W,E,E,W,W,N,N,S,S,N), nrow=5,ncol=2)
        BB = SpatialPolygons(list(Polygons(list(Polygon(BB)),1)))
        as.vector(countries[which(gIntersects(countries,BB,byid=TRUE)),]$NAME)
      })}
    

    所以 pointsinbounds() 告诉我哪些国家是可见的, displayed 是已下载内容的列表(以避免重新下载)。 我想实现的是增加(并保持) 边界中的点() 到 显示 列出并观察 显示 如果有变化,请列出(仅当新国家在边界内时才触发下载)。我设法通过以下方式添加到列表中 unique(c(displayed,pointsinbounds())) 但它并不是永久存储的——当我搬出法国时,它会把法国从名单上删除。无论何时,它都会做出反应 边界中的点() 更改,而我只想在总列表更改时作出反应,以避免在您返回同一国家时重新计算。 有什么想法吗?

    1 回复  |  直到 10 年前
        1
  •  4
  •   Eduardo Bergel    10 年前

    根据您的需要,您可以使用两个选项之一。

    1) 将值存储到reactiveValues()中。在这种情况下,国家名单将在该届会议期间保持不变。

    2) 要将该值存储在全局值中,它将在会话之间持久化,并可在会话/用户之间访问(您可以将赋值放在global.R中)。

    编辑:在选项2中,添加了检测新国家的代码。

    注意:在下面的示例中,我添加了一个控件来监视国家列表。

    选项1示例

    library(shiny)
    library(DT)
    library(leaflet)
    library(rgdal)
    library(maptools)
    library(rgeos)
    
    
    ui <- fluidPage(leafletOutput("mymap") ,
                    verbatimTextOutput('myCountries'))
    
    countries <- readShapeSpatial("TM_WORLD_BORDERS_SIMPL-0.3.shp")
    
    displayed <- c("United Kingdom")
    
    server <- function(input, output, session) {
      r <- reactiveValues()
    
    
      observe({
        r$displayed <- unique(c(r$displayed, pointsInBounds())) 
      })
    
      output$mymap <- renderLeaflet({
        leaflet(countries) %>%
          addTiles() %>%
          setView(lng = -0.1294984,
                  lat = 51.4992921,
                  zoom = 10)
      })
    
      output$myCountries <- renderPrint({
        r$displayed 
      })
    
    
      pointsInBounds <- reactive({
        if (is.null(input$mymap_bounds)) {
          return(NULL)
        }
        bounds <- input$mymap_bounds
        N <- bounds$north
        S <- bounds$south
        E <- bounds$east
        W <- bounds$west
        BB = matrix(c(W, E, E, W, W, N, N, S, S, N), nrow = 5, ncol = 2)
        BB = SpatialPolygons(list(Polygons(list(Polygon(
          BB
        )), 1)))
        as.vector(countries[which(gIntersects(countries, BB, byid = TRUE)),]$NAME)
      })
    } 
    
    shinyApp(ui, server)
    

    选项2示例

    library(shiny)
    library(DT)
    library(leaflet)
    library(rgdal)
    library(maptools)
    library(rgeos)
    
    
    ui <- fluidPage(leafletOutput("mymap") ,
                    verbatimTextOutput('myCountries'))
    
    countries <- readShapeSpatial("TM_WORLD_BORDERS_SIMPL-0.3.shp")
    
    if (!exists('displayed')){
      displayed <<- c("United Kingdom")
    }
    
    server <- function(input, output, session) { 
    
      observe({   
        current.coutries <- pointsInBounds()
        new.countries    <- current.coutries[ ! (current.coutries %in% displayed) ]
        # DOWNLOAD new.countries
        displayed <<- unique(c(displayed, pointsInBounds()))   
      })
    
      output$mymap <- renderLeaflet({
        leaflet(countries) %>%
          addTiles() %>%
          setView(lng = -0.1294984,
                  lat = 51.4992921,
                  zoom = 10)
      })
    
      output$myCountries <- renderPrint({
        pointsInBounds()
    
        displayed 
      })
    
      observeEvent(input$mymap_bounds, {})
      pointsInBounds <- reactive({
        if (is.null(input$mymap_bounds)) {
          return(NULL)
        }
        bounds <- input$mymap_bounds
        N <- bounds$north
        S <- bounds$south
        E <- bounds$east
        W <- bounds$west
        BB = matrix(c(W, E, E, W, W, N, N, S, S, N), nrow = 5, ncol = 2)
        BB = SpatialPolygons(list(Polygons(list(Polygon(
          BB
        )), 1)))
        as.vector(countries[which(gIntersects(countries, BB, byid = TRUE)),]$NAME)
      })
    } 
    
    shinyApp(ui, server)