Skip to contents

# Miscellaneous Package Related Functions

Source: `R/pkgs.R`
`pkgs.Rd`

The `pkgs %installed in% lib.loc` operator checks if one or more given packages (`pkgs`) exist in the given library paths (`lib.loc`), without loading the packages.
The syntax of this operator forces the user to make it syntactically explicit where to look for installed R-packages.
As `pkgs %installed in% lib.loc` does not even load a package, the user can safely use it without fearing any unwanted side-effects.
The `pkg_get_deps()` function gets the direct dependencies of a package from the Description file. It works on non-CRAN packages also.
The `pkg_get_deps_minimal()` function is the same as `pkg_get_deps()`, except with `base, recom, rstudioapi, shared_tidy` all set to `FALSE`, and the default value for `deps_type` is c("Depends", "Imports").
The `pkg_lsf()` function gets a list of exported functions/operators from a package.
One handy use for this function is to, for example, globally attach all infix operators from a package using `library`, like so:

    y <- pkg_lsf("packagename", type = "inops")
    library(packagename, include.only = y)

## Usage

    pkgs %installed in% lib.loc

    pkg_get_deps(
      package,
      lib.loc = .libPaths(),
      deps_type = c("LinkingTo", "Depends", "Imports"),
      base = FALSE,
      recom = TRUE,
      rstudioapi = TRUE,
      shared_tidy = TRUE
    )

    pkg_get_deps_minimal(
      package,
      lib.loc = .libPaths(),
      deps_type = c("Depends", "Imports")
    )

    pkg_lsf(package, type, lib.loc = .libPaths())

## Arguments

pkgs

  a character vector with the package name(s).

lib.loc

  character vector specifying library search path (the location of R library trees to search through).
  The `lib.loc` argument would usually be `.libPaths()`.
  See also loadNamespace.

package

  a single string giving the package name.

deps_type

  a character vector, giving the dependency types to be used.
  The order of the character vector given in `deps_type` affects the order of the returned character vector; see Details sections.

base

  logical, indicating whether base/core R should be included (`TRUE`), or not included (`FALSE`).

recom

  logical, indicating whether the pre-installed 'recommended' R-packages should be included (`TRUE`), or not included (`FALSE`).

rstudioapi

  logical, indicating whether the 'rstudioapi' R-package should be included (`TRUE`), or not included (`FALSE`).

shared_tidy

  logical, indicating whether the shared dependencies of the 'tidyverse' should be included (`TRUE`), or not included (`FALSE`).
  Details:
  Some of the (often many) dependencies 'tidyverse' packages have are shared across the majority of the 'tidyverse'.
  The "official" list of shared dependencies in the 'tidyverse' currently is the following:
  'rlang', 'lifecycle', 'cli', 'glue', and 'withr'.

type

  The type of functions to list. Possibilities:

  - `"inops"` or `"operators"`: Only infix operators.

  - `"regfuns"`: Only regular functions (thus excluding infix operators).

  - `"all"`: All functions, both regular functions and infix operators.

## Value

For `pkgs %installed in% lib.loc`:
Returns a named logical vector.
The names give the package names.
The value `TRUE` indicates a package is installed in `lib.loc`.
The value `FALSE` indicates a package is not installed in `lib.loc`.
The value `NA` indicates a package is not actually a separate package, but base/core 'R' (i.e. 'base', 'stats', etc.).
For `pkg_get_deps()` and `pkg_get_deps_minimal()`:
A character vector of direct dependencies, without duplicates.
For `pkg_lsf()`:
Returns a character vector of exported function names in the specified package.

## Details

For `pkg_get_deps()`:
For each string in argument `deps_type`, the package names in the corresponding field of the Description file are extracted, in the order as they appear in that field.
The order given in argument `deps_type` also affects the order of the returned character vector:
For example, `c("LinkingTo", "Depends", "Imports")`,
means the package names are extracted from the fields in the following order:

1. "LinkingTo";

2. "Depends";

3. "Imports".

The unique (thus non-repeating) package names are then returned to the user.

## References

O'Brien J., elegantly extract R-package dependencies of a package not listed on CRAN. Stack Overflow. (1 September 2023). https://stackoverflow.com/questions/30223957/elegantly-extract-r-package-dependencies-of-a-package-not-listed-on-cran

## See also

tinycodet_import

