代码之家  ›  专栏  ›  技术社区  ›  Danish Zahid Malik

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

  •  1
  • Danish Zahid Malik  · 技术社区  · 6 年前

    我是新来的闪亮人,现在我正试图在我闪亮的应用程序中绘制巴基斯坦地图。我目前使用的代码在控制台上运行良好,但我不太确定如何在我闪亮的应用程序中实现它。[![控制台输出][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) 
    
    #

    这就是我目前在闪亮应用程序中所做的:

    **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 回复  |  直到 6 年前
        1
  •  0
  •   Jakub Buček    6 年前

    在里面 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)
      })
    })