Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #####
- ### The code below is written by Benjamin Lind under CC: BY-NC-SA 3.0
- ### If you use or develop this code, please provide proper attribution.
- ### You may contact me at lind.benjamin//at//gmail//d0t//com
- ### You may also find me @benjaminlind on Twitter
- #####
- #####
- ### See: http://blog.revolutionanalytics.com/2014/07/dependencies-of-popular-r-packages.html
- ### To run this script, enter: source("http://pastebin.com/raw.php?i=2Fa2Uz0N")
- #####
- library(igraph)
- library(compiler)
- ap <- available.packages()
- ap <- ap[, c("Package", "Depends", "Suggests")]
- pack2el <- function(packname, packmat, packrel){
- # _packname_ is the package name, a character
- # _packmat_ is the matrix produced by available.packages()
- # _packrel_ is the relationship between packages. Should be either "Depends" or "Suggests"
- ind <- which(packmat[, "Package"] == packname)
- alters <- packmat[ind, packrel]
- # Clean up the input
- alters <- gsub(" ", "", alters, fixed = TRUE)
- alters <- gsub("\n", "", alters, fixed = TRUE)
- alters <- gsub("\t", "", alters, fixed = TRUE)
- alters <- gsub("\r", "", alters, fixed = TRUE)
- alters <- unlist(strsplit(alters, ",", fixed = TRUE))
- # Drop R version dependency
- rvers <- grep("R(>", alters, fixed = TRUE)
- if (length(rvers) > 0)
- alters <- alters[-rvers]
- # Drop the version numbers of packages
- alters <- sapply(alters, function(x) return(strsplit(x, "(", fixed = TRUE)[[1]][1]))
- if (length(alters) == 0)
- alters <- NA
- outdfnrow <- length(alters)
- ego <- rep(packname, times = outdfnrow)
- if (packrel == "Depends")
- outdf <- data.frame(Sender = alters, Receiver = ego, stringsAsFactors = FALSE, row.names = 1:outdfnrow)
- else if (packrel == "Suggests")
- outdf <- data.frame(Sender = ego, Receiver = alters, stringsAsFactors = FALSE, row.names = 1:outdfnrow)
- return (outdf)
- }
- pack2el <- cmpfun(pack2el)
- depel <- do.call(rbind, lapply(ap[, "Package"], pack2el, packmat = ap, packrel = "Depends"))
- depel <- depel[which(is.na(depel[, "Sender"]) == FALSE), ]
- rownames(depel) <- 1:nrow(depel)
- sugel <- do.call(rbind, lapply(ap[,"Package"], pack2el, packmat = ap, packrel = "Suggests"))
- sugel <- sugel[which(is.na(sugel[, "Receiver"]) == FALSE), ]
- rownames(sugel) <- 1:nrow(sugel)
- packnames <- sort(unique(c(ap[,"Package"], depel[, "Sender"], sugel[, "Receiver"])))
- dep.ig <- graph.data.frame(depel, TRUE, packnames)
- sug.ig <- graph.data.frame(sugel, TRUE, packnames)
- rm(ap, pack2el, depel, sugel, packnames)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement