为什么闪亮的应用程序在尝试在reactive()中使用osrm trip时给我一个错误?

问题描述 投票:0回答:1

我正在尝试制作一个应用程序,考虑到当前位置,生成一条到数据帧最近位置的路线,但是在闪亮的情况下进行计算时,它给了我以下错误:

Warning: Error in validateCoords: addPolylines requires non-NULL longitude/latitudes
并且我不知道如何修复它,我已经尝试更改代码的各个部分,但它似乎不起作用。

代码如下:

library(shiny)
library(osrm)
library(leaflet)
library(dplyr)

script <- '
      $(document).ready(function () {
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
              
        function onError (err) {
          Shiny.onInputChange("geolocation", false);
        }
              
        function onSuccess (position) {
          setTimeout(function () {
            var coords = position.coords;
            console.log(coords.latitude + ", " + coords.longitude);
            Shiny.onInputChange("geolocation", true);
            Shiny.onInputChange("lat", coords.latitude);
            Shiny.onInputChange("long", coords.longitude);
          }, 1100)
        }
      });
              '


data2 <- tibble(
  
  lat = c(19.44211,19.24312),
  lon = c(-99.23122,-99.24231)
  
)

shinyApp(
  ui = fluidPage(
    shinybusy::add_busy_bar(),
    titlePanel("Using Geolocation"),
    
    actionButton("ejemplo", "xd"),
    actionButton("ruta", "generar ruta"),
    
    uiOutput("xd"),
    fluidRow(column(width = 12,
                    leafletOutput("map", height = 600))
    )
  ),
  server = function(input, output, session) {
    
    output$map <- renderLeaflet({
      
      leaflet() |> 
        #setView(-93.65, 42.0285, zoom = 17) |> 
        addProviderTiles(provider = providers$CartoDB.DarkMatter)
      
    })
    
    #observeEvent(input$ejemplo,{
    
    data2 <- reactive({
      
      data.frame(
        lat = input$lat,
        lng = input$long
      )
    })
    
    #})
    
    
    
    #observeEvent(input$ruta,{
      trip <-
      reactive({
        
        if (nrow(data2() > 0)){
          
          ejemplo <- 
            data |> 
            mutate(
              actual_lon = data2()$lng,
              actual_lat = data2()$lat,
              diff_lon = abs(lon-actual_lon),
              diff_lat = abs(lat-actual_lat),
              total_diff = diff_lon+diff_lat
            ) 
          
          ejemplo_val <- 
            ejemplo |> 
            filter(total_diff == min(ejemplo$total_diff, na.rm = TRUE))
          route<- 
            tibble(
              com =c("Origen","Destino"),
              lon =c(ejemplo_val$actual_lon,ejemplo_val$lon),
              lat =c(ejemplo_val$actual_lat,ejemplo_val$lat)
              
              
            )
          
          trip <- osrmTrip(route, returnclass="sf", overview = "full", osrm.profile = "foot")[[1]]$trip
          
          
        }
        
        
      })
      
      
    #})
    
    
    
    
    observeEvent(input$ruta,{
      
      req(data2())
      req(trip())
      
      if (nrow(data2()) >0 ){
        
        leafletProxy("map", session) |> 
          clearMarkers() |> 
          clearShapes() |> 
          addCircleMarkers(
            lat = data2()$lat,
            lng = data2()$lng,
            color = "red"
          ) |> 
          setView(data2()$lng, data2()$lat, zoom = 17) |> 
          addPolylines(data =trip(),
                       lat = trip()$lat,
                       lng = trip()$lon
                       )
      }
      
      
    })
    
    observeEvent(input$ejemplo,{
      
      output$xd <- renderUI(tags$script(script))
      
    })
    
  }
)

有人知道错误可能出在哪里吗?

javascript r shiny r-leaflet osrm
1个回答
0
投票

尝试使用包含您需要的所有对象的observeEvent

library(shiny)
library(osrm)
library(leaflet)
library(dplyr)

script <- '
      $(document).ready(function () {
        navigator.geolocation.getCurrentPosition(onSuccess, onError);
              
        function onError (err) {
          Shiny.onInputChange("geolocation", false);
        }
              
        function onSuccess (position) {
          setTimeout(function () {
            var coords = position.coords;
            console.log(coords.latitude + ", " + coords.longitude);
            Shiny.onInputChange("geolocation", true);
            Shiny.onInputChange("lat", coords.latitude);
            Shiny.onInputChange("long", coords.longitude);
          }, 1100)
        }
      });
              '


data2 <- tibble(
  
  lat = c(19.44211,19.24312),
  lon = c(-99.23122,-99.24231)
  
)

shinyApp(
  ui = fluidPage(
    shinybusy::add_busy_bar(),
    titlePanel("Using Geolocation"),
    
    actionButton("ejemplo", "xd"),
    actionButton("ruta", "generar ruta"),
    
    uiOutput("xd"),
    fluidRow(column(width = 12,
                    leafletOutput("map", height = 600))
    )
  ),
  server = function(input, output, session) {
    
    output$map <- renderLeaflet({
      
      leaflet() |> 
        #setView(-93.65, 42.0285, zoom = 17) |> 
        addProviderTiles(provider = providers$CartoDB.DarkMatter)
      
    })
    
    #observeEvent(input$ejemplo,{
    
    observeEvent(input$ruta,{
    
    data2 <- reactive({
      
      data.frame(
        lat = input$lat,
        lng = input$long
      )
    })
    
    #})
    
    
    
    #observeEvent(input$ruta,{
    trip <-
      reactive({
        
        if (nrow(data2() > 0)){
          
          ejemplo <- 
            data |> 
            mutate(
              actual_lon = data2()$lng,
              actual_lat = data2()$lat,
              diff_lon = abs(lon-actual_lon),
              diff_lat = abs(lat-actual_lat),
              total_diff = diff_lon+diff_lat
            ) 
          
          ejemplo_val <- 
            ejemplo |> 
            filter(total_diff == min(ejemplo$total_diff, na.rm = TRUE))
          route<- 
            tibble(
              com =c("Origen","Destino"),
              lon =c(ejemplo_val$actual_lon,ejemplo_val$lon),
              lat =c(ejemplo_val$actual_lat,ejemplo_val$lat)
              
              
            )
          
          trip <- osrmTrip(route, returnclass="sf", overview = "full", osrm.profile = "foot")[[1]]$trip
          
          
        }
        
        
      })
    
    
    #})
    
    
    
    
    
      
      req(data2())
      req(trip())
      
      if (nrow(data2()) >0 ){
        
        leafletProxy("map", session) |> 
          clearMarkers() |> 
          clearShapes() |> 
          addCircleMarkers(
            lat = data2()$lat,
            lng = data2()$lng,
            color = "red"
          ) |> 
          setView(data2()$lng, data2()$lat, zoom = 17) |> 
          addPolylines(data =trip(),
                       lat = trip()$lat,
                       lng = trip()$lon
          )
      }
      
      
    })
    
    observeEvent(input$ejemplo,{
      
      output$xd <- renderUI(tags$script(script))
      
    })
    
  }
)
© www.soinside.com 2019 - 2024. All rights reserved.