【问题标题】:Calculating NDVI in Rshiny在 R Shiny 中计算 NDVI
【发布时间】:2021-02-17 01:21:56
【问题描述】:

我正在尝试创建一个小型闪亮应用程序,该应用程序可以根据从文件夹加载的 tiff 文件绘制 NDVI。

这是我所做的:

library(shiny)
library(leaflet)
library(dbplyr)
library(raster)
library(rgdal)



ui<-fluidPage(
  
  titlePanel("NDVI calculation"),
  
  sidebarPanel(
    fileInput('layer', 'Choose Layer', multiple=FALSE, accept='tiff')
  ),
  imageOutput(outputId = "ndvi")
  
  
)

server<-function(input, output) {
  
    output$ndvi <- renderPlot({
     
      S2 <- input$layer
      
      S2 <- stack(S2)
      
      S2 <- brick(S2)
      
      S2_ndvi <- (S2[[4]] - S2[[1]]) / (S2[[4]] + S2[[1]])
      

      
      plot(S2_ndvi,
           main = "NDVI of site 12RD8",
           axes = FALSE, box = FALSE)    })
    
    
  }
  
  
  


shinyApp( ui=ui, server=server)

但我收到此错误消息:

警告:stack.default 中的错误:至少需要一个向量元素

有人可以帮帮我吗?

【问题讨论】:

    标签: r shiny satellite-image


    【解决方案1】:

    现在,闪亮的应用程序正在尝试从服务器中的任何内容创建一个堆栈,因为当应用程序加载时,input$layer 为空。在上传图层之前隐藏该错误的最简单方法是使用req()。您将所需的输入放在 req() 中,除非这些输入具有值,否则应用将不会运行该代码块。

    server<-function(input, output) {
      
        output$ndvi <- renderPlot({
         req(input$layer)
    
          S2 <- input$layer
          S2 <- stack(S2)
          S2 <- brick(S2)
          S2_ndvi <- (S2[[4]] - S2[[1]]) / (S2[[4]] + S2[[1]])
          
          plot(S2_ndvi,
               main = "NDVI of site 12RD8",
               axes = FALSE, box = FALSE)    })
          
      }
    

    编辑 您还可以根据 if input$layer 为 null is.null(input$layer) 编写 if else 语句,当它为 null 时要求用户上传文件

    【讨论】:

    • 非常感谢eco-Alys!但是现在当图层加载应用程序崩溃时,我在 R 控制台中收到此错误消息:Warning in stack.data.frame(x, ...) : non-vector columns will be ignored Erreur : C stack usage 15922880 is too close to the limit 你知道什么会产生这样的错误吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多