Repeat the process of creating a .png file based on multiple dataframes using lapply or loop

Viewed 47

I have the nodes and edges dataframes below and then I create a graph object gph. After that I create an list of igraph.vs objects named asp with all simple paths. Then I want to be able to use a for loop, or a lappy() in order to create as many dataframes as the length of the list with the names of each igraph.vs object, as nodes datasets, and based on those nodes datasets to create as many relative edges datasets. Then I use nodes and edges to create all networks and then as many .png as networks. So if the list of asp contains 7 igraph.vs objects I should create 7 .png files. Below is the process for 1 file.

name<-c("Robert Zero",       "Amy Adams",         "Barry Beane",       "Henry Handler",     "Julie Jones",      
       "Charlie Cheese",    "Karen Klein",       "Lolly Landswright", "Imogene Isler",     "David Dufresne",   
       "Frank Flaherty",    "Egbert English",    "George Graham",     "Molly McKee",       "Norman Needle",    
       "Oscar Oliver",      "Peter Platteville", "Quincy Quebec",     "Roger Rabbit",      "Steve Sweet",      
       "Tom Thompson",      "Victor Valentino",  "Ulrich Uxbridge" )
label<-c("Robert Zero",       "Amy Adams",         "Barry Beane",       "Henry Handler",     "Julie Jones",      
         "Charlie Cheese",    "Karen Klein",       "Lolly Landswright", "Imogene Isler",     "David Dufresne",   
         "Frank Flaherty",    "Egbert English",    "George Graham",     "Molly McKee",       "Norman Needle",    
         "Oscar Oliver",      "Peter Platteville", "Quincy Quebec",     "Roger Rabbit",      "Steve Sweet",      
         "Tom Thompson",      "Victor Valentino",  "Ulrich Uxbridge" )
nodes<-data.frame(name,label)


from<-c("Robert Zero",       "Amy Adams",         "Barry Beane",       "Barry Beane",       "Barry Beane",      
         "Julie Jones",       "Karen Klein",       "Imogene Isler",     "David Dufresne",    "Frank Flaherty",   
         "Egbert English",    "George Graham",     "Molly McKee",       "Molly McKee",       "Amy Adams",        
         "Peter Platteville", "Amy Adams",         "Roger Rabbit",      "Julie Jones",       "Robert Zero",      
         "Steve Sweet",       "Tom Thompson",      "Steve Sweet",       "Ulrich Uxbridge",   "Henry Handler",    
         "Barry Beane",       "Barry Beane",       "Charlie Cheese",    "Barry Beane",       "Victor Valentino", 
         "Tom Thompson"     )
to<-c( "Amy Adams",         "Barry Beane",       "Henry Handler",     "Julie Jones",       "Charlie Cheese",   
       "Karen Klein",       "Lolly Landswright", "Robert Zero",       "Frank Flaherty",    "Robert Zero",      
       "George Graham",     "Robert Zero",       "Norman Needle",     "Julie Jones",       "Oscar Oliver",     
       "Robert Zero",       "Quincy Quebec",     "Robert Zero",       "Molly McKee",       "Steve Sweet",      
       "Tom Thompson",      "Victor Valentino",  "Ulrich Uxbridge",   "Barry Beane",       "Robert Zero",      
       "Imogene Isler",     "Peter Platteville", "Robert Zero",       "Roger Rabbit",      "Robert Zero",      
       "Roger Rabbit"   )

edges<-data.frame(from,to)


#Combine them in order to create the graph object
library(igraph)
library(webshot); #webshot::install_phantomjs() #in case phantomjs was not installed
library(visNetwork)
library(dplyr)

gph <- graph_from_data_frame(edges, directed=TRUE, vertices=nodes)

#Create the simple paths asp 
asp <- all_simple_paths(gph, "Steve Sweet", "Robert Zero")

anodes<-data.frame()
aedges<-data.frame()
for(i in 1 :length(asp)){
  #new nodes
  anodes[i]<-data.frame(names(asp[[i]]))
  #new edges
  aedges<-data.frame(anodes %>% mutate(to = lead(id, default = first(id))))
  colnames(aedges)[1]<-"from"
  aedges$label<-""
  aedges<-merge(edges, aedges, by = c("from", "to"))
  #create network
  vis<-visNetwork(anodes, aedges, width = "100%")
  
  #save as png in directory
  html_name <- tempfile(fileext = ".html")
  visSave(vis, html_name)
  webshot(html_name, zoom = 2, file = paste("Path", i))
  
}
1 Answers

I guess what you are after is to plot sub-graphs, and you may try the code below using induced_subgraph

for (k in seq_along(asp)) {
  png(paste0("sub_gph_",k,".png"))
  plot(induced_subgraph(gph, asp[[k]]))
  dev.off()
}
Related