Advertisement
BenjaminLind

PackageNetworks.R

Jul 10th, 2014
384
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 2.48 KB | None | 0 0
  1. #####
  2. ### The code below is written by Benjamin Lind under CC: BY-NC-SA 3.0
  3. ### If you use or develop this code, please provide proper attribution.
  4. ### You may contact me at lind.benjamin//at//gmail//d0t//com
  5. ### You may also find me @benjaminlind on Twitter
  6. #####
  7.  
  8. #####
  9. ### See: http://blog.revolutionanalytics.com/2014/07/dependencies-of-popular-r-packages.html
  10. ### To run this script, enter: source("http://pastebin.com/raw.php?i=2Fa2Uz0N")
  11. #####
  12.  
  13. library(igraph)
  14. library(compiler)
  15.  
  16. ap <- available.packages()
  17. ap <- ap[, c("Package", "Depends", "Suggests")]
  18.  
  19. pack2el <- function(packname, packmat, packrel){
  20.   # _packname_ is the package name, a character
  21.   # _packmat_ is the matrix produced by available.packages()
  22.   # _packrel_ is the relationship between packages. Should be either "Depends" or "Suggests"
  23.   ind <- which(packmat[, "Package"] == packname)
  24.   alters <- packmat[ind, packrel]
  25.   # Clean up the input
  26.   alters <- gsub(" ", "", alters, fixed = TRUE)
  27.   alters <- gsub("\n", "", alters, fixed = TRUE)
  28.   alters <- gsub("\t", "", alters, fixed = TRUE)
  29.   alters <- gsub("\r", "", alters, fixed = TRUE)
  30.   alters <- unlist(strsplit(alters, ",", fixed = TRUE))
  31.   # Drop R version dependency
  32.   rvers <- grep("R(>", alters, fixed = TRUE)
  33.   if (length(rvers) > 0)
  34.     alters <- alters[-rvers]
  35.   # Drop the version numbers of packages
  36.   alters <- sapply(alters, function(x) return(strsplit(x, "(", fixed = TRUE)[[1]][1]))
  37.   if (length(alters) == 0)
  38.     alters <- NA
  39.   outdfnrow <- length(alters)
  40.   ego <- rep(packname, times = outdfnrow)
  41.   if (packrel == "Depends")
  42.     outdf <- data.frame(Sender = alters, Receiver = ego, stringsAsFactors = FALSE, row.names = 1:outdfnrow)
  43.   else if (packrel == "Suggests")
  44.     outdf <- data.frame(Sender = ego, Receiver = alters, stringsAsFactors = FALSE, row.names = 1:outdfnrow)
  45.   return (outdf)
  46.   }
  47. pack2el <- cmpfun(pack2el)
  48.  
  49. depel <- do.call(rbind, lapply(ap[, "Package"], pack2el, packmat = ap, packrel = "Depends"))
  50. depel <- depel[which(is.na(depel[, "Sender"]) == FALSE), ]
  51. rownames(depel) <- 1:nrow(depel)
  52.  
  53. sugel <- do.call(rbind, lapply(ap[,"Package"], pack2el, packmat = ap, packrel = "Suggests"))
  54. sugel <- sugel[which(is.na(sugel[, "Receiver"]) == FALSE), ]
  55. rownames(sugel) <- 1:nrow(sugel)
  56.  
  57. packnames <- sort(unique(c(ap[,"Package"], depel[, "Sender"], sugel[, "Receiver"])))
  58.  
  59. dep.ig <- graph.data.frame(depel, TRUE, packnames)
  60. sug.ig <- graph.data.frame(sugel, TRUE, packnames)
  61.  
  62. rm(ap, pack2el, depel, sugel, packnames)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement