[R] Suppressing error messages printed in xyplot() with panel function

Deepayan Sarkar deepayan.sarkar at gmail.com
Thu Aug 25 06:47:39 CEST 2011


On Thu, Aug 25, 2011 at 1:00 AM, Adam Zeilinger <zeil0006 at umn.edu> wrote:
> Hello,
>
> I am using the xyplot() function to create a series of scatterplot panels
> with lines of best fit.  To draw the lines of best fit for each panel, I am
> using a panel function.  Here's an example:
>
>> species <- as.character(c(rep(list("A", "B", "A"), 10), "B"))
>> year <- as.character(c(rep(list("2009", "2009", "2010"), 10), "2010"))
>> x <- rnorm(31, mean = 50, sd = 1)
>> y <- 3*x + 20
>> ex.data <- data.frame(cbind(species, year, x, y))
>> ex.data$x <- as.numeric(ex.data$x)
>> ex.data$y <- as.numeric(ex.data$y)
>> xyplot(y ~ x|species*year, data = ex.data,
> +   panel = function(x, y) {
> +   panel.xyplot(x, y, pch=16, col="black")
> +   panel.abline(lm(y ~ x))})
>
> With my data set, there are some panels with less than 2 data points.  In
> these panels, an error message is printed in the panel, something like:
> "Error using packet 4 missing value where TRUE/FALSE needed."
>
> In the panels with error messages, I want to keep the panels but suppress
> the error message, such that the panel is blank or has only one datum.  How
> do I do suppress the printing of the error message?

Generally speaking, suppressing error messages should not be your
first instinct. If you know why the error is happening, it's better to
make sure it never happens, e.g., with something like

   if (length(x) >= 2) panel.abline(lm(y ~ x))

Of course, as Dennis says, in this case you can take advantage of the
'type' argument, which already implements what you want.

-Deepayan



More information about the R-help mailing list