在闪亮的R中绘制图像

时间:2018-12-19 15:17:43

标签: r plot shiny

我开始使用shinny应用,尝试运行代码时出现以下错误。 output$myrgboutput$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

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

enter image description here

enter image description here

1 个答案:

答案 0 :(得分:1)

没有文件很难提供帮助。但是你应该做类似的事情。使用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")
  })

}