Estoy construyendo una aplicación que incluye capturas de geolocalización usando el paquete geoloc
Esta es una aplicación de ejemplo:
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)
Tengo dos preguntas para esto:
Necesito esto porque, por lo general, nos gusta tomar 4 o 5 veces la ubicación y luego usar la mediana.
Esto se ha abordado en esta pregunta, sin embargo, hay algunos problemas que no puedo resolver ya que el botón es personalizado, y las entradas no son input$myBtn, sino input$myBtn_lat y input$myBtn_lon, lo encuentro difícil computar. Esto es lo que estoy tratando de hacer con los eventos de observación.
Esto irá a una aplicación brillante más grande, por lo que me encantaría generar módulos para esto, pero nuevamente, el hecho de que la entrada en ui es "myBtn", pero luego en el servidor tengo 2 entradas (MyBtn_lon y MyBtn_lat), hacer es muy difícil de entender
Cualquier ayuda es bienvenida
Debe hacer clic en "myBtn", no en "myBtn_lat". Así que intente cambiar observeEvent(input$myBtn_lat
a observeEvent(input$myBtn
. Además, ¿cuál es el propósito de tomar 4 o 5 veces la ubicación? Las coordenadas no cambian o cambian muy poco cada vez que hace clic en el botón.
¿Qué tal el siguiente código con módulos Shiny? Probé y funcionó.
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)