Skip to content

Commit e5922c6

Browse files
authored
Swap to functional (#6)
* Switch to using base functionals to avoid growing objects * Add note * Name the elements
1 parent 1f1a81f commit e5922c6

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- Improved graceful errors for `fetch_ucirepo()` and `list_available_datasets()`
66
when resources are not found/available. ([#3](https://github.com/coatless-rpkg/ucimlrepo/issues/3),
77
thanks Prof. Ripley!)
8+
- Speed up `fetch_ucirepo()` for large data frames by switching to using base
9+
functionals instead of growing a vector in a loop while sorting variable roles.
10+
([#6](https://github.com/coatless-rpkg/ucimlrepo/pull/6))
811

912
## Bug fixes
1013

R/fetch-ucirepo.R

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -235,19 +235,21 @@ fetch_ucirepo <- function(name, id) {
235235
metadata$variables <- NULL
236236

237237
# Organize variables into IDs, features, or targets
238-
variables_by_role <- list(
239-
ID = list(),
240-
Feature = list(),
241-
Target = list(),
242-
Other = list()
243-
)
244-
for (variable in variables) {
245-
if (!(variable$role %in% names(variables_by_role))) {
246-
stop('Role must be one of "ID", "Feature", "Target", or "Other"')
238+
# First, ensure all roles are valid
239+
if (!all(sapply(variables, function(v) v$role %in% c("ID", "Feature", "Target", "Other")))) {
240+
stop('Role must be one of "ID", "Feature", "Target", or "Other".')
241+
}
242+
243+
# Create the list of variables by role
244+
variables_by_role <- lapply(
245+
c("ID", "Feature", "Target", "Other"),
246+
function(role) {
247+
trimws(sapply(variables[sapply(variables, function(v) v$role == role)], `[[`, "name"))
247248
}
249+
)
248250

249-
variables_by_role[[variable$role]] <- c(variables_by_role[[variable$role]], trimws(variable$name))
250-
}
251+
# Name the list elements
252+
names(variables_by_role) <- c("ID", "Feature", "Target", "Other")
251253

252254
# Extract dataframes for each variable role
253255
ids_df <- if (length(variables_by_role$ID) > 0) df[ , unlist(variables_by_role$ID), drop = FALSE] else NULL

0 commit comments

Comments
 (0)