R interface: loop edge label plotmath expression gives warning

Bug #527895 reported by Keith Ponting
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
igraph
Fix Released
Undecided
Unassigned

Bug Description

Trying to use plotmath (i.e. expressions) with edge labels gives warning messages like the following relating to the labels on loop edges:

Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (!is.na(label)) { :
  the condition has length > 1 and only the first element will be used
Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (!is.na(label)) { :
  the condition has length > 1 and only the first element will be used

Some debugging in plot.igraph (see below) revealed that although the labels are a vector of expressions outside the mapply call which invokes the loop function, once you get inside that function the individual label values have become of type language (and of length > 1) rather than of type expression. Therefore a possible fix might be to replace "if (!is.na(label) )" with "if (is.language(label) || !is.na(label))".

To reproduce:

library(igraph)
nodes <- data.frame(name=c("hello","world"),
                    color=rep("grey",2),
                    x=c(-1,1),
                    y=c(1,-1))
labels <- expression(O[t],O[t+1])
nn <- as.character(nodes$name)
edges <- data.frame(from=c(nn,nn),
                    to=c(rev(nn),nn),
                    loop.angle=rep(90,4),
                    curved=0.35,
                    lty=rep(1,4),
                    color=rep("darkgrey",4))
gg <- graph.data.frame(edges,directed=TRUE,vertices=nodes)
V(gg)$label <- labels
plot(gg,layout=as.matrix(subset(nodes,select=c(x,y))),rescale=TRUE,margin=rep(0.2,4))

options(warn=1)
probs <- c(expression(phantom("xxxxxxxx")*P[u](O)),expression(phantom("xxxxxxxx")*P[v](O)),expression(phantom("xxxxxxxx")*P[w](O)),expression(phantom("xxxxxxxx")*P[x](O)))
str(probs)
E(gg)$label <- probs
plot(gg,layout=as.matrix(subset(nodes,select=c(x,y))),rescale=TRUE,margin=c(0,0.2,0.2,0.2))

By the way, I am using the phantom("xxxxxx") used in an attempt to shift the labels off the edges - have I missed a better way to do that? (If not, could I submit a request for some kind of position adjustment for edge labels?)

Session transcript below includes debugging:

R version 2.10.1 Patched (2010-02-02 r51093)
Copyright (C) 2010 The R Foundation for Statistical Computing
ISBN 3-900051-07-0

R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.

  Natural language support but running in an English locale

R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.

Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.

> library(igraph)
> nodes <- data.frame(name=c("hello","world"),
+ color=rep("grey",2),
+ x=c(-1,1),
+ y=c(1,-1))
> labels <- expression(O[t],O[t+1])
> nn <- as.character(nodes$name)
> edges <- data.frame(from=c(nn,nn),
+ to=c(rev(nn),nn),
+ loop.angle=rep(90,4),
+ curved=0.35,
+ lty=rep(1,4),
+ color=rep("darkgrey",4))
> gg <- graph.data.frame(edges,directed=TRUE,vertices=nodes)
> V(gg)$label <- labels
> plot(gg,layout=as.matrix(subset(nodes,select=c(x,y))),rescale=TRUE,margin=rep(0.2,4))
> options(warn=1)
> probs <- c(expression(phantom("xxxxxxxx")*P[u](O)),expression(phantom("xxxxxxxx")*P[v](O)),expression(phantom("xxxxxxxx")*P[w](O)),expression(phantom("xxxxxxxx")*P[x](O)))
> str(probs)
  expression(phantom("xxxxxxxx") * P[u](O), phantom("xxxxxxxx") * P[v](O), phantom("xxxxxxxx") * P[w](O)) ...
> E(gg)$label <- probs
> plot(gg,layout=as.matrix(subset(nodes,select=c(x,y))),rescale=TRUE,margin=c(0,0.2,0.2,0.2))
Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (!is.na(label)) { :
  the condition has length > 1 and only the first element will be used
Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (!is.na(label)) { :
  the condition has length > 1 and only the first element will be used
> sessionInfo()
R version 2.10.1 Patched (2010-02-02 r51093)
i386-pc-mingw32

locale:
[1] LC_COLLATE=English_United Kingdom.1252
[2] LC_CTYPE=English_United Kingdom.1252
[3] LC_MONETARY=English_United Kingdom.1252
[4] LC_NUMERIC=C
[5] LC_TIME=English_United Kingdom.1252

attached base packages:
[1] stats graphics grDevices utils datasets methods base

other attached packages:
[1] igraph_0.5.3
>
> library(debug)
Loading required package: mvbutils
Loading required package: tools

Attaching package: 'mvbutils'

        The following object(s) are masked _by_ .GlobalEnv :

         cd

        The following object(s) are masked from package:graphics :

         clip

Loading required package: tcltk
Loading Tcl/Tk interface ... done
> mtrace(plot.igraph)
> plot(gg)

D(2)> bp(94) # line 94 is mapply(loop ... call
NULL

D(2)> go()
NULL
Vertices: 2
Edges: 4
Directed: TRUE
Edges:

[0] 'hello' -> 'world'
[1] 'world' -> 'hello'
[2] 'hello' -> 'hello'
[3] 'world' -> 'world'

D(2)> str(loop.labels) # just before mapply(loop ...
  expression(phantom("xxxxxxxx") * P[w](O), phantom("xxxxxxxx") * P[x](O))
NULL

D(2)> mtrace(loop) # loop is internal function within plot.igraph, so have to mtrace it here
Mode: function Length: 1 Size: 16004

D(2)>

D(26)> str(label) # now inside loop function - note magic transition from expression to language
 language phantom("xxxxxxxx") * P[w](O)
NULL

D(26)> length(label) # this is immediate cause of the warning
[1] 3

D(26)> go()
NULL
[1] 1.570796
Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (j) TRUE :
  the condition has length > 1 and only the first element will be used

D(26)> str(label)
 language phantom("xxxxxxxx") * P[x](O)
NULL

D(26)> go()
NULL
[1] 1.570796
Warning in is.na(label) :
  is.na() applied to non-(list or vector) of type 'language'
Warning in if (j) TRUE :
  the condition has length > 1 and only the first element will be used
NULL
>

Revision history for this message
Gábor Csárdi (gabor.csardi) wrote :

Thanks for your detailed report, I've fixed this in revision #1786 (0.6-main) and #1591 (0.5-main).

I've also added a bug for the edge-label positioning:
https://bugs.launchpad.net/igraph/+bug/530802

Changed in igraph:
status: New → Fix Released
Revision history for this message
Gábor Csárdi (gabor.csardi) wrote : Continue on github

The development of igraph has moved to github, so please do not comment on this bug here. You are of course welcome to comment on github, here:
https://github.com/igraph/igraph/issues/411

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.