代码之家  ›  专栏  ›  技术社区  ›  GCGM

用计算机绘制图像

  •  0
  • GCGM  · 技术社区  · 6 年前

    我开始玩shinny应用程序,当我尝试运行代码时出现以下错误。这个 output$myrgb output$mynrg 未绘制变量。

    有什么想法吗?

    library(shiny)
    library(leaflet)
    library(dbplyr)
    library(raster)
    library(rgdal)
    
    ui<-fluidPage(
    
      titlePanel("Calculation"),
    
      "SHORT DESCRIPTION ---- ",
      "Study area location",
    
      textInput(inputId = "mypath", label = "Path to Sentinel images"),
      leafletOutput("mymap",height = 1000),
      imageOutput(outputId = "myrgb"),
      imageOutput(outputId = "mynrg"),
      imageOutput(outputId = "ndvi")
    
    
    )
    
    server<-function(input, output) {
    
      output$mymap <- renderLeaflet({
        m <- leaflet() %>%
          addTiles() %>%
          setView(lng=-60.143, lat=-19.9052, zoom=7)
        m
    
        # Load  images
        bands<-c("B((0[2348]_10m)).jp2$")
        S2<-list.files(input$mypath, full.names = TRUE, pattern = ".SAFE")
        S2<-list.files(S2, recursive = TRUE, full.names = TRUE, pattern=bands)
        S2<-lapply(1:length(S2), function (x) {raster(S2[x])})
        S2<-stack(S2) 
    
        utmcoor<-SpatialPoints(cbind(xmin(S2[[1]]),ymax(S2[[1]])), proj4string=CRS(proj4string(S2[[1]]))) # prepare UTM coordinates matrix 
        longlatcoor<-spTransform(utmcoor,CRS("+proj=longlat +datum=WGS84")) # converting
        utmcoor2<-SpatialPoints(cbind(xmax(S2[[1]]),ymin(S2[[1]])), proj4string=CRS(proj4string(S2[[1]]))) # prepare UTM coordinates matrix abajo derecha
        longlatcoor2<-spTransform(utmcoor2,CRS("+proj=longlat +datum=WGS84")) # converting
    
        lng1<-xmin(longlatcoor) # extract coordinates to variable
        lng2<-xmin(longlatcoor2)
        lat1<-ymin(longlatcoor)
        lat2<-ymin(longlatcoor2)
    
        leaflet() %>% addTiles() %>% # Add coordinates to map
          addRectangles(
            lng1=lng1, lat1=lat1,
            lng2=lng2, lat2=lat2,
            fillColor = "transparent")
      }) 
      output$myrgb <- renderPlot({plotRGB(S2, r=3, g=2, b=1, scale=maxValue(S2[[1]]), stretch="lin")})
      output$mynrg <- renderPlot({plotRGB(S2, r=4, g=3, b=2, scale=maxValue(S2[[1]]), stretch="lin")})
    
      }
    
    shinyApp( ui=ui, server=server)
    

    enter image description here

    编辑--- 错误:“closure”类型的对象不可子集

    enter image description here

    1 回复  |  直到 6 年前
        1
  •  2
  •   Stéphane Laurent    6 年前

    reactive conductor 传递光栅对象。

    server<-function(input, output) {
    
      Raster <- reactive({
        bands <- c("B((0[2348]_10m)).jp2$")
        S2 <- list.files(input$mypath, full.names = TRUE, pattern = ".SAFE")
        S2 <- list.files(S2, recursive = TRUE, full.names = TRUE, pattern=bands)
        S2 <- lapply(1:length(S2), function (x) {raster(S2[x])})
        stack(S2) 
      })
    
      output$mymap <- renderLeaflet({
        m <- leaflet() %>%
          addTiles() %>%
          setView(lng=-60.143, lat=-19.9052, zoom=7)
    
        S2 <- Raster()
    
        utmcoor<-SpatialPoints(cbind(xmin(S2[[1]]),ymax(S2[[1]])), proj4string=CRS(proj4string(S2[[1]]))) # prepare UTM coordinates matrix 
        longlatcoor<-spTransform(utmcoor,CRS("+proj=longlat +datum=WGS84")) # converting
        utmcoor2<-SpatialPoints(cbind(xmax(S2[[1]]),ymin(S2[[1]])), proj4string=CRS(proj4string(S2[[1]]))) # prepare UTM coordinates matrix abajo derecha
        longlatcoor2<-spTransform(utmcoor2,CRS("+proj=longlat +datum=WGS84")) # converting
    
        lng1<-xmin(longlatcoor) # extract coordinates to variable
        lng2<-xmin(longlatcoor2)
        lat1<-ymin(longlatcoor)
        lat2<-ymin(longlatcoor2)
    
        m %>% # Add coordinates to map
          addRectangles(
            lng1=lng1, lat1=lat1,
            lng2=lng2, lat2=lat2,
            fillColor = "transparent")
      }) 
    
      output$myrgb <- renderPlot({
        S2 <- Raster()
        plotRGB(S2, r=3, g=2, b=1, scale=maxValue(S2[[1]]), stretch="lin")
      })
    
      output$mynrg <- renderPlot({
        S2 <- Raster()
        plotRGB(S2, r=4, g=3, b=2, scale=maxValue(S2[[1]]), stretch="lin")
      })
    
    }