Skip to content
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

Remove unnecessary requireNamespace() from internal code #296

Closed
sda030 opened this issue Oct 22, 2022 · 2 comments
Closed

Remove unnecessary requireNamespace() from internal code #296

sda030 opened this issue Oct 22, 2022 · 2 comments
Labels

Comments

@sda030
Copy link

sda030 commented Oct 22, 2022

Hi
requireNamespace() only returns TRUE or FALSE, depending upon whether the package is installed. However, as it is currently used in the .import-methods, they serve no purpose. The imported packages should be loaded into the namespace with the ::. And now it is slightly strange to have the message upon import(), as one of the reasons for using rio is to avoid having to load all the various upstream packages.

>import("testfile.parquet")
Loading required namespace: arrow
>
@chainsawriot
Copy link
Collaborator

@sda030 Thank you for reporting this. I agree with you that this is quite counterproductive to load the entire namespace if a package is installed.

I think the intended purpose of the requireNamespace() should be like this:

.check_install <- function(pkg) {
    if (identical(find.package(pkg, quiet = TRUE), character(0))) {
        stop("Please install this package: ", pkg, call. = FALSE)
    }
    return(invisible(NULL))
}

.import.rio_parquet <- function(file, which = 1, as_data_frame = TRUE, ...) {
    .check_install("arrow")
    ## the as_data_frame = TRUE is kind of problematic too; probably as_data_frame = as_data_frame
    arrow::read_parquet(file = file, as_data_frame = TRUE, ...) 
}

.import.rio_parquet_ori <- function(file, which = 1, as_data_frame = TRUE, ...) {
    requireNamespace("arrow")
    arrow::read_parquet(file = file, as_data_frame = TRUE, ...) 
}

.import.rio_parquet("~/dev/misc/iris.parquet")
#> Error: Please install this package: arrow

.import.rio_parquet_ori("~/dev/misc/iris.parquet")
#> Loading required namespace: arrow
#> Error in loadNamespace(x): there is no package called 'arrow'

Created on 2023-08-29 with reprex v2.0.2

Does the first one look like a reasonable solution to you?

@sda030
Copy link
Author

sda030 commented Aug 29, 2023

Hi @chainsawriot, have you read this article by Christian Hohenfeld regarding checking if a package is installed? Seems most approaches are not perfect. Could test out various approaches and profile it with regard to memory allocation and speed?

And yes, I agree regarding the as_data_frame argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants