如何在闪亮的r中绘制来自gadm的地图?

时间:2018-04-12 12:05:56

标签: r shiny visualization raster

我是新手,闪亮而且好奇地想在我闪亮的应用程序中绘制巴基斯坦地图。我目前使用的代码在控制台上完美运行,但我不太确定如何在我的闪亮应用程序中实现它。 [![控制台输出] [1]] [1]代码:

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) 

这就是我目前在Shiny应用程序中所做的事情:

**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.stack.imgur.com/ihfvM.png

1 个答案:

答案 0 :(得分:0)

ui.R中使用plotOutput()代替uiOutput()

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

server.R中使用renderPlot()函数并删除每行末尾的逗号。

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)
  })
})