Kathryn
Kathryn

Reputation: 35

R: what is a meaning of this error message in a user-defined function?

I'm working on a homework question that asks me to write a function that will graph different types of plots based on input data.

The arguments to the function are x, y, and type, where x and y are the vectors I want to plot and type is the type of plot (scatter, box, or histogram).

The function needs to be written so that if you don't specify data for x with type scatterplot, you get an error message saying that you need x data to make a scatterplot.

Likewise, if you do specify data for x with the type histogram or boxplot, you get an error message saying that you only need y data for these plot types.

I have a function written that produces the correct graphs and error messages, but also gives me a warning message:

In if (y == FALSE & type == 1) { :
   the condition has length > 1 and only the first element will be used

The function is below. Can somebody tell me why I'm getting this particular warning?

plot.function2=function(x=FALSE,y=FALSE,type){
  if(x==FALSE & type==1){
    stop("Error Message 1")
  }else if (y==FALSE & type==1){
    stop("Error Message 1.5")
  }else if(type==1){
    plot=plot(y~x) 
  }else if (x==TRUE & type==2){
    stop("Error Message 2")
  }else if(type==2){
    plot=boxplot(y)
  }else if(type==3){
    plot=barplot(y)
  }

plot
}

The message shows up for most inputs; for instance, entering plot.function2(v1, v2, 1) gets me a scatterplot of the two vectors, but also the warning message. Thanks!

Upvotes: 0

Views: 1348

Answers (1)

cdeterman
cdeterman

Reputation: 19970

You are comparing vectors to boolean with your function.

# Here if you provide a vector x, you are essentially checking
# if each element is TRUE, hence the warning

  if(x==FALSE & type==1){
    stop("Error Message 1")
  }

As an example, see a vector from the iris dataset

> iris[1:25,1] == TRUE
 [1] FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE FALSE

You should use NULL and is.null to see if the object is empty

plot.function2=function(x=NULL,y=NULL,type){
  if(is.null(x) & type==1){
    stop("Error Message 1")
  }else if (is.null(y) & type==1){
    stop("Error Message 1.5")
  }else if(type==1){
    plot=plot(y~x) 
  }else if (is.null(x) & type==2){
    stop("Error Message 2")
  }else if(type==2){
    plot=boxplot(y)
  }else if(type==3){
    plot=barplot(y)
  }
plot
}

As per a suggestion, this could be cleaned up with a switch statement which as you can see .

plot.function2=function(x=NULL,y=NULL,type){  
  # check if nulls
  if(is.null(y)){
    stop("You need to provide y data!!!")
  }
  if(is.null(x) & type == 1){
    stop("You need to provide x data!!!")
  }

  plot <- switch(as.character(type),
                 "1" = plot(y~x),
                 "2" = boxplot(y),
                 "3" = barplot(y))
  plot
}

Upvotes: 2

Related Questions