-
Notifications
You must be signed in to change notification settings - Fork 18
feat: New ns_graph() and lower-level ns_df() and ns_metadata() to download from [netzschleuder](https://networks.skewed.de)
#23
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
25f0193
67ec488
5c5895d
233b9e0
e521a71
4b07fc2
33ae5ed
db8b0db
51046b9
d8b1f77
bc6a34a
d7a0b06
ca89264
a7c2ef1
4b473a1
e60885d
6e45a5d
dd495a4
7a8004c
e29f90f
bcd1b1a
4431e4b
a52dec8
10b25c4
4485280
02fb9db
0c717fc
1974d6a
29412cf
0ad558d
3cd0496
eb12a3a
c9ef882
94bab69
18f87d1
2a6d9f1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,8 @@ | ||
| # Generated by roxygen2: do not edit by hand | ||
|
|
||
| export(graph_from_netzschleuder) | ||
| export(lesmis_gml) | ||
| export(lesmis_graphml) | ||
| export(lesmis_pajek) | ||
| export(read_from_netzschleuder) | ||
| importFrom(igraph,vcount) |
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1,80 @@ | ||||||
| #' Download a graph from the Netzschleuder data catalogue | ||||||
| #' Netzschleuder (<https://networks.skewed.de/>) is a large online repository for | ||||||
| #' network datasets with the aim of aiding scientific research. | ||||||
| #' @param name character. name of the network dataset. | ||||||
| #' @param net character. If the dataset contains several networks this is the network name. | ||||||
| #' @return a named list containing an edge list and node attribute data frame and some metadata | ||||||
| #' @export | ||||||
| read_from_netzschleuder <- function(name, net = NULL) { | ||||||
| if (is.null(net)) { | ||||||
| net <- name | ||||||
| } | ||||||
| zip_url <- paste0( | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| "https://networks.skewed.de/net/", | ||||||
| name, | ||||||
| "/files/", | ||||||
| net, | ||||||
| ".csv.zip" | ||||||
| ) | ||||||
|
|
||||||
| temp <- tempfile() | ||||||
| utils::download.file(zip_url, temp, quiet = TRUE) #TODO: add better error handling | ||||||
| zip_contents <- utils::unzip(temp, list = TRUE) | ||||||
schochastics marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| edge_file_name <- zip_contents$Name[grepl("edge", zip_contents$Name)] | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| node_file_name <- zip_contents$Name[grepl("node", zip_contents$Name)] | ||||||
| meta_file_name <- zip_contents$Name[grepl("gprops", zip_contents$Name)] | ||||||
|
|
||||||
| edges_df <- utils::read.csv(unz(temp, edge_file_name)) + 1 | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| names(edges_df)[c(1, 2)] <- c("from", "to") | ||||||
|
|
||||||
| nodes_df <- utils::read.csv(unz(temp, node_file_name)) | ||||||
| names(nodes_df)[1] <- "id" | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm leaning towards implementing lintr just for this, here and elsewhere.
Suggested change
|
||||||
| nodes_df$id <- nodes_df$id + 1 | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| if ("X_pos" %in% names(nodes_df)) { | ||||||
| pos_array <- gsub("array\\(\\[|\\]|\\)", "", nodes_df[["X_pos"]]) | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| split_coords <- strsplit(pos_array, ",") | ||||||
|
|
||||||
| x_vals <- sapply(split_coords, function(x) as.numeric(trimws(x[1]))) | ||||||
| y_vals <- sapply(split_coords, function(x) as.numeric(trimws(x[2]))) | ||||||
|
|
||||||
| nodes_df[["X_pos"]] <- NULL | ||||||
| nodes_df$x <- x_vals | ||||||
| nodes_df$y <- y_vals | ||||||
| } | ||||||
|
|
||||||
| meta_df <- utils::read.csv(unz(temp, meta_file_name)) | ||||||
| on.exit(unlink(temp)) | ||||||
schochastics marked this conversation as resolved.
Show resolved
Hide resolved
schochastics marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
|
||||||
| list(nodes = nodes_df, edges = edges_df, meta = meta_df) | ||||||
| } | ||||||
|
|
||||||
| #' Create a graph from the Netzschleuder data catalogue | ||||||
| #' | ||||||
| #' Netzschleuder (<https://networks.skewed.de/>) is a large online repository for | ||||||
| #' network datasets with the aim of aiding scientific research. | ||||||
| #' @param name character. name of the network dataset. | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| #' @param net character. If the dataset contains several networks this is the network name. | ||||||
| #' @param directed logical. Whether a directed graph is constructed. | ||||||
| #' @param bipartite logical. Whether a bipartite graph is constructed. | ||||||
| #' @return a new graph object. | ||||||
| #' @export | ||||||
| graph_from_netzschleuder <- function( | ||||||
| name, | ||||||
| net = NULL, | ||||||
| directed = FALSE, | ||||||
| bipartite = FALSE | ||||||
| ) { | ||||||
| graph_data <- read_from_netzschleuder(name, net) | ||||||
| g <- igraph::graph_from_data_frame( | ||||||
| graph_data$edges, | ||||||
| directed = directed, | ||||||
| vertices = graph_data$nodes | ||||||
| ) | ||||||
| if (bipartite) { | ||||||
schochastics marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| types <- rep(FALSE, igraph::vcount(g)) | ||||||
| types[graph_data$nodes$id %in% graph_data$edges[, 1]] <- TRUE | ||||||
schochastics marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||
| g <- igraph::set_vertex_attr(g, "type", value = types) | ||||||
| } | ||||||
| g | ||||||
schochastics marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
| } | ||||||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Uh oh!
There was an error while loading. Please reload this page.