Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I do not understand error "object not found" inside the function

Tags:

r

I have roughly this function:

plot_pca_models <- function(models, id) {
  library(lattice)

  splom(models, groups=id)
}

and I'm calling it like this:

plot_pca_models(data.pca, log$id)

wich results in this error:

Error in eval(expr, envir, enclos) : object 'id' not found

when I call it without the wrapping function:

splom(data.pca, groups=log$id)

it raises this error:

Error in log$id : object of type 'special' is not subsettable

but when I do this:

 id <- log$id
 splom(models, groups=id)

it behaves as expected.

Please can anybody explain why it behaves like this and how to correct it? Thanks.

btw: I'm aware of similar questions here, eg:

  • Help understand the error in a function I defined in R
  • Object not found error with ddply inside a function
  • Object disappears from namespace in function

but none of them helped me.

edit: As requested, there is full "plot_pca_models" function:

plot_pca_models <- function(data, id, sel=c(1:4), comp=1) {
  # 'data' ... princomp objects
  # 'id'   ... list of samples id (classes)
  # 'sel'  ... list of models to compare
  # 'comp' ... which pca component to compare

  library(lattice)

  models <- c()
  models.size <- 1:length(data)
  for(model in models.size) {
    models <- c(models, list(data[[model]]$scores[,comp]))
  }
  names(models) <- 1:length(data)

  models <- do.call(cbind, models[sel])

  splom(models, groups=id)
}

edit2: I've managed to make the problem reproducible.

require(lattice)
my.data <- data.frame(pca1 = rnorm(100), pca2 = rnorm(100), pca3 = rnorm(100))
my.id <- data.frame(id = sample(letters[1:4], 100, replace = TRUE))

plot_pca_models2 <- function(x, ajdi) {
  splom(x, group = ajdi)
}

plot_pca_models2(x = my.data, ajdi = my.id$id)

which produce the same error like above.

like image 658
WestFlame Avatar asked Aug 01 '13 11:08

WestFlame


People also ask

Why does R keep saying object not found?

This error usually occurs for one of two reasons: Reason 1: You are attempting to reference an object you have not created. Reason 2: You are running a chunk of code where the object has not been defined in that chunk.

What does error object mean?

The error object is a built-in object that provides a standard set of useful information when an error occurs, such as a stack trace and the error message.

What are objects in R?

Objects are the instance of the class. Also, everything in R is an object and to know more look at Data types in R. They also can have their attributes like class, attributes,dimnnames, names, etc.


2 Answers

The problem is that splom evaluates its groups argument in a nonstandard way.A quick fix is to rewrite your function so that it constructs the call with the appropriate syntax:

f <- function(data, id)
eval(substitute(splom(data, groups=.id), list(.id=id)))

# test it
ir <- iris[-5]
sp <- iris[, 5]
f(ir, sp)
like image 154
Hong Ooi Avatar answered Oct 14 '22 20:10

Hong Ooi


log is a function in base R. Good practice is to not name objects after functions...it can create confusion. Type log$test into a clean R session and you'll see what's happening:

object of type 'special' is not subsettable

like image 41
Thomas Avatar answered Oct 14 '22 20:10

Thomas