0

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! :)

Xiongbing Jin
  • 11,779
  • 3
  • 47
  • 41

1 Answers1

0

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

Geovany
  • 5,389
  • 21
  • 37
  • Hey Geovany! I totally see your point and it worked well man! I really appreciate your help. But here's a doubt. When you say hat using [[ ]] returns the single element on the list you mean one value from the column? Cause I'd like to capture the entire column to plot. I'm sorry about my confusion. Thanks a lot! – Iasmim Louriene Oct 21 '16 at 21:25
  • Glad to know that it help you. The single element in the list, so it could one value, a vector, or any other structure stored in the list. In this case it will be the entire column. You can get access to individual values by `[["heigh"]][n]` – Geovany Oct 21 '16 at 22:05
  • Oh, I got it Geovany! Thanks so much for your help and explanation! :) – Iasmim Louriene Oct 23 '16 at 01:40