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


  titlePanel("title here"),

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



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() %>%

  output$map <- renderLeaflet(map)


geojson gives me the following:

[1] "Feature Collection"

[1] "name"


 [1] "Feature"



 [1] "Point"

 [1] 103.77749    1.28983


 [1] "Feature"



 [1] "Point"

 [1] 103.67749    1.32983

while geojson_sql has the following structure:

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

1 Answer 1


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:


  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 and acknowledge you have read our privacy policy.

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