Skip to content

Be able to read and write to strings in read_igraph, write_igraph #543

@clpippel

Description

@clpippel

What is the feature or improvement you would like to see?

Be able to read and write to strings in read_graph(), write_graph()

For example:
Create graph.

g <- make_ring(10)
write_graph(g)           # output graph to the console, e.g. stdout()
write_graph(g, file="")  # output graph to the console, e.g. stdout()

write_graph(g, text = s) # output graph to string
And read back.

library(igraph)
g <- make_ring(10)
s1 = r"---(0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 8 8 9 0 9)---"
# g1 <- read_graph(text=s1, format="edgelist", directed=FALSE) # proposed
g1 <- read_graph(rawConnection(charToRaw(s1)), format="edgelist", directed=FALSE) # work around as suggested by ntamas
isomorphic(g, g1)

From the R documentation of write.table:

text | character string: if file is not supplied and this is,
then data are read from the value of text via a text connection.
Notice that a literal string can be used to include (small) data sets within R code.

The current implementation of write/read_graph uses binary connections. Therefore, the above construction fails.
One idea is to add ascii =TRUE as in saveRDS.

Use cases for the feature

  1. Avoids interaction with the file system.
  2. Makes it easier to create standalone code samples.
  3. Allows moving graph objects across platforms using cut and paste and plain text files.

References
See also read.csv, write.csv

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions