Skip to content

Commit 563a830

Browse files
committed
Remove ucimlrepo display in favor of a special print method to appease CRAN.
This simplifies logic by relying upon `print.data.frame()` method with a combination of parameters that allows for the display of: name id
1 parent a5603e7 commit 563a830

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

R/list-available-datasets.R

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -83,39 +83,52 @@ list_available_datasets <- function(filter, search, area) {
8383
# Create and return a table of data
8484
table_of_data <- as.data.frame(do.call(rbind, data))
8585

86-
# Column width for dataset name
87-
maxNameLen <- max(nchar(sapply(data, function(d) d$name))) + 3
88-
maxNameLen <- max(maxNameLen, 15)
86+
# Add attributes to the data frame
87+
attr(table_of_data, 'filter') <- if(!missing(filter)) filter else NULL
88+
attr(table_of_data, 'search') <- if(!missing(search)) search else NULL
89+
attr(table_of_data, 'area') <- if(!missing(area)) area else NULL
90+
91+
class(table_of_data) <- c('ucimlrepo_list_of_available_datasets', 'data.frame')
92+
93+
# Print the table of data
94+
return(table_of_data)
95+
}
96+
97+
#' @export
98+
print.ucimlrepo_list_of_available_datasets <- function(x, ...) {
99+
100+
# Create separate data frame to operate on...
101+
x2 <- x
102+
103+
# Unpack
104+
filter <- attr(x, 'filter')
105+
search <- attr(x, 'search')
106+
area <- attr(x, 'area')
107+
108+
# Nullify
109+
attr(x2, 'filter') <- NULL
110+
attr(x2, 'search') <- NULL
111+
attr(x2, 'area') <- NULL
112+
class(x2) <- 'data.frame'
89113

90114
# Print table title
91-
title <- paste0('The following ', if (!missing(filter)) paste0(filter, ' ') else '', 'datasets are available', if (!missing(search)) paste0(' for search query "', search, '"') else '', ':')
115+
title <- paste0(
116+
'The following ', if (!is.null(filter)) paste0(filter, ' ') else '',
117+
'datasets are available',
118+
if (!is.null(search)) paste0(' for search query "', search, '"') else '', ':'
119+
)
120+
92121
cat(paste(rep('-', nchar(title)), collapse = ''), fill = TRUE)
93122
cat(title, fill = TRUE)
94-
if (!missing(area)) {
123+
if (!is.null(area)) {
95124
cat(paste0('For subject area: ', area), fill = TRUE)
96125
}
97126
cat(paste(rep('-', nchar(title)), collapse = ''), fill = TRUE)
98127

99-
# Print table headers
100-
header_str <- sprintf('%-*s %-6s', maxNameLen, 'Dataset Name', 'ID')
101-
underline_str <- sprintf('%-*s %-6s', maxNameLen, '------------', '--')
102-
if (length(data) > 0 && 'description' %in% names(data$data[[1]])) {
103-
header_str <- paste0(header_str, ' ', sprintf('%-100s', 'Prediction Task'))
104-
underline_str <- paste0(underline_str, ' ', sprintf('%-100s', '---------------'))
105-
}
106-
cat(header_str, fill = TRUE)
107-
cat(underline_str, fill = TRUE)
108-
109-
# Print row for each dataset
110-
for (dataset in data) {
111-
row_str <- sprintf('%-*s %-6d', maxNameLen, dataset$name, dataset$id)
112-
if ('description' %in% names(dataset)) {
113-
row_str <- paste0(row_str, ' ', sprintf('%-100s', dataset$description))
114-
}
115-
cat(row_str, fill = TRUE)
116-
}
117128

118-
cat(fill = TRUE)
129+
# Print the table
130+
print(x2[, c('name', 'id')], row.names = FALSE, right = FALSE)
119131

120-
return(invisible(table_of_data))
132+
cat(fill = TRUE)
133+
invisible(x)
121134
}

0 commit comments

Comments
 (0)