Skip to content

Commit

Permalink
Work with extractors
Browse files Browse the repository at this point in the history
  • Loading branch information
ikosmidis committed May 21, 2018
1 parent 9fce446 commit e8b04c5
Show file tree
Hide file tree
Showing 9 changed files with 122 additions and 81 deletions.
2 changes: 2 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ S3method(author_with,cranly_network)
S3method(build_dependence_tree,cranly_network)
S3method(build_network,cranly_db)
S3method(depends,cranly_network)
S3method(enhances,cranly_network)
S3method(imports,cranly_network)
S3method(linking_to,cranly_network)
S3method(package_by,cranly_network)
Expand All @@ -26,6 +27,7 @@ export(clean_up_author)
export(clean_up_directives)
export(compute_dependence_tree)
export(depends)
export(enhances)
export(imports)
export(linking_to)
export(package_by)
Expand Down
5 changes: 5 additions & 0 deletions R/cranly-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,11 @@ linking_to <- function(x, package = NULL, exact = FALSE) {
UseMethod("linking_to")
}

#' @rdname package_by
#' @export
enhances <- function(x, package = NULL, exact = FALSE) {
UseMethod("enhances")
}


#' \code{build_network} method for an object
Expand Down
107 changes: 84 additions & 23 deletions R/extractors.R
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ package_by.cranly_network <- function(x, author = NULL, exact = FALSE) {
if (any(is.infinite(author))) {
return(unique(unlist(x$nodes$package)))
}
perspective <- attr(x, "perspective")

if (exact) {
str <- paste(author, collapse = "$|^")
str <- paste0("^", str, "$")
Expand All @@ -24,7 +24,7 @@ package_by.cranly_network <- function(x, author = NULL, exact = FALSE) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}

}
Expand All @@ -38,7 +38,7 @@ package_with.cranly_network <- function(x, name = NULL, exact = FALSE) {
if (any(is.infinite(name))) {
return(unique(unlist(x$nodes$package)))
}
perspective <- attr(x, "perspective")

## Escape .
name <- gsub("\\.", "\\\\.", name)
if (exact) {
Expand All @@ -49,14 +49,15 @@ package_with.cranly_network <- function(x, name = NULL, exact = FALSE) {
str <- paste(name, collapse = "|")
}
package <- unlist(x$nodes$package)
inds <- grep(str, package, ignore.case = !exact, perl = TRUE)

inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))
out <- unique(package[inds])

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}

}
Expand All @@ -70,7 +71,7 @@ author_of.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$author)))
}
perspective <- attr(x, "perspective")

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
Expand All @@ -79,14 +80,15 @@ author_of.cranly_network <- function(x, package = NULL, exact = FALSE) {
else {
str <- paste(package, collapse = "|")
}
inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)

inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))
out <- unique(unlist(x$nodes[inds, "author"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}

Expand All @@ -99,7 +101,7 @@ author_with.cranly_network <- function(x, name = NULL, exact = FALSE) {
if (any(is.infinite(name))) {
return(unique(unlist(x$nodes$author)))
}
perspective <- attr(x, "perspective")

if (exact) {
str <- paste(name, collapse = "$|^")
str <- paste0("^", str, "$")
Expand All @@ -108,27 +110,31 @@ author_with.cranly_network <- function(x, name = NULL, exact = FALSE) {
str <- paste(name, collapse = "|")
}
authors <- unlist(x$nodes$author)
inds <- grep(str, authors, ignore.case = !exact)
inds <- sapply(x$nodes$author, function(z) any(grepl(str, z, ignore.case = !exact)))
out <- unique(authors[inds])

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}

#' @rdname package_by
#' @export
suggests.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (attr(x, "perspective") == "author") {
stop(match.call()[[1]], " is not designed for cranly_network objects with perspective = 'author'")
}

if (is.null(package)) {
return(NULL) # return(unlist(x$nodes$Package))
}
if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$suggests)))
}
perspective <- attr(x, "perspective")

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
Expand All @@ -137,29 +143,34 @@ suggests.cranly_network <- function(x, package = NULL, exact = FALSE) {
else {
str <- paste(package, collapse = "|")
}

inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)
## inds <- sapply(x$nodes$Package, function(x) any(grepl(str, x)))
## inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))
out <- unique(unlist(x$nodes[inds, "suggests"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}


#' @rdname package_by
#' @export
imports.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (attr(x, "perspective") == "author") {
stop(match.call()[[1]], " is not designed for cranly_network objects with perspective = 'author'")
}

if (is.null(package)) {
return(NULL) # return(unlist(x$nodes$Package))
}
if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$imports)))
}
perspective <- attr(x, "perspective")

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
Expand All @@ -168,28 +179,34 @@ imports.cranly_network <- function(x, package = NULL, exact = FALSE) {
else {
str <- paste(package, collapse = "|")
}

inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)
## inds <- sapply(x$nodes$Package, function(x) any(grepl(str, x)))
## inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))

out <- unique(unlist(x$nodes[inds, "imports"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}

#' @rdname package_by
#' @export
depends.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (attr(x, "perspective") == "author") {
stop(match.call()[[1]], " is not designed for cranly_network objects with perspective = 'author'")
}

if (is.null(package)) {
return(NULL) # return(unlist(x$nodes$Package))
}
if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$depends)))
}
perspective <- attr(x, "perspective")

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
Expand All @@ -198,29 +215,35 @@ depends.cranly_network <- function(x, package = NULL, exact = FALSE) {
else {
str <- paste(package, collapse = "|")
}

inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)
## inds <- sapply(x$nodes$Package, function(x) any(grepl(str, x)))
## inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))

out <- unique(unlist(x$nodes[inds, "depends"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}


#' @rdname package_by
#' @export
linking_to.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (attr(x, "perspective") == "author") {
stop(match.call()[[1]], " is not designed for cranly_network objects with perspective = 'author'")
}

if (is.null(package)) {
return(NULL) # return(unlist(x$nodes$Package))
}
if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$linkingto)))
}
perspective <- attr(x, "perspective")

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
Expand All @@ -229,15 +252,53 @@ linking_to.cranly_network <- function(x, package = NULL, exact = FALSE) {
else {
str <- paste(package, collapse = "|")
}

inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)
## inds <- sapply(x$nodes$Package, function(x) any(grepl(str, x)))
## inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))
out <- unique(unlist(x$nodes[inds, "linkingto"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out)
return(out[!is.na(out)])
}
}


#' @rdname package_by
#' @export
enhances.cranly_network <- function(x, package = NULL, exact = FALSE) {
if (attr(x, "perspective") == "author") {
stop(match.call()[[1]], " is not designed for cranly_network objects with perspective = 'author'")
}

if (is.null(package)) {
return(NULL) # return(unlist(x$nodes$Package))
}

if (any(is.infinite(package))) {
return(unique(unlist(x$nodes$enhances)))
}

package <- gsub("\\.", "\\\\.", package)
if (exact) {
str <- paste(package, collapse = "$(?!\\.)|^")
str <- paste0("^", str, "$(?!\\.)")
}
else {
str <- paste(package, collapse = "|")
}

inds <- grep(str, x$nodes$package, ignore.case = !exact, perl = TRUE)
## inds <- sapply(x$nodes$package, function(z) any(grepl(str, z, ignore.case = !exact, perl = TRUE)))
out <- unique(unlist(x$nodes[inds, "enhances"]))

if (all(is.na(out)) | !length(out)) {
return(NULL)
}
else {
return(out[!is.na(out)])
}
}

6 changes: 3 additions & 3 deletions man/as.igraph.cranly_network.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 0 additions & 41 deletions man/cran_sample.Rd

This file was deleted.

Loading

0 comments on commit e8b04c5

Please sign in to comment.