igraph-help
[Top][All Lists]

## Re: [igraph] Question related to R implementation

 From: Csardi Gabor Subject: Re: [igraph] Question related to R implementation Date: Wed, 29 Aug 2007 16:25:40 +0200 User-agent: Mutt/1.5.9i

```Hi, these are more R questions than igraph.

On Wed, Aug 29, 2007 at 10:48:54AM +0200, Sylvain Loiseau wrote:
> Hi,
>
> My question is perhaps not really related to the igraph library usage but
> rather to R. I'm curious to understand how the library is implemented. I'm
> particular, I'm wondering:
>
> - How the vector of the object igraph.vs and igraph.es can be O-based? If
> I try :
>
>   mode(V(g))
>
>
>   [1] "numeric"

Yes, but

> g <- graph.ring(10)
> class(V(g))
[1] "igraph.vs"

and in iterators.R in the igraph source we have

"[.igraph.vs" <- function(x, i) {
i <- substitute(i)
if (is.numeric(i) || is.integer(i)) {
# simple indexing by vertex ids
res <- i[ i %in% x ]
attributes(res) <- attributes(x)
} else if (is.logical(i)) {
# simple indexing by logical vector
res <- as.numeric(x) [ i ]
attributes(res) <- attributes(x)
} else {
[...]

See ?UseMethod.

> It's probably usefull for linking with the C code, but how such a
> primitive R object as a vector can be redifined as being 0-based?

You mean that it is confusing? I agree. But I chose it to be 0-based
because of consistency with the C, Python, etc. versions.

> - I don't understand why the V(g) object can behave like a list (having
> "\$" subscript: V(g)\$name), if they are stored as vector.

Because we have

"\$.igraph.vs" <- function(x, name) {
get.vertex.attribute(get("graph", attr(x, "env")), name, x)
}

Again, see ?UseMethod.

> - And a last question: what is this subscript syntax:
>
>   V(g)[name == "foo"]
>
>   Why don't we need to write
>
>   V(g)[V(g)\$name == "foo"]
>
>   ?

Tricky. In [.igraph.vs we have this:

as.list(parent.frame())))

That means that we evaluate the expression (that is i, the formula you
give in the brackets) in an environment which contains graph[[9]][[3]],
the named vertex attributes, adj, nei, from, to, and as.list(parent.frame()),
the last one contains the variables in the fucntion you're calling from or
global variables. It is similar to what the 'with' R function does,
see ?with for some hints.

Might be not completely clear yet, but try to follow the help pages i
suggested and read the igraph source code. You can still ask on the
igraph list if it is still not clear.

Best, Gabor

> Cheers,
> Sylvain
>
> --
> Sylvain Loiseau
> http://www.limsi.fr/~sloiseau
>
> FainÃ©anter dans un monde neuf est la plus absorbante des occupations.
>
> Nicolas Bouvier, /L'usage du monde/.
>
>
> _______________________________________________
> igraph-help mailing list