Search code examples
rplumber

Invalid Arguments for rnorm() with plumber


I am trying my had at building a simple R plumber API file.

Here is the file:

#* Plot out data from a random normal distribution
#* @param mean The mean of the standard normal deviation
#* @get /plot
#* @serializer png
function(mean) {
    hist(rnorm(n = 1000, mean = mean, sd = 1))
}

This fails with the following Error:

{
  "error": "500 - Internal server error",
  "message": "Error in rnorm(n = 1000, mean = mean, sd = 1): invalid arguments\n"
}

I do not know why this is failing, given the conical example from plumber is not horrendously different as seen here (and works when I run it):

#* Plot out data from the iris dataset
#* @param spec If provided, filter the data to only this species (e.g. 'setosa')
#* @get /irisplot
#* @serializer png
function(spec){
  myData <- iris
  title <- "All Species"
  
  # Filter if the species was specified
  if (!missing(spec)){
    title <- paste0("Only the '", spec, "' Species")
    myData <- subset(iris, Species == spec)
  }
  
  plot(myData$Sepal.Length, myData$Petal.Length,
       main=title, xlab="Sepal Length", ylab="Petal Length")
}

So I am not sure what is going wrong here.


Solution

  • You have to convert the mean value passed to your function to a numeric:

    #* Plot out data from a random normal distribution
    #* @param mean The mean of the standard normal deviation
    #* @get /plot
    #* @serializer png
    function(mean) {
      hist(rnorm(n = 1000, mean = as.numeric(mean), sd = 1))
    }
    

    enter image description here