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

I have something like this:

plot_pca_models <- function(models, id) { library(lattice) splom(models, groups=id) } 

and I call it like this:

 plot_pca_models(data.pca, log$id) 

leading to this error:

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

when I call it without a wrapping function:

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

it causes 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) 

he behaves as expected.

Please can someone explain why he is behaving this way and how to fix him? Thanks.

By the way: I know similar questions here, for example:

but none of them helped me.

edit : As requested, there is a complete 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 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 as above.

+6
source share
3 answers

The problem is that splom evaluates its groups argument in a non-standard way. A quick fix is ​​to rewrite your function so that it builds a 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) 
+2
source

log is a function in the R database. Good practice is to not name objects after functions ... this can create confusion. Enter log$test in a clean R session and you will see what happens:

object of type 'special' is not subsettable

+2
source

There is a Hong Oi answer option here. First, I would recommend including id in the main data frame, i.e.

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

.. and then

 plot_pca_models2 <- function(x, ajdi) { Call <- bquote(splom(x, group = x[[.(ajdi)]])) eval(Call) } plot_pca_models2(x = my.data, ajdi = "id") 

The reason for the confusion is the following line in the lattice: splom.formula:

 groups <- eval(substitute(groups), data, environment(formula)) 

... whose only point is to be able to specify groups without quotes, i.e.

 # instead of splom(DATA, groups="ID") # you can now be much shorter, thanks to eval and substitute: splom(DATA, groups=ID) 

But, of course, this makes using splom (and other functions, such as replacements that use a "non-standard rating") harder to use from other functions, and contradicts the philosophy that is "mainly" used in the rest of R.

+2
source

Source: https://habr.com/ru/post/950806/


All Articles