Skip to content

Use selective imports #5970

Description

@MichaelChirico

import(utils)

In general it's preferable (perhaps just IMO) to import selectively vs. blanket import(pkg). Blanket imports risk unintended collisions, especially as packages evolve (add+remove functions), though the risk for the case of default packages (methods/utils/stats) is more borderline.

Activity

  1. Nj221102 commented on Mar 7, 2024

    @Nj221102
    Contributor

    Hi @MichaelChirico i want to work on this issue , can i get assigned and what can be a good approach to know specifically which methods/classes we need to import selectively from the default packages (methods/utils/stats)

  2. ben-schwen commented on Mar 7, 2024

    @ben-schwen
    Member

    One way to do this is to grep for each function in a package. You can list all functions from a package with ls("package:utils"). With this you can create yourself a regex for searching via grep. Then you can grep in the R directory with grep -E -r -n searchString .

    Note that you might have to use the function name without any brackets since we sometimes assign function aliases like in fread with ./fread.R:103: FUN = if (w==2L) untar else unzip

    You might want to use multiple different search string, e.g. for aliases paste0("(", paste0(" ", ls("package:utils"), " ", collapse="|"), ")") and for function calls something like (download.file\([a-Z]+|capture.output\([a-Z]+)

    Michael as regex ninja might come up with a more elegant search string though

  3. Nj221102 commented on Mar 7, 2024

    @Nj221102
    Contributor

    One way to do this is to grep for each function in a package. You can list all functions from a package with ls("package:utils"). With this you can create yourself a regex for searching via grep. Then you can grep in the R directory with grep -E -r -n searchString .

    Note that you might have to use the function name without any brackets since we sometimes assign function aliases like in fread with ./fread.R:103: FUN = if (w==2L) untar else unzip

    You might want to use multiple different search string, e.g. for aliases paste0("(", paste0(" ", ls("package:utils"), " ", collapse="|"), ")") and for function calls something like (download.file\([a-Z]+|capture.output\([a-Z]+)

    Thanks will try using this method :)

  4. MichaelChirico commented on Mar 7, 2024

    @MichaelChirico
    MemberAuthor

    I would say it's easier to just remove the import(utils) line and run R CMD check. it should fail and the output will tell you which symbols are not defined anymore.

  5. Nj221102 commented on Mar 7, 2024

    @Nj221102
    Contributor

    I would say it's easier to just remove the import(utils) line and run R CMD check. it should fail and the output will tell you which symbols are not defined anymore.

    thanks :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions