R语言 在shiny应用程序中拖动标记时在表中显示坐标

u3r8eeie  于 2023-06-03  发布在  其他
关注(0)|答案(1)|浏览(203)

在我的简单闪亮应用程序中,我可以拖动Map上的标记,并在observe函数中查看它们的新坐标:

coords_data <- data.frame(id = 1:3, lng = c(2, 30, 50), lat = c(48, 40, 60)) 

library(shiny)
library(leaflet)
library(tidyverse)

ui = fluidPage(
    leafletOutput('map'),
    textOutput('dragEndLocation')
)

server = function(input, output, session){
  
    output$map <-renderLeaflet({ 
      leaflet()%>%
        addTiles()%>%
        addMarkers(data = coords_data, 
                   options = markerOptions(draggable = TRUE))
      
    })
    
    
      observe({
        print(input$map_marker_dragend)
      })
    

                   
}

shinyApp(ui, server)

然而,我想打印的最终坐标在一个表,我不知道如何!我试过this example,但没有成功。
此外,我想用圆圈替换标记:

coords_data <- data.frame(id = 1:3, lng = c(2, 30, 50), lat = c(48, 40, 60)) 

library(shiny)
library(leaflet)
library(tidyverse)

ui = fluidPage(
    leafletOutput('map'),
    textOutput('dragEndLocation')
)

server = function(input, output, session){
  
    output$map <-renderLeaflet({ 
      leaflet()%>%
        addTiles()%>%
        addCircles(data = coords_data, 
                   options = markerOptions(draggable = TRUE),
                   radius = 100,color="red")%>%
        addDrawToolbar(circleOptions=NA, markerOptions=NA,
                       polygonOptions=NA, rectangleOptions=NA,
                       polylineOptions=NA)
      
    })
    
    
      observe({
        print(input$map_marker_dragend)
      })
    

                   
}

shinyApp(ui, server)

在这种情况下,我仍然可以移动圆,但我不知道如何获得它们的新坐标。任何帮助或建议?

q43xntqr

q43xntqr1#

library(shiny)
library(leaflet)
library(tidyverse)

coords_data <- data.frame(id = 1:3, lng = c(2, 30, 50), lat = c(48, 40, 60))

ui = fluidPage(
  leafletOutput('map'),
  tableOutput('coordsTable')
)

server = function(input, output, session){
  
  coords_rv <- reactiveValues(data = coords_data)
  
  output$map <-renderLeaflet({ 
    leaflet()%>%
      addTiles()%>%
      addMarkers(data = coords_data,
                       lng = ~lng, lat = ~lat,
                       layerId = ~id, 
                       options = markerOptions(draggable = TRUE))
  })
  
  
  observeEvent(input$map_marker_dragend, {
    
    markerId <- input$map_marker_dragend$id
    lat <- input$map_marker_dragend$lat
    lng <- input$map_marker_dragend$lng
    
    
    coords_rv$data[coords_rv$data$id == markerId, c("lat", "lng")] <- c(lat, lng)
    
    
    leafletProxy('map') %>% clearMarkers() %>% clearGroup("rad")%>%
      addMarkers(data = coords_rv$data,
                       lng = ~lng, lat = ~lat,
                       layerId = ~id,
                       options = markerOptions(draggable = TRUE))%>%
      addCircles(data = coords_rv$data,
                 lng = ~lng, lat = ~lat,
                 layerId = ~id,radius = 10000,color="red",group = "rad")
  })
  
  
  output$coordsTable <- renderTable({
    coords_rv$data
  })
  
}

shinyApp(ui, server)

相关问题