Categories
data science

Make Dot file from nCol Data Frame – R

### NOTE TO SELF
# You have obviously loaded the igraph package
library(igraph)

# Now for the function
make.dot.file<-function(df,location){
    # Location where you want the file to be saved
    loc<-location
    # From data frame to graph
    g<-graph.data.frame(df,directed=FALSE)
    # Simplify Graph
    sg<-simplify(g)
    # Write header to create dot file including folds for Vim
    dotHeader<-(
    "graph{\n
    //<<< Graph Settings
    \tsplines=TRUE;
    \tmargin=\"0,0\";
    \tfontsize=10;
    \tcenter=TRUE;
    \tlayout=sfdp;
    \tbgcolor=\"#ffffff\";
    \toverlap=FALSE;
    \tnode[shape=circle,height=.1,label=\"\"];
    \tedge[color=gray40,];\n
    //>>>\n\n//<<< ncol\n
    "
    )
    # Now put that stuff in a file
    write(
        dotHeader,loc
    )
    # Write as table and add the "--" to create edges
    write.table(
        get.edgelist(sg),
        loc,sep="--",
        row.names=FALSE,
        col.names=FALSE,
        append=TRUE
    )
    # write footer to finish formating the dot file
    endBit<-c("\n//>>>\n\n}")
    write(
        endBit,loc,append=TRUE
    )
}

The output file will create something like this

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s