Search code examples
rshinyvisualizationraster

How to plot a map from gadm in shiny r?


im new to shiny and currrently m trying to plot the map of Pakistan in my shiny app. The code that im currently using works perfectly on the console, but im not quite sure as to how to implement it in my shiny app. [![Console Output][1]][1]THE code:

library(maptools)
library(raster)
    adm <- raster::getData('GADM', country='PAK', level=2),
          mar<-(adm),
          plot(mar, bg="dodgerblue", axes=T),
          ##Plot downloaded data
          plot(mar, lwd=10, border="skyblue", add=T),
          plot(mar,col="green4", add=T),
          grid(),
          box(),
          invisible(text(getSpPPolygonsLabptSlots(mar), labels=as.character(mar$NAME_2), cex=1.1, col="white", font=2)),
          mtext(side=3, line=1, "Pakistan", cex=2),
          mtext(side=1, "Longitude", line=2.5, cex=1.1),
          mtext(side=2, "Latitude", line=2.5, cex=1.1) 
#

This is what im currently doing in the Shiny app:

**ui.r**

    shinyUI(
      navbarPage(
        inverse = TRUE,
        title = "Campaign Launch Demo",
    tabPanel("Daily Insights",
    sidebarLayout(
          sidebarPanel( )


     mainPanel(
          fluid = TRUE,
        uiOutput('map2')
        )
        )
        )
        )
        )

and this the code for the server script:
     **server.R**
       shinyServer(function(input, output, session){
         output$map2 <- 
            print(
              ## Download data from gadm.org 
              adm <- raster::getData('GADM', country='PAK', level=2),
              mar<-(adm),
              plot(mar, bg="dodgerblue", axes=T),
              ##Plot downloaded data
              plot(mar, lwd=10, border="skyblue", add=T),
              plot(mar,col="green4", add=T),
              grid(),
              box(),
              invisible(text(getSpPPolygonsLabptSlots(mar), labels=as.character(mar$NAME_2), cex=1.1, col="white", font=2)),
              mtext(side=3, line=1, "Pakistan", cex=2),
              mtext(side=1, "Longitude", line=2.5, cex=1.1),
              mtext(side=2, "Latitude", line=2.5, cex=1.1)
        )   }



  [1]: 

https://i.sstatic.net/ihfvM.png


Solution

  • In ui.R use plotOutput() instead of uiOutput().

    shinyUI(
      navbarPage(
      inverse = TRUE,
      title = "Campaign Launch Demo",
      tabPanel("Daily Insights",
         sidebarLayout(
            sidebarPanel(),
            mainPanel(fluid = TRUE, plotOutput('map2'))
         ))))
    

    In server.R use renderPlot() function and get rid of the commas at the end of every line.

    library(maptools)
    library(raster)
    
    shinyServer(function(input, output, session){
      output$map2 <- renderPlot({
        ## Download data from gadm.org 
        adm <- raster::getData('GADM', country='PAK', level=2)
        mar<-(adm)
        plot(mar, bg="dodgerblue", axes=T)
        ##Plot downloaded data
        plot(mar, lwd=10, border="skyblue", add=T)
        plot(mar,col="green4", add=T)
        grid()
        box()
        invisible(text(getSpPPolygonsLabptSlots(mar), labels=as.character(mar$NAME_2), cex=1.1, col="white", font=2))
        mtext(side=3, line=1, "Pakistan", cex=2)
        mtext(side=1, "Longitude", line=2.5, cex=1.1)
        mtext(side=2, "Latitude", line=2.5, cex=1.1)
      })
    })