[R] interactively getting alist of functions for a given package?

Jake Elmstedt j@ke@e|m@tedt @end|ng |rom gm@||@com
Thu Jun 24 16:01:39 CEST 2021


Here's something which is perhaps a bit more sophisticated than what's 
been offered already.

Here's a function which classifies and returns all of the objects in the 
namespace of a package.

There are three advantages to this approach over something like 
ls("package:ggplot2").

1) You don't need to have the library loaded to be able to see the 
objects in the namespace (though it must be installed of course).
2) ls("package:ggplot2") does not distinguish between the types of 
objects, so you'll get things like "diamonds" coming out of it with no 
indication that it's a data object.
3) With the namespace approach you will also get the internal functions 
and S3 methods in the namespace.

     package_objects <- function(pkg, pattern = "", only_functions = 
FALSE) {
       classify <- function(x, ns, all) {
         obj <- get(x, envir = ns)
         if (is.null(obj) || is.atomic(obj) && !is.object(obj)) {
           "values"
         } else {
           mode(obj)
         }
       }
       pkg <- as.character(substitute(pkg))
       ns <- asNamespace(pkg)
       ns_names <- ls(ns, all.names = TRUE, pattern = pattern)
       type <- sapply(ns_names, classify, ns = ns)
       if (only_functions) type[type != "function"] <- NA
       split(ns_names, type)
     }

     str(package_objects(ggplot2)) # All objects
     #> List of 4
     #>  $ environment: chr [1:121] ".__NAMESPACE__." 
".__S3MethodsTable__." "AxisSecondary" "Coord" ...
     #>  $ function   : chr [1:859] "$.ggproto" "$.ggproto_parent" 
"$<-.uneval" "%||%" ...
     #>  $ list       : chr [1:3] ".element_tree" ".store" ".zeroGrob"
     #>  $ values     : chr [1:13] ".__global__" ".all_aesthetics" 
".base_to_ggplot" ".packageName" ...

     str(package_objects(ggplot2, "", TRUE)) # All functions
     #> List of 1
     #>  $ function: chr [1:859] "$.ggproto" "$.ggproto_parent" 
"$<-.uneval" "%||%" ...

     package_objects(ggplot2, "^geom", TRUE) # Function starts with "geom"
     #> $`function`
     #>  [1] "geom_abline"            "geom_area" 
"geom_bar"
     #>  [4] "geom_bin2d"             "geom_blank" 
"geom_boxplot"
     #>  [7] "geom_col"               "geom_column" 
"geom_contour"
     #> [10] "geom_contour_filled"    "geom_count" 
"geom_crossbar"
     #> [13] "geom_curve"             "geom_density" 
"geom_density_2d"
     #> [16] "geom_density_2d_filled" "geom_density2d" 
"geom_density2d_filled"
     #> [19] "geom_dotplot"           "geom_errorbar" 
"geom_errorbarh"
     #> [22] "geom_freqpoly"          "geom_function" 
"geom_hex"
     #> [25] "geom_histogram"         "geom_hline" 
"geom_jitter"
     #> [28] "geom_label"             "geom_line" 
"geom_linerange"
     #> [31] "geom_map"               "geom_path" 
"geom_point"
     #> [34] "geom_pointrange"        "geom_polygon"           "geom_qq" 

     #> [37] "geom_qq_line"           "geom_quantile" 
"geom_raster"
     #> [40] "geom_rect"              "geom_ribbon" 
"geom_rug"
     #> [43] "geom_segment"           "geom_sf" 
"geom_sf_label"
     #> [46] "geom_sf_text"           "geom_smooth" 
"geom_spoke"
     #> [49] "geom_step"              "geom_text" 
"geom_tile"
     #> [52] "geom_violin"            "geom_vline"

     package_objects(ggplot2, "geom", TRUE) # Function contains "geom"
     #> $`function`
     #>  [1] "geom_abline"            "geom_area" 
"geom_bar"
     #>  [4] "geom_bin2d"             "geom_blank" 
"geom_boxplot"
     #>  [7] "geom_col"               "geom_column" 
"geom_contour"
     #> [10] "geom_contour_filled"    "geom_count" 
"geom_crossbar"
     #> [13] "geom_curve"             "geom_density" 
"geom_density_2d"
     #> [16] "geom_density_2d_filled" "geom_density2d" 
"geom_density2d_filled"
     #> [19] "geom_dotplot"           "geom_errorbar" 
"geom_errorbarh"
     #> [22] "geom_freqpoly"          "geom_function" 
"geom_hex"
     #> [25] "geom_histogram"         "geom_hline" 
"geom_jitter"
     #> [28] "geom_label"             "geom_line" 
"geom_linerange"
     #> [31] "geom_map"               "geom_path" 
"geom_point"
     #> [34] "geom_pointrange"        "geom_polygon"           "geom_qq" 

     #> [37] "geom_qq_line"           "geom_quantile" 
"geom_raster"
     #> [40] "geom_rect"              "geom_ribbon" 
"geom_rug"
     #> [43] "geom_segment"           "geom_sf" 
"geom_sf_label"
     #> [46] "geom_sf_text"           "geom_smooth" 
"geom_spoke"
     #> [49] "geom_step"              "geom_text" 
"geom_tile"
     #> [52] "geom_violin"            "geom_vline" 
"guide_geom"
     #> [55] "guide_geom.axis"        "guide_geom.bins" 
"guide_geom.colorbar"
     #> [58] "guide_geom.guide_none"  "guide_geom.legend" 
"guides_geom"
     #> [61] "update_geom_defaults"

     # Has "geom" or "Geom" anywhere in the object name
     package_objects(ggplot2, "[gG]eom")
     #> $environment
     #>  [1] "Geom"                "GeomAbline" 
"GeomAnnotationMap"
     #>  [4] "GeomArea"            "GeomBar"             "GeomBlank" 

     #>  [7] "GeomBoxplot"         "GeomCol"             "GeomContour" 

     #> [10] "GeomContourFilled"   "GeomCrossbar"        "GeomCurve" 

     #> [13] "GeomCustomAnn"       "GeomDensity"         "GeomDensity2d" 

     #> [16] "GeomDensity2dFilled" "GeomDotplot"         "GeomErrorbar" 

     #> [19] "GeomErrorbarh"       "GeomFunction"        "GeomHex" 

     #> [22] "GeomHline"           "GeomLabel"           "GeomLine" 

     #> [25] "GeomLinerange"       "GeomLogticks"        "GeomMap" 

     #> [28] "GeomPath"            "GeomPoint" 
"GeomPointrange"
     #> [31] "GeomPolygon"         "GeomQuantile"        "GeomRaster" 

     #> [34] "GeomRasterAnn"       "GeomRect"            "GeomRibbon" 

     #> [37] "GeomRug"             "GeomSegment"         "GeomSf" 

     #> [40] "GeomSmooth"          "GeomSpoke"           "GeomStep" 

     #> [43] "GeomText"            "GeomTile"            "GeomViolin" 

     #> [46] "GeomVline"
     #>
     #> $`function`
     #>  [1] "geom_abline"            "geom_area" 
"geom_bar"
     #>  [4] "geom_bin2d"             "geom_blank" 
"geom_boxplot"
     #>  [7] "geom_col"               "geom_column" 
"geom_contour"
     #> [10] "geom_contour_filled"    "geom_count" 
"geom_crossbar"
     #> [13] "geom_curve"             "geom_density" 
"geom_density_2d"
     #> [16] "geom_density_2d_filled" "geom_density2d" 
"geom_density2d_filled"
     #> [19] "geom_dotplot"           "geom_errorbar" 
"geom_errorbarh"
     #> [22] "geom_freqpoly"          "geom_function" 
"geom_hex"
     #> [25] "geom_histogram"         "geom_hline" 
"geom_jitter"
     #> [28] "geom_label"             "geom_line" 
"geom_linerange"
     #> [31] "geom_map"               "geom_path" 
"geom_point"
     #> [34] "geom_pointrange"        "geom_polygon"           "geom_qq" 

     #> [37] "geom_qq_line"           "geom_quantile" 
"geom_raster"
     #> [40] "geom_rect"              "geom_ribbon" 
"geom_rug"
     #> [43] "geom_segment"           "geom_sf" 
"geom_sf_label"
     #> [46] "geom_sf_text"           "geom_smooth" 
"geom_spoke"
     #> [49] "geom_step"              "geom_text" 
"geom_tile"
     #> [52] "geom_violin"            "geom_vline" 
"guide_geom"
     #> [55] "guide_geom.axis"        "guide_geom.bins" 
"guide_geom.colorbar"
     #> [58] "guide_geom.guide_none"  "guide_geom.legend" 
"guides_geom"
     #> [61] "update_geom_defaults"

Created on 2021-06-24 by the [reprex 
package](https://reprex.tidyverse.org) (v2.0.0.9000)



More information about the R-help mailing list