闪亮的地理定位应用程序中的纬度和经度矢量

Vectors of latitude and longitude in geolocation app in shiny

我正在制作 uilding 和应用程序,其中包括使用 geoloc 程序包

进行地理定位捕获

这是一个示例应用程序:

library(shiny)
library(leaflet)
library(geoloc)

ui <- fluidPage(
  h2("Where Am I?"),
  tags$p("Click the button to get your location"),
  geoloc::button_geoloc("myBtn", "Get my Location"),
  tags$br(),
  textOutput("coords"),
  textOutput("col"),
  leafletOutput("lf")
)

server <- function(input, output) {
  output$coords <- renderText(paste(input$myBtn_lat, input$myBtn_lon, sep = ", "))
  
  Lats <- reactiveValues(Lat = NULL)
  
  observeEvent(input$myBtn_lat, {
    Lats$Lat <- append(Lats$Lat, input$myBtn_lat)
  })
  
  
  output$col <- renderText({
    Lats$Lat
  })
  
  output$lf <- renderLeaflet({
    req(input$myBtn_lon)
    req(input$myBtn_lat)
    leaflet() %>%
      addTiles() %>%
      setView(as.numeric(input$myBtn_lon), as.numeric(input$myBtn_lat), zoom = 17) %>%
      addMarkers(as.numeric(input$myBtn_lon), as.numeric(input$myBtn_lat), label = "You're here!")
  })
}

shinyApp(ui, server)

对此我有两个问题:

如何使用按钮获取经纬度向量

我需要这个,因为通常我们喜欢取 4 或 5 倍的位置,然后使用中位数。

这已在 问题中得到解决,但是,由于该按钮是自定义按钮,并且输入不是 input$myBtn,而是 input$[,所以有些问题我无法弄清楚=31=] 和输入$myBtn_lon,我觉得很难计算。这就是我要对观察事件做的事情

如何将其转换成闪亮的模块

这将转到一个更大的闪亮应用程序,所以我很乐意为此生成模块,但同样,ui 中的输入是“myBtn”,但在服务器中我有2 个输入(MyBtn_lon 和 MyBtn_lat),很难弄清楚

欢迎任何帮助

您应该点击“myBtn”,而不是“myBtn_lat”。所以尝试将 observeEvent(input$myBtn_lat 更改为 observeEvent(input$myBtn。 另外,取4、5倍位置的目的是什么?每次点击按钮时坐标不会改变或改变很小。

以下带有 Shiny 模块的代码怎么样?我测试了并且有效。

library(shiny)
library(leaflet)
library(geoloc)

mapUI <- function(id, label = "Location in map"){
  ns <- NS(id)
  
  tagList(
    geoloc::button_geoloc(ns("myBtn"), "Get my Location"),
    tags$br(),
    textOutput(ns("coords")),
    textOutput(ns("col")),
    textOutput(ns("md")), # for median latitude
    leafletOutput(ns("lf"))
  )
}

mapServer <- function(id){
  moduleServer(
    id,
    function(input, output, session){
      output$coords <- renderText(paste(input$myBtn_lat, input$myBtn_lon, sep = ", "))
      
      Lats <- reactiveValues(Lat = NULL)
      
      observeEvent(input$myBtn, {
        Lats$Lat <- c(Lats$Lat, input$myBtn_lat)
      })
      
      
      output$col <- renderText({
        Lats$Lat 
      })
      
      # add median latitude
      output$md <- renderText({
        req(input$myBtn_lat)
        if(length(Lats$Lat) %% 5 == 0){
          paste0("Median latitute is: ", median(Lats$Lat))
        } 
      })
      
      output$lf <- renderLeaflet({
        req(input$myBtn_lon)
        req(input$myBtn_lat)
        leaflet() %>%
          addTiles() %>%
          setView(as.numeric(input$myBtn_lon), as.numeric(input$myBtn_lat), zoom = 17) %>%
          addMarkers(as.numeric(input$myBtn_lon), as.numeric(input$myBtn_lat), label = "You're here!")
      })
    }
  )
}

ui <- fluidPage(
  h2("Where Am I?"),
  tags$p("Click the button to get your location"),
  mapUI("map1")
)

server <- function(input, output, session) {
  mapServer("map1")
  
}

shinyApp(ui, server)