Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ColinFay/geoloc
Add geolocation inside your shiny app
https://github.com/ColinFay/geoloc
Last synced: about 2 months ago
JSON representation
Add geolocation inside your shiny app
- Host: GitHub
- URL: https://github.com/ColinFay/geoloc
- Owner: ColinFay
- License: other
- Created: 2018-08-10T14:24:26.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-11-25T10:50:02.000Z (about 5 years ago)
- Last Synced: 2024-04-16T06:03:00.953Z (9 months ago)
- Language: R
- Size: 10.7 KB
- Stars: 42
- Watchers: 3
- Forks: 8
- Open Issues: 2
-
Metadata Files:
- Readme: README.Rmd
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
- awesome-shiny-extensions - geoloc - Use the Geolocation API to get the location of the user (with user's permission). (Backend / Web APIs Integration)
- jimsghstars - ColinFay/geoloc - Add geolocation inside your shiny app (R)
README
---
output:
github_document:
html_preview: false
---```{r setup, include = FALSE}
knitr::opts_chunk$set(
collapse = TRUE,
comment = "#>",
fig.path = "man/figures/README-",
out.width = "100%"
)
```# geoloc
The goal of geoloc is to provide access to the geolocation API from modern web-browser, in order to get the location of the user in a Shiny app.
> Note that this location will be provided only if the user agrees to share it.
As it relies on the browser HTML API, this won't work with the RStudio native viewer.
This package has been tested on Google Chrome 68.0.3440.106 and Firefox 61.0.2.
## Installation
You can install the dev version of {geoloc} from Github with:
``` r
remotes::install_github("ColinFay/geoloc")
```## How it works
`{geoloc}` implements multiple ways to get a user's location.
This can for example be used to set the application language according to the location of the user.
### Throught `wtfismyip
__Please get your user consent before using this__.
`wtfismyip()` is a simple wrapper to the `wtfismyip.com` service.
```{r eval = FALSE}
library(shiny)
ui <- function(request){
tagList(
verbatimTextOutput("plop")
)
}server <- function(
input,
output,
session
){
output$plop <- renderPrint({
geoloc::wtfismyip()
})
}shinyApp(ui, server)
```### With Native HTML5 API
The two functions (described below) launch an alert box that asks the user if he is willing to share his/her location.
If the user agrees, the `lat` and `long` are sent back to shiny in `input$geoloc_lon` and `input$geoloc_lat`.
The communication from the browser to Shiny may take some time (a matter of seconds, though).
### Error
There are four cases where the location can fail. They are sent back to Shiny as:
+ `"NOT_SUPPORTED"`: location not supported in the browser
+ `"PERMISSION_DENIED"`: the user did not allowed to be located
+ `"POSITION_UNAVAILABLE"`: the user location is not available
+ `"TIMEOUT"`: the operation timed-out## Onload or With a button
There are two ways to get the user location:
### At page load time
You can get the location of the user as soon as the app loads with `onload_geoloc`.
```{r eval = FALSE}
library(shiny)
library(leaflet)ui <- fluidPage(
geoloc::onload_geoloc(),
leafletOutput("lf")
)server <- function(input, output) {
output$lf <- renderLeaflet({
req(input$geoloc_lon)
req(input$geoloc_lat)
leaflet() %>%
addTiles() %>%
setView(as.numeric(input$geoloc_lon), as.numeric(input$geoloc_lat), zoom = 17) %>%
addMarkers(as.numeric(input$geoloc_lon), as.numeric(input$geoloc_lat), label = "You're here!")
})
}shinyApp(ui, server)
```
### With a button
`button_geoloc` behaves like any `shiny::actionButton` (and takes the same parameters), except that it launches the geolocation service when it is clicked. You can then pick up the coordinates with `myButtonId_lat` and `myButtonId_lon`.
```{r eval = FALSE}
library(shiny)
library(leaflet)ui <- fluidPage(
h2("Where Am I?"),
tags$p("Click the button to get your location"),
geoloc::button_geoloc("myBtn", "Get my Location"),
tags$br(),
leafletOutput("lf")
)server <- function(input, output) {
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)
```
## Code of Conduct
Please note that this project is released with a [Contributor Code of Conduct](CODE_OF_CONDUCT.md).
By participating in this project you agree to abide by its terms.