首页 > 解决方案 > R/Shiny,响应式值:如何正确创建异构数据元素的响应式列表?

问题描述

我想将一组坐标和辅助信息存储在列表之类的东西中。每当编译列表时,我都想将其添加到反应元素中,以便可以更新相应的依赖项。作为一种数据结构,我想到了这样的事情:

convexHull = list (lat = NULL, lon = NULL, label = NULL, colour = NULL, group = NULL, layerID = NULL)
env_map_polygon = reactiveValues (list (convexHull))

现在的想法是做这样的事情:

dummycv <- convexHull
dummycv$lat <- c (1, 2, 3, 4, 5, 6)
dummycv$lon <- c (6, 5, 4, 3, 2, 1)
dummycv$label <- "Area 1"
dummycv$colour <- "red"
dummycv$group <- 1
dummycv$layerID <- 1

最后,我想通过 append 将 dummycv 附加到 env_map_polygon 的反应列表中,通过访问它来强制更新相应的输出:

for (i in 1:length (env_map_polygon) { dosomething with env_map_polygon[[i]]$lat ...}

纬度/经度坐标列表是可变的,长度可能会随着现在的每个多边形/凸包而变化。

目前我缺乏以正确/正确的方式实施它的想法......

为了更清楚一点,我刚刚编译了一个示例。希望有帮助:

library(shiny)

ui <- fluidPage(
    
    actionButton ("ui_env_map_marker_add",
                  "Add markers",
                  icon = icon ("map-marker-alt")
                 ),
                                                   
    
                                                   
    actionButton ("ui_env_map_marker_remove",
                  "Remove all",
                  icon = icon ("trash-alt")
                 ),
    
    
    tags$hr(),
    
    actionButton ("ui_env_map_convexHull_add",
                  "Add convex hull",
                  icon = icon ("vector-square")
                 ),
                                                   
    
                                                   
    actionButton ("ui_env_map_convexHull_remove",
                  "Remove all",
                  icon = icon ("trash-alt")
                 ),
    
    tags$hr(),
    
    plotOutput ("env_map")
    
)

server <- function(input, output, session) {
    
    
    
############################################################ Here the working section for markers
############################################################ Here the working section for markers
############################################################ Here the working section for markers
############################################################ Here the working section for markers
############################################################ Here the working section for markers
  env_map_markers = reactiveValues (lat = NULL, lon = NULL, label = NULL, colour = NULL, group = NULL, layerID = NULL)

  
  # observe, whether the remove marker button was pressed:
  # in this case we delete the entire reactive marker list, 
  # finally triggering the map to redraw
  observeEvent (input$ui_env_map_marker_remove, {
    env_map_markers$lon     = NULL
    env_map_markers$lat     = NULL
    env_map_markers$colour  = NULL
    env_map_markers$label   = NULL
    env_map_markers$group   = NULL
    env_map_markers$layerID = NULL
  })
  
  
  
   observeEvent (input$ui_env_map_marker_add, {
    
    # this section populates the marker with fake data
    temp_markers <- list (lat     = rnorm (20), #coord2decimalCoord (c (rnorm (20)), TRUE,  input$ui_env_map_latitudeFormat, 0), 
                          lon     = rnorm (20), #coord2decimalCoord (c (rnorm (20)), FALSE, input$ui_env_map_longitudeFormat, 0), 
                          group   = rep (c ("Marker1", "Marker2"), 10),
                          layerID = input$ui_env_map_marker_layerID,
                          colour  = rep (c ("red", "green"), 10), 
                          label  = 1:20)
    #output$ui_env_map_zoomlevel <- renderText (paste0 ("Point added to list"))
    
    # attach the new markers to the internal list
    env_map_markers$lon     <- c (env_map_markers$lon,     temp_markers$lon)
    env_map_markers$lat     <- c (env_map_markers$lat,     temp_markers$lat)
    env_map_markers$colour  <- c (env_map_markers$colour,  temp_markers$colour)
    env_map_markers$label   <- c (env_map_markers$label,   temp_markers$label)
    env_map_markers$group   <- c (env_map_markers$group,   temp_markers$group)
    env_map_markers$layerID <- c (env_map_markers$layerID, temp_markers$label)
    
  })
  
############################################################ End of working section for markers
############################################################ End of working section for markers
############################################################ End of working section for markers
############################################################ End of working section for markers
############################################################ End of working section for markers

   
   
  output$env_map <- renderPlot ({
      plot ( c(-10, 10), c(-10, 10),type = "n")
      if (!is.null (env_map_markers[["lon"]])){
          points (env_map_markers$lon, env_map_markers$lat, col = env_map_markers$colour)
      }
      
      # and here something like the latter one, but for one or more polygons in env_map_polygon
      
      
       
  })
   
   
############################################################ Here the NON working section forconvex hulls
  
 # a convex hull is a structure that includes several coordinates lat/lon to display an area
 # label, colour, group or layerID are additional information, required for later plotting on a map 
  convexHull = list (
    lat = NULL,
    lon = NULL,
    label = NULL,
    colour = NULL,
    group = NULL,
    layerID = NULL
  )
  
  # initially set to NULL?
  env_map_polygon <- do.call(shiny::reactiveValues, convexHull)
  
  
  # if button is clicked, then attach
  observeEvent (input$ui_env_map_convexHull_add, {
      temp_convexHull <- convexHull
      
      temp_convexHull$lat     <- 1:20
      temp_convexHull$lon     <- (1:20)^2
      temp_convexHull$label   <- "Area 1"
      temp_convexHull$colour  <- "red"
      temp_convexHull$group   <- "cv"
      temp_convexHull$layerID <- 1

      append (env_map_polygon, env_map_polygon)
  })
  
  
  
}

shinyApp(ui, server)

标签: rlistshinyappendreactive

解决方案


推荐阅读