Search code examples
shinyshiny-server

Error: argument


I'm trying to create a histogram of density and I'm having the error: argument 'x' must be numeric. I tried to use (as.numeric(input$d)) instead of just d but got the same error. Does anyone know how to solve this?

server.R

output$hist <- renderPlot({

  input$action

  if(is.null(input$action))
    return(NULL)

  else

    isolate({

      trees3 <- FindTreesCHM(chm(), (as.numeric(input$fws)), (as.numeric(input$minht)))
      d <- density(trees3["height"])
      plot(d, xlab = "Height", ylab = "Density", main = "")
      polygon((as.numeric(input$d)), col = "darkseagreen")

    })
  })

Thank you a lot! :)


Solution

  • I think that the problem is in d <- density(trees3["height"]). The first argument of the density function is x and it should be numeric. You are using [] instead of [[]]. [] return the list of elements and [[]] return the single element on the list. So just try changing

    d <- density(trees3["height"])

    with

    d <- density(trees3[["height"]]).

    Also, I don't think you need the else in your code. But if you need to use an if...else statement make sure that:

    It is important to note that else must be in the same line as the closing braces of the if statements. http://www.programiz.com/r-programming/if-else-statement