qpisqp
qpisqp

Reputation: 51

How to output a tmap_leaflet in Rshiny

So I'm new to Rshiny and R in general. I was testing out the tmap package and when I typed this into the console

> working_map <- readOGR(dsn=".",layer=file_name, GDAL1_integer64_policy=TRUE)
> japan <- tm_shape(working_map) + tm_fill(col="NumIB", title="# Inbound to Cities", style="jenks")
> tmap_leaflet(japan)

I get an interactive leaflet widget that allows me to see and zoom in and out like a leaflet.

But I can't integrate this into my Rshiny application.

# ui.R


shinyUI(fluidPage(  

  mainPanel(  
    leafletOutput("working_map", height=900)
  )

))

#server.R
output$working_map <- renderLeaflet({
  working_map <- readOGR(dsn=".",layer=filename, GDAL1_integer64_policy=TRUE) 
  japan <- tm_shape(working_map) + tm_fill(col="NumIB", title="# Inbound to Cities", style="jenks")
  tmap_leaflet(japan)
})

I've tried various combinations. Such as plotOutput, or putting the tmap_leaflet inside the ui.R. None of it seems to be working. If I'm not wrong, tmap_leaflet create a Leaflet Widget. Should I be creating this in the ui side then? Or should I utilise a global.R?

Upvotes: 2

Views: 985

Answers (1)

qpisqp
qpisqp

Reputation: 51

I managed to solve it.

#ui.R

shinyUI(fluidPage(
  titlePanel("Japan Map"),

  mainPanel(    
    leafletOutput("working_map", height=900)
  )

))

#server.R
shinyServer(function(input, output) {


  output$working_map <- renderLeaflet({
    working_map <- readOGR(dsn=".",layer="japan_ver81", GDAL1_integer64_policy=TRUE)
    working_map <- tm_shape(working_map) + tm_fill(col="NumIB", title="# Inbound to Cities", style="jenks")
    tmap_leaflet(working_map)

  })


  })

It was quite a silly mistake with the variables. Packages I utilised are library(tmap) and library(rgdal).

Upvotes: 2

Related Questions