Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a string as literal to create a graph object

Tags:

r

igraph

We can pass literals to make a graph as below:

# R version 4.0.2 (2020-06-22)
library(igraph) # igraph_1.2.6

graph_from_literal(A-B)
# IGRAPH 7e5604a UN-- 2 1 -- 
# + attr: name (v/c)
# + edge from 7e5604a (vertex names):
#   [1] A--B

Thought passing a string would work, but it doesn't. (This is a small example "A-B", imagine a long complex string):

graph_from_literal("A-B")
# IGRAPH 8b32703 UN-- 1 0 -- 
# + attr: name (v/c)
# + edges from 8b32703 (vertex names):

# objects are not the same
identical_graphs(graph_from_literal(A-B),
                 graph_from_literal("A-B"))
# [1] FALSE

Either I am using a wrong function or I need another function to drop the quotes. cat, noquote didn't work. Ideas?


Edit 1: I would want to avoid string manipulation: split "A-B" into "from" and "to", then using graph_from_dataframe. For example, splitting into 2 columns would not work for a simple input of "A-B-C".

Edit 2: Motivation came from another question where I thought I could use igraph package as a solution. I substituted dots with dashes, wanted to convert that string into a graph object, but realised literal doesn't like string input.

So the longer question: How would I convert below into a graph object?

# input
c('0.1', '0.1.1', '0.1.2', '0.11', '0.12', '0.11.1', '0.12.1', '0.12.2')

# expected output:
graph_from_literal(0-1, 0-1-1, 0-1-2, 0-11, 0-12, 0-11-1, 0-12-1, 0-12-2)
# IGRAPH 0792a00 UN-- 5 7 -- 
# + attr: name (v/c)
# + edges from 0792a00 (vertex names):
#   [1] 0--1  0--11 0--12 1--2  1--11 1--12 2--12

Edit 3: There is now a related open GitHub issue 475 to address this functionality.

like image 239
zx8754 Avatar asked Sep 20 '21 09:09

zx8754


People also ask

How do you make a string literal?

This means that a string literal is written as: a quote, followed by zero, one, or more non-quote characters, followed by a quote. In practice this is often complicated by escaping, other delimiters, and excluding newlines.

Is string literal an object?

A String literal is a String object, but a String object is not necessarily a String literal. And once assigned to a reference variable, it's all but impossible to tell if a given String object is a literal or not.

What is the difference between creating string object using new and string literals?

When we create a String object using the new() operator, it always creates a new object in heap memory. On the other hand, if we create an object using String literal syntax e.g. “Baeldung”, it may return an existing object from the String pool, if it already exists.

What is a string literal in C++?

String literals. A string literal represents a sequence of characters that together form a null-terminated string. The characters must be enclosed between double quotation marks.


Video Answer


2 Answers

Split the input by comma, parse into an expression and call the internal graph_from_literal_i. No packages other than igraph are used.

graph_from_string <- function(x) {
  e <- str2expression(strsplit(x, ",")[[1]])
  do.call(igraph:::graph_from_literal_i, list(e))
}

# test 1
graph_from_string("A-B")
## IGRAPH 063d605 UN-- 2 1 -- 
## + attr: name (v/c)
## + edge from 063d605 (vertex names):
## [1] A--B

# test 2 - a more complex example
graph_from_string("A-B-C, D-E")
## IGRAPH b155b39 UN-- 5 3 -- 
## + attr: name (v/c)
## + edges from b155b39 (vertex names):
## [1] A--B B--C D--E

If there are no commas in the input then this one-liner would also work:

do.call("graph_from_literal", list(parse(text = "A-B")[[1]]))
## IGRAPH dad0219 UN-- 2 1 -- 
## + attr: name (v/c)
## + edge from dad0219 (vertex names):
## [1] A--B
like image 154
G. Grothendieck Avatar answered Dec 17 '22 07:12

G. Grothendieck


Perhaps we can do something like this

> x <- "A-B-C, D-E"

> eval(str2lang(sprintf("graph_from_literal(%s)", x)))
IGRAPH ab43602 UN-- 5 3 -- 
+ attr: name (v/c)
+ edges from ab43602 (vertex names):
[1] A--B B--C D--E
like image 32
ThomasIsCoding Avatar answered Dec 17 '22 06:12

ThomasIsCoding