Integrating tmap into Shiny?

shiny
leaflet
rstudio

#1

I am trying to create a shiny app which creates side by side choropleth maps for two different variables. Right now, my code looks like this:

library(shiny)
library(tmap)
library(leaflet)
library(RColorBrewer)
library(raster)
library(rgdal)

#get data
source("Practice/Map_Maker.R")

#create a mapping function
getChor=function(variable,color,n_value,title){
  tm_shape(Spatial_Election_Data) + 
    tm_fill(
      variable,palette=color,style="jenks",n=n_value,
      title=title,legend.hist=TRUE
    ) + tm_layout(title="USA",legend.outside=TRUE,frame=FALSE)
}

#define UI
ui=fluidPage(
  titlePanel("Election Analysis"),
  sidebarLayout(
    sidebarPanel(
      helpText(
        "Create maps with demographic and election data."
      ),
      selectInput("election_var",
                  label="Choose an election to map",
                  choices=c("Dem Vote '12 (Obama)","Dem Vote '16 (Clinton)","GOP Vote '12 (Romney)","GOP Vote '16 (Trump)"),
                  selected="Dem Vote '16 (Obama)"
      ),
      sliderInput("slider",
                  label="Choose number of partitions",
                  min=1,max=7,value=4
      ),
      selectInput("attribute_var",
        label="Choose an attribute to map",
        choices=c("Poverty","Education","Percent Senior","Percent African-American"),
        selected="Poverty"
      ),
      sliderInput("slider1",
                  label="Choose number of partitions",
                  min=1,max=7,value=4
      )
    ),
    mainPanel(
      textOutput("selected_var"),
      leafletOutput("election_var_map",height=900),
      leafletOutput("attribute_var_map",height=900)
    )
  )
)

#define server-side logic
server=function(input,output,session){
  output$selected_var=renderText({
    paste("You have selected",input$election_var,"and",input$attribute_var)
  })
  output$election_var_map=renderLeaflet({
    args=switch(input$election_var,
      "Dem Vote '12 (Obama)"=list("Dem_Vote12","Blues"),
      "Dem Vote '16 (Clinton)"=list("Dem_Vote16","Blues"),
      "GOP Vote '12 (Romney)"=list("GOP_Vote12","Reds"),
      "GOP Vote '16 (Trump)"=list("GOP_Vote16","Reds")
    )
    tmap_leaflet(getChor(args,input$slider,input$election_var))
  })
  output$attribute_var_map=renderLeaflet({
    args1=switch(input$attribute_var,
      "Poverty"=list("Poverty","Purples"),
      "Education"=list("Educated","Greens"),
      "Percent Senior"=list("AGE775214","Oranges"),
      "Percent African-American"=list("RHI225214","Greys")
    )
    tmap_leaflet(getChor(args1,input$slider1,input$attributes_var))
  })
}

#run the app godammit
shinyApp(ui = ui, server = server)

The sourced R-script simply creates a data frame from a csv file and binds it to a shapefile:

#call libraries
library(ctv)
library(foreign)
library(vioplot)
library(ggplot2)
library(reshape2)
library(corrplot)
library(rgdal)
library(rgeos)
library(tmap)
library(leaflet)
library(sp)
library(GISTools)
library(RColorBrewer)
library(raster)
library(dismo)

#load and join data
Project_Data=read.csv("project_data.csv")
Spatial.Data=readOGR(".","County_election_2012_16")
Spatial_Election_Data=merge(Spatial.Data,Project_Data,by.x="AFFGEOID",by.y="County_Code")

#set coordinate system at some point once I know the right one
proj4string(Spatial_Election_Data)=CRS("+init=EPSG:4326")
tmap_mode("view")

When I run my shiny app, there are no errors, but neither map appears. Is there something else I need to do? What I’ve done so far is almost identical to what’s done in the tutorial just using a different mapping package.


#2

Can we have a reproducible example?


#3

Hi,

you can look at this link : Tmap Leaflet and shiny integration

# in UI part:
leafletOutput("my_tmap")

# in server part
output$my_tmap = renderLeaflet({
    tm <- tm_shape(World) + tm_polygons("HPI", legend.title = "Happy Planet Index")
    tmap_leaflet(tm)
})

If you don't want ti use the viewer, just set inside #Server :

tmap_mode("plot")