@@ -13,7 +13,7 @@ get_base_req <- function() {
1313 .pkg_env $ base_req
1414}
1515
16- make_request <- function (path , token = NULL , method = " GET" ) {
16+ make_request <- function (path , token = NULL , method = " GET" , file = NULL ) {
1717 rlang :: check_installed(" httr2" )
1818 req <- httr2 :: req_url_path(get_base_req(), path )
1919 req <- httr2 :: req_method(req , method )
@@ -24,7 +24,7 @@ make_request <- function(path, token = NULL, method = "GET") {
2424 req <- httr2 :: req_headers(req , `WWW-Authenticate` = token )
2525 }
2626
27- resp <- httr2 :: req_perform(req )
27+ resp <- httr2 :: req_perform(req , path = file )
2828
2929 if (httr2 :: resp_status(resp ) != 200 ) {
3030 stop(" Failed to download file. Status: " , httr2 :: resp_status(resp ))
@@ -40,16 +40,17 @@ resolve_name <- function(x) {
4040 x <- sub(" //" , " /" , x )
4141
4242 if (grepl(" /" , x )) {
43- x_split <- strsplit(x , " /" , fixed = TRUE )[[1 ]]
44- if (length(x_split ) > 2 ) {
43+ res_names <- strsplit(x , " /" , fixed = TRUE )[[1 ]]
44+ bad_names_format <- (length(res_names ) > 2 )
45+ if (bad_names_format ) {
4546 cli :: cli_abort(
46- " {.arg name} has {length(x_split)} components instead of 2 ."
47+ " {.arg name} is not correctly formatted ."
4748 )
4849 }
49- return (x_split )
5050 } else {
51- c(x , x )
51+ res_names <- c(x , x )
5252 }
53+ rlang :: set_names(res_names , c(" collection" , " network" ))
5354}
5455
5556download_file <- function (zip_url , token = NULL , file , size_limit ) {
@@ -62,8 +63,7 @@ download_file <- function(zip_url, token = NULL, file, size_limit) {
6263 " i" = " To download the file, set {.arg size_limit} to a value greater than {gb_size}"
6364 ))
6465 }
65- resp <- make_request(zip_url , token , method = " GET" )
66- writeBin(httr2 :: resp_body_raw(resp ), file )
66+ make_request(zip_url , token , method = " GET" , file = file )
6767 invisible (NULL )
6868}
6969
@@ -109,17 +109,23 @@ download_file <- function(zip_url, token = NULL, file, size_limit) {
109109ns_metadata <- function (name , collection = FALSE ) {
110110 rlang :: check_installed(" cli" )
111111 net_ident <- resolve_name(name )
112- path <- sprintf(" api/net/%s" , net_ident [[1 ]])
113- collection_url <- sprintf(" https://networks.skewed.de/net/%s" , net_ident [[1 ]])
112+ path <- sprintf(" api/net/%s" , net_ident [[" collection" ]])
113+ collection_url <- sprintf(
114+ " https://networks.skewed.de/net/%s" ,
115+ net_ident [[" collection" ]]
116+ )
114117 resp <- make_request(path )
115118 raw <- httr2 :: resp_body_json(resp )
116119 class(raw ) <- c(" ns_meta" , class(raw ))
117120 raw [[" is_collection" ]] <- collection
118- raw [[" collection_name" ]] <- net_ident [[1 ]]
121+ raw [[" collection_name" ]] <- net_ident [[" collection " ]]
119122 if (collection ) {
120123 return (raw )
121- } else if (
122- net_ident [[1 ]] == net_ident [[2 ]] &&
124+ }
125+
126+ # Check if collection equals network and multiple nets exist
127+ if (
128+ net_ident [[" collection" ]] == net_ident [[" network" ]] &&
123129 length(unlist(raw $ nets )) > 1 &&
124130 ! collection
125131 ) {
@@ -129,22 +135,27 @@ ns_metadata <- function(name, collection = FALSE) {
129135 " i" = " see {.url {collection_url}}"
130136 )
131137 )
132- } else if (net_ident [[1 ]] == net_ident [[2 ]]) {
138+ }
139+
140+ # If collection equals network
141+ if (net_ident [[" collection" ]] == net_ident [[" network" ]]) {
133142 return (raw )
134- } else {
135- idx <- which(unlist(raw [[" nets" ]]) == net_ident [[2 ]])
136- if (length(idx ) == 0 ) {
137- cli :: cli_abort(
138- c(
139- " {net_ident[[2]]} is not part of the collection {net_ident[[1]]}." ,
140- " i" = " see {.url {collection_url}}"
141- )
143+ }
144+
145+ # Find matching network
146+ idx <- which(unlist(raw [[" nets" ]]) == net_ident [[" network" ]])
147+ if (length(idx ) == 0 ) {
148+ cli :: cli_abort(
149+ c(
150+ " {net_ident[[2]]} is not part of the collection {net_ident[[1]]}." ,
151+ " i" = " see {.url {collection_url}}"
142152 )
143- }
144- raw [[" analyses" ]] <- raw [[" analyses" ]][[net_ident [[2 ]]]]
145- raw [[" nets" ]] <- raw [[" nets" ]][idx ]
146- raw
153+ )
147154 }
155+
156+ raw [[" analyses" ]] <- raw [[" analyses" ]][[net_ident [[" network" ]]]]
157+ raw [[" nets" ]] <- raw [[" nets" ]][idx ]
158+ raw
148159}
149160
150161# ' @rdname netzschleuder
@@ -162,18 +173,22 @@ ns_df <- function(name, token = NULL, size_limit = 1) {
162173 ))
163174 }
164175 meta <- name
165- net_ident <- c(meta [[" collection_name" ]], meta [[" nets" ]])
176+ net_ident <- c(
177+ collection = meta [[" collection_name" ]],
178+ network = meta [[" nets" ]]
179+ )
166180 } else {
167181 cli :: cli_abort(" {.arg name} must be a string or a `ns_meta` object." )
168182 }
169183
170184 zip_url <- sprintf(
171185 " net/%s/files/%s.csv.zip" ,
172- net_ident [[1 ]],
173- net_ident [[2 ]]
186+ net_ident [[" collection " ]],
187+ net_ident [[" network " ]]
174188 )
175189
176190 temp <- tempfile(fileext = " zip" )
191+ on.exit(unlink(temp ))
177192 download_file(zip_url , token = token , file = temp , size_limit = size_limit )
178193
179194 zip_contents <- utils :: unzip(temp , list = TRUE )
@@ -182,7 +197,9 @@ ns_df <- function(name, token = NULL, size_limit = 1) {
182197 node_file_name <- grep(" node" , zip_contents $ Name , value = TRUE )
183198 gprops_file_name <- grep(" gprops" , zip_contents $ Name , value = TRUE )
184199
185- edges_df_raw <- utils :: read.csv(unz(temp , edge_file_name ))
200+ con_edge <- unz(temp , edge_file_name )
201+ on.exit(close(con_edge ))
202+ edges_df_raw <- utils :: read.csv(con_edge )
186203 edges_df <- suppressWarnings(minty :: type_convert(edges_df_raw ))
187204 source_loc <- grep(" source" , names(edges_df ))
188205 target_loc <- grep(" target" , names(edges_df ))
@@ -192,10 +209,13 @@ ns_df <- function(name, token = NULL, size_limit = 1) {
192209 edges_df [[" from" ]] <- edges_df [[" from" ]] + 1L
193210 edges_df [[" to" ]] <- edges_df [[" to" ]] + 1L
194211
195- nodes_df_raw <- utils :: read.csv(unz(temp , node_file_name ))
212+ con_nodes <- unz(temp , node_file_name )
213+ on.exit(close(con_nodes ))
214+ nodes_df_raw <- utils :: read.csv(con_nodes )
215+
196216 # suppress warning if no character columns found
197217 nodes_df <- suppressWarnings(minty :: type_convert(nodes_df_raw ))
198- names(nodes_df )[1 ] <- " id"
218+ names(nodes_df )[[ 1 ] ] <- " id"
199219
200220 # netzschleuder uses 0-indexing, igraph uses 1-indexing
201221 nodes_df [[" id" ]] <- nodes_df [[" id" ]] + 1L
@@ -210,9 +230,9 @@ ns_df <- function(name, token = NULL, size_limit = 1) {
210230 nodes_df [[" y" ]] <- mat [2 , ]
211231 }
212232
213- gprops_df <- readLines( unz(temp , gprops_file_name ) )
214-
215- on.exit(unlink( temp ) )
233+ con_gprops <- unz(temp , gprops_file_name )
234+ on.exit(close( con_gprops ))
235+ gprops_df <- readLines( con_gprops )
216236
217237 list (nodes = nodes_df , edges = edges_df , gprops = gprops_df , meta = meta )
218238}
0 commit comments