-
Notifications
You must be signed in to change notification settings - Fork 76
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
Declutter: Review all else
blocks
#334
Comments
Some dummy else blocks were removed in 44eb387 |
@chainsawriot would you consider this decluttered: .check_file(file, single_only = TRUE)
if (missing(file) && missing(format)) {
stop("Must specify 'file' and/or 'format'")
}
if(!missing(file)){
cfile <- file
f <- find_compress(file)
file <- f$file
compress <- f$compress
format <- ifelse(!missing(format), tolower(format), get_info(file)$input)
} else{
format <- .standardize_format(format)
file <- paste0(as.character(substitute(x)), ".", format)
compress <- NA_character_
} |
@schochastics Sure is! Anything reducing LOCs without loss of readibility is a great improvement! One minor |
@chainsawriot ok thanks. I will keep looking for other |
in
is already handled by |
This line never did what it was supposed to do due to wrong parenthesis Line 64 in 294ba02
|
Lines 139 to 152 in 294ba02
|
@chainsawriot a lot going on there:
.export.rio_rdata <- function(file, x, ...) {
if (is.data.frame(x)) {
return(save(x, file = file, ...))
} else if (is.list(x)) {
e <- as.environment(x)
save(list = names(x), file = file, envir = e, ...)
} else if (is.environment(x)) {
save(list = ls(x), file = file, envir = x, ...)
} else if (is.character(x)) {
save(list = x, file = file, ...)
} else {
stop("'x' must be a data.frame, list, or environment")
}
}
x <- data.frame( a=14 )
.export.rio_rdata("test.RData","x")
load("test.RData")
x
#> [1] "x" Created on 2023-09-11 with reprex v2.0.2 |
hmm but library(rio)
export("iris","iris.RData")
x <- data.frame(a = 14)
export("x","x.RData")
head(import("iris.RData"))
#> Sepal.Length Sepal.Width Petal.Length Petal.Width Species
#> 1 5.1 3.5 1.4 0.2 setosa
#> 2 4.9 3.0 1.4 0.2 setosa
#> 3 4.7 3.2 1.3 0.2 setosa
#> 4 4.6 3.1 1.5 0.2 setosa
#> 5 5.0 3.6 1.4 0.2 setosa
#> 6 5.4 3.9 1.7 0.4 setosa
import("x.RData")
#> [1] "x" Created on 2023-09-11 with reprex v2.0.2 |
@schochastics Yea, the .Rdata support is wacky. #295 |
@chainsawriot I see. Before I mess something up: .export.rio_rdata <- function(file, x, ...) {
if (is.data.frame(x)) {
save(x, file = file, ...)
}
if (is.list(x)) {
e <- as.environment(x)
save(list = names(x), file = file, envir = e, ...)
}
if (is.environment(x)) {
save(list = ls(x), file = file, envir = x, ...)
}
if (is.character(x)) {
save(list = x, file = file, ...)
} else {
stop("'x' must be a data.frame, list, or environment")
}
} |
.export.rio_rdata <- function(file, x, ...) {
if (isFALSE(is.data.frame(x)) && isFALSE(is.list(x)) && isFALSE(is.environment(x)) && isFALSE(is.character(x))) {
stop("'x' must be a data.frame, list, or environment")
}
if (is.data.frame(x)) {
return(save(x, file = file, ...))
}
if (is.list(x)) {
e <- as.environment(x)
return(save(list = names(x), file = file, envir = e, ...))
}
if (is.environment(x)) {
return(save(list = ls(x), file = file, envir = x, ...))
}
return(save(list = x, file = file, ...)) ## characters, but is this doing what it does?
} But I am card-carrying never nester. I abuse return sometimes. |
ok then I was already close :) |
@chainsawriot are you fine with me rewriting Lines 51 to 94 in 81bbf48
to parse_archive <- function(file, which, file_type, ...) {
d <- tempfile()
dir.create(d)
if (file_type == "zip") {
file_list <- utils::unzip(file, list = TRUE)$Name
extract_func <- utils::unzip
} else if (file_type == "tar") {
file_list <- utils::untar(file, list = TRUE)
extract_func <- utils::untar
} else {
stop("Unsupported file_type. Use 'zip' or 'tar'.")
}
if (missing(which)) {
if (length(file_list) > 1) {
warning(sprintf("%s archive contains multiple files. Attempting first file.", file_type))
}
which <- 1
}
if (is.numeric(which)) {
extract_func(file, files = file_list[which], exdir = d)
return(file.path(d, file_list[which]))
} else {
if (substring(which, 1, 1) != "^") {
which2 <- paste0("^", which)
}
extract_func(file, files = file_list[grep(which2, file_list)[1]], exdir = d)
return(file.path(d, which))
}
} and making the relevant adjustments in |
@schochastics I like that. I would stop upfront when Maybe one less if (is.numeric(which)) {
extract_func(file, files = file_list[which], exdir = d)
return(file.path(d, file_list[which]))
}
if (substring(which, 1, 1) != "^") {
which2 <- paste0("^", which)
}
extract_func(file, files = file_list[grep(which2, file_list)[1]], exdir = d)
return(file.path(d, which))
} |
Some of them are not needed, e.g.
rio/R/export.R
Lines 81 to 99 in 067a0ae
The text was updated successfully, but these errors were encountered: