1

I am creating a Rshiny interface on that can take in the user input when a user click on a point on the map, query the user's selection from postgresql and return another map layer. The package I am using is leaflet. (https://rstudio.github.io/leaflet/). I have followed the examples, but am unable to generate any map layers.

The following are my ui.r and server.r codes

ui.r

shinyUI(fluidPage(
  titlePanel("title here"),

  sidebarLayout(position = "right", 
     sidebarPanel(
       selectInput("time", 
                   label = "Choose a time period",
                   choices = list("1", "2", "3")
     )
   ),

  mainPanel(leafletOutput('map'))
))

server.r

shinyServer(function(input,output) {


  drv <- dbDriver("PostgreSQL")
  con <- dbConnect(drv, dbname="postgres",host="localhost",user=***, password=***, port=5432)
  geojson_fromsql <- dbGetQuery(con, "SELECT ST_ASGeoJson(geom) FROM table WHERE time='input';)

  geojson <- fromJSON("file path/file name.geojson")

  map <- leaflet() %>%  
    addTiles() %>%
    addGeoJson(geojson)

  output$map <- renderLeaflet(map)

}

geojson gives me the following:

$type
[1] "Feature Collection"

$crs 
$crs$type 
[1] "name"

$crs$properties 
                         name
"urn:ogc:def:crs:OGC:1.3:CR84"

 $features
 $features[[1]]
 $features[[1]]$type
 [1] "Feature"

 $features[[1]]$properties 

 year 
 "2012"

 $features[[1]]$geometry
 $features[[1]]$geometry$type 
 [1] "Point"

 $features[[1]]$geometry$coordinates
 [1] 103.77749    1.28983

 ...

 $features
 $features[[17]]
 $features[[17]]$type
 [1] "Feature"

 $features[[17]]$properties 

 year 
 "2012"

 $features[[17]]$geometry
 $features[[17]]$geometry$type 
 [1] "Point"

 $features[[17]]$geometry$coordinates
 [1] 103.67749    1.32983

while geojson_sql has the following structure:

(data.frame) 
                                        st_asgeojson
1{"type":"Point","coordinates":[103.77748, 1.28983]}
...
17{"type":"Point","coordinates":[103.67749, 1.32983]}

addGeoJson(geojson) gives me nothing in the main panel (fig 1). *addGeoJson(geojson_sql) gives a leaflet widget, with no tilelayers (fig 2). *addGeoJson(geojson_sql[1,]) gives a leaflet widget, with no tilelayers as well (fig 2). I believe I have some syntax errors in setting the tilelayer as well.

But more importantly, I would like to know what format/structure does the leaflet package accept for geojson, and how I can derive it in R?

PS: Suggestions for alternative packages or methods are welcome too. I have tried rCharts and have hit a wall here as well: https://gis.stackexchange.com/questions/151536/how-to-convert-a-geojson-from-postgresql-to-geojson-format-readable-in-rcharts-i And I am not certain that rCharts takes in the user's input when users click on points on the map.

fig 1 - reading geojson directly into R fig 1 - reading geojson directly into R

fig 2 - reading geojson from postgresql fig 2 - reading geojson from postgresql

2

I don't quite understand what you want to accomplish from the user clicking on the map. But in regards to rendering the map layers, if you want to use addGeoJSON() I think it is easiest to read in the json file as follows:

shinyServer(function(input,output){

  geojson <- reactive({
    readLines("filename.json") %>% paste(collapse = "\n")
    })

  output$map <- renderLeaflet({
    leaflet() %>% addTiles() %>% addGeoJson(geojson())
    })
})

You can change the code inside reactive() to include your user input such as input$time in order to change the geojson that is read in. Note the parenthesis after geojson in the last line.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.