## Examples

    "dplyr" %installed in% .libPaths()
    #> dplyr
    #>  TRUE

    pkg_get_deps_minimal("dplyr")
    #> [1] "generics"   "magrittr"   "pillar"     "R6"         "tibble"
    #> [6] "tidyselect" "vctrs"
    pkgs <- pkg_get_deps("dplyr")
    pkgs %installed in% .libPaths()
    #>        cli   generics       glue  lifecycle   magrittr     pillar         R6
    #>       TRUE       TRUE       TRUE       TRUE       TRUE       TRUE       TRUE
    #>      rlang     tibble tidyselect      vctrs
    #>       TRUE       TRUE       TRUE       TRUE
    pkg_lsf("dplyr", "all")
    #>   [1] "slice_max"             "src"                   "db_desc"
    #>   [4] "group_by_drop_default" "db_commit"             "coalesce"
    #>   [7] "summarize_if"          "mutate_"               "db_create_index"
    #>  [10] "join_by"               "check_dbplyr"          "src_tbls"
    #>  [13] "db_query_rows"         "dense_rank"            "slice_head"
    #>  [16] "tbl"                   "consecutive_id"        "first"
    #>  [19] "arrange"               "dplyr_row_slice"       "sql_semi_join"
    #>  [22] "is.grouped_df"         "summarise_each"        "group_vars"
    #>  [25] "bind_cols"             "last_dplyr_warnings"   "distinct_at"
    #>  [28] "mutate_all"            "relocate"              "compute"
    #>  [31] "mutate_if"             "distinct_"             "group_indices_"
    #>  [34] "eval_tbls2"            "rename_all"            "count"
    #>  [37] "do_"                   "do"                    "cumany"
    #>  [40] "wrap_dbplyr_obj"       "recode_factor"         "same_src"
    #>  [43] "top_frac"              "select_vars"           "desc"
    #>  [46] "db_create_indexes"     "lead"                  "mutate_at"
    #>  [49] "distinct_prepare"      "src_sqlite"            "distinct_if"
    #>  [52] "cur_column"            "semi_join"             "filter_if"
    #>  [55] "sql_translate_env"     "arrange_"              "case_match"
    #>  [58] "sql_set_op"            "eval_tbls"             "arrange_at"
    #>  [61] "add_count"             "dplyr_reconstruct"     "slice_min"
    #>  [64] "new_rowwise_df"        "cross_join"            "select"
    #>  [67] "distinct_all"          "src_local"             "as.tbl"
    #>  [70] "summarise"             "db_drop_table"         "rows_patch"
    #>  [73] "cur_data"              "row_number"            "groups"
    #>  [76] "anti_join"             "tbl_vars"              "cur_group_id"
    #>  [79] "auto_copy"             "sql_subquery"          "group_indices"
    #>  [82] "progress_estimated"    "transmute_all"         "new_grouped_df"
    #>  [85] "add_tally_"            "group_data"            "rows_insert"
    #>  [88] "all_equal"             "group_split"           "tbl_ptype"
    #>  [91] "inner_join"            "db_insert_into"        "db_save_query"
    #>  [94] "id"                    "summarise_all"         "db_begin"
    #>  [97] "left_join"             "summarize_each_"       "dplyr_col_modify"
    #> [100] "compare_tbls2"         "filter"                "union_all"
    #> [103] "summarize_all"         "slice_tail"            "n"
    #> [106] "add_count_"            "summarize_each"        "show_query"
    #> [109] "group_cols"            "summarise_at"          "n_distinct"
    #> [112] "transmute_at"          "rename"                "reframe"
    #> [115] "select_vars_"          "near"                  "pick"
    #> [118] "n_groups"              "across"                "all_vars"
    #> [121] "select_all"            "slice"                 "na_if"
    #> [124] "select_if"             "grouped_df"            "tbl_df"
    #> [127] "tally_"                "is.src"                "db_create_table"
    #> [130] "mutate_each"           "group_keys"            "add_rownames"
    #> [133] "rename_vars"           "select_at"             "is.tbl"
    #> [136] "arrange_all"           "summarize"             "cummean"
    #> [139] "rename_with"           "group_map"             "current_vars"
    #> [142] "with_groups"           "last"                  "rowwise"
    #> [145] "group_size"            "nth"                   "src_postgres"
    #> [148] "dim_desc"              "transmute"             "transmute_if"
    #> [151] "case_when"             "arrange_if"            "order_by"
    #> [154] "with_order"            "group_rows"            "any_vars"
    #> [157] "db_analyze"            "db_data_type"          "top_n"
    #> [160] "add_tally"             "db_query_fields"       "group_trim"
    #> [163] "between"               "select_"               "lag"
    #> [166] "mutate_each_"          "percent_rank"          "distinct"
    #> [169] "group_by_prepare"      "db_explain"            "summarise_each_"
    #> [172] "tbl_nongroup_vars"     "bench_tbls"            "is_grouped_df"
    #> [175] "right_join"            "summarise_if"          "filter_"
    #> [178] "cur_group_rows"        "src_mysql"             "summarize_at"
    #> [181] "rows_delete"           "combine"               "ntile"
    #> [184] "location"              "symdiff"               "sql_escape_string"
    #> [187] "rename_vars_"          "nest_by"               "db_rollback"
    #> [190] "filter_at"             "collapse"              "db_write_table"
    #> [193] "db_has_table"          "ungroup"               "group_by"
    #> [196] "funs"                  "vars"                  "if_all"
    #> [199] "slice_sample"          "recode"                "validate_rowwise_df"
    #> [202] "rename_"               "cur_group"             "group_nest"
    #> [205] "filter_all"            "src_df"                "summarise_"
    #> [208] "group_by_"             "copy_to"               "cur_data_all"
    #> [211] "if_else"               "transmute_"            "funs_"
    #> [214] "summarize_"            "failwith"              "compare_tbls"
    #> [217] "sample_frac"           "group_by_all"          "explain"
    #> [220] "rename_if"             "sample_n"              "group_by_if"
    #> [223] "validate_grouped_df"   "common_by"             "group_walk"
    #> [226] "changes"               "make_tbl"              "rows_upsert"
    #> [229] "cume_dist"             "ident"                 "rows_append"
    #> [232] "rows_update"           "select_var"            "db_list_tables"
    #> [235] "rename_at"             "sql_escape_ident"      "group_by_at"
    #> [238] "pull"                  "mutate"                "nest_join"
    #> [241] "slice_"                "count_"                "group_modify"
    #> [244] "if_any"                "tally"                 "bind_rows"
    #> [247] "c_across"              "cumall"                "collect"
    #> [250] "min_rank"              "sql"                   "sql_select"
    #> [253] "sql_join"              "full_join"