1

I am new to the 'shiny' package. I just try to produce an aspect map from the existing DEM data using the Shiny dashboard for training purposes. However, I faced the below warning in R and error in Shiny dashboard.

Warning: Error in h: error in evaluating the argument 'x' in selecting a method for function 'plot': unable to find an inherited method for function ‘terrain’ for signature ‘"NULL"’

Error:error in evaluating the argument 'x' in selecting a method for function 'plot': unable to find an inherited method for function ‘terrain’ for signature ‘"character"’

I realized situations where similar errors were encountered but I didn't find the exact reason and didn't solve this issue.

Similar error - I: What does this mean: unable to find an inherited method for function ‘A’ for signature ‘“B”’

Similar error - II: Error in (function (classes, fdef, mtable) : unable to find an inherited method for function ‘shapefile’ for signature ‘"NULL"’

Here is my simple code:

library(shiny)
library(raster)
library(rgdal)

ui=shinyUI(fluidPage(pageWithSidebar(
  headerPanel("Header1"),
  sidebarPanel(
    fileInput('DEM', 'Choose DEM', multiple=FALSE, accept='asc'),
    
  ),
  mainPanel(
    plotOutput("aspect")
  )
)))


server = shinyServer(function(input,output){
  
  inFile <- reactive({
    raster::terrain(input$DEM$datapath, opt = 'aspect')
  })
  
  output$aspect<-renderPlot(
    {
      plot(inFile())
    })
})

shinyApp(ui = ui, server = server)
1

This:

raster::terrain(input$DEM$datapath, opt = 'aspect')

looks like you are calling terrain on the file path - you need to read it into a raster first. Perhaps:

raster::terrain(raster::raster(input$DEM$datapath), opt = 'aspect')

which doesn't remove the first error, which is because the field is empty. Once you upload a DEM you will get a plot:

enter image description here

you should probably put a test for an empty field before reading it - you are going to have to trap all sorts of other failure modes anyway, like people uploading non-DEM GDAL files...

3
  • Unfortunately, the same error arose. Thanks again. Apr 26 at 8:24
  • You'll get the first error the first time because the field is empty so the uploaded file path is empty so there's no data to map. Once you've uploaded a DEM it will work. Put a test for a blank filename and that will stop the initial error.
    – Spacedman
    Apr 26 at 10:07
  • It works, thank you. Apr 26 at 11:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.