igraph / rigraph

igraph R package
https://r.igraph.org
551 stars 200 forks source link

R: rethink operators #21

Closed gaborcsardi closed 9 years ago

gaborcsardi commented 9 years ago

From @gaborcsardi on May 9, 2014 14:5

For creating subgraphs (https://github.com/igraph/xdata-igraph/issues/39) and more.

Copied from original issue: igraph/igraph#614

gaborcsardi commented 9 years ago

Some random ideas:

Comparison operators:

Alternatively < an > could be used for time slicing as well, e.g.

G < 1990

can be the graph before (at, really) 1990, and

G > 1980

can be the graph after 1980, and

G < 1990 > 1980

is the intersection of the two. Somewhat cumbersome and not very readable.

How about using ~ for time slicing? This could be good, actually, although ~ is not generic, so we'll need to shadow the original definition:

`~` <- function(class, slot) UseMethod("~")
`~.default` <- function(class, slot) base::`~`(class, slot)
`~.igraph` <- function(graph, at) { 
  print("foo!"); print(substitute(at))
}
G ~ 100

This works for igraph, but unfortunately this seems to break functions that work with formulas, e.g. lm(), which is a no-go.

ego() to return an ego network, graph.neighborhood with order=1, essentially. Not really an operator, just writing it down, so that I don't forget it.

& could be used to create subgraphs: G & edges(...) keeps only the edges listed, it can be abbreviated as G & E(). It should be flexible enough to select edges with specific attributes, etc. E.g.

G & E(weight > 1)
G & E(color == "red")

G & vertices(...) keeps only the vertices listed (induced subgraph) it can be abbreviated as G & V(). It handles attributes as well:

G & V(degree(G) > 1)

or even

G & V(degree > 1)

in which case it notices that degree is a function, so it calls it with the graph as the argument. This could be done for the already existing E() and V() functions as well.

gaborcsardi commented 9 years ago

On the other hand people can just use https://github.com/smbache/magrittr, which works well with igraph functions in most cases, and is very readable.

igraph operators do not play that well with it, because %>% has a higher precedence than +, etc. so you need to write parens, e.g.

(graph.ring(10) + 5) %>% plot()

which might not be as clear as

graph.ring(10) %>%
  add(5) %>%
  plot()
gaborcsardi commented 9 years ago

Not really needed, so closing now.

github-actions[bot] commented 7 months ago

This old thread has been automatically locked. If you think you have found something related to this, please open a new issue and link to this old issue if necessary.