How to find the package of a R function

Load packages

library(tidyverse)

Where does my function reside?

Finding the package of a given R function is some hassle. I am not aware of a quick built-in way in R to find the package of a function.

That’s why I came up with my own function, check it out:

Install package

Speaking of packages of function, that’s the package where this function stays:

library(devtools)
install_github("sebastiansauer/prada")

Example

library(prada)
find_funs("select")
#> # A tibble: 11 x 3
#>    package_name builtin_pckage loaded
#>    <chr>        <lgl>          <lgl> 
#>  1 BDgraph      FALSE          FALSE 
#>  2 dplyr        FALSE          TRUE  
#>  3 jmvcore      FALSE          FALSE 
#>  4 jqr          FALSE          FALSE 
#>  5 MASS         TRUE           FALSE 
#>  6 plotly       FALSE          FALSE 
#>  7 raster       FALSE          FALSE 
#>  8 rstatix      FALSE          FALSE 
#>  9 tidygraph    FALSE          FALSE 
#> 10 tidylog      FALSE          FALSE 
#> 11 VGAM         FALSE          FALSE
find_funs("tidy")
#> # A tibble: 14 x 3
#>    package_name  builtin_pckage loaded
#>    <chr>         <lgl>          <lgl> 
#>  1 broom         FALSE          FALSE 
#>  2 broom.mixed   FALSE          FALSE 
#>  3 broomExtra    FALSE          FALSE 
#>  4 geepack       FALSE          FALSE 
#>  5 generics      FALSE          FALSE 
#>  6 huxtable      FALSE          FALSE 
#>  7 parsnip       FALSE          FALSE 
#>  8 recipes       FALSE          FALSE 
#>  9 rsample       FALSE          FALSE 
#> 10 rstatix       FALSE          FALSE 
#> 11 tidyposterior FALSE          FALSE 
#> 12 tidypredict   FALSE          FALSE 
#> 13 tidytext      FALSE          FALSE 
#> 14 yardstick     FALSE          FALSE

Note that his may take a while (up to a few seconds in some cases).

Code

find_funs
#> function (f) 
#> {
#>     help_installed <- utils::help.search(paste0("^", f, "$"), 
#>         agrep = FALSE)
#>     pckg_hits <- help_installed$matches[, "Package"]
#>     if (length(pckg_hits) == 0) 
#>         pckg_hits <- "No_results_found"
#>     pckgs <- utils::installed.packages()
#>     pckgs <- tibble::as_tibble(pckgs)
#>     pckgs <- dplyr::filter(pckgs, Priority == "base" | Priority == 
#>         "recommended")
#>     pckgs <- dplyr::select(pckgs, Package)
#>     builtin_pckgs_df <- dplyr::distinct(pckgs)
#>     results <- tibble::data_frame(package_name = pckg_hits, builtin_pckage = match(pckg_hits, 
#>         builtin_pckgs_df$Package, nomatch = 0) > 0, loaded = match(paste("package:", 
#>         pckg_hits, sep = ""), search(), nomatch = 0) > 0)
#>     base::return(results)
#> }
#> <bytecode: 0x7fb6ebd09b90>
#> <environment: namespace:prada>

Enjoy!