Greg Minshall
2021-Jun-23 12:37 UTC
[R] interactively getting alist of functions for a given package?
hi. at the R prompt, i often hit, e.g., "data.table::<TAB>", to try to find a routine in a give package. however, some packages have a *lot* of functions (i'm looking at *you*, ggplot2...), so if i know the routine name starts with, e.g., "set", i can filter the returned list of routines by typing "data.table::set<TAB>" to get a list of completions. but, what if i know the name *contains*, but doesn't start with, "set"? is there an obvious way to find this? something like the unix-y : ls /bin | grep -i "set" ? cheers, Greg
Bert Gunter
2021-Jun-23 13:46 UTC
[R] interactively getting alist of functions for a given package?
?ls and note the "pattern" argument.
e.g.
ls("package:base", pat =".*set.*")
Bert Gunter
"The trouble with having an open mind is that people keep coming along and
sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )
On Wed, Jun 23, 2021 at 5:38 AM Greg Minshall <minshall at umich.edu>
wrote:
> hi.
>
> at the R prompt, i often hit, e.g., "data.table::<TAB>", to
try to find
> a routine in a give package.
>
> however, some packages have a *lot* of functions (i'm looking at *you*,
> ggplot2...), so if i know the routine name starts with, e.g.,
"set", i
> can filter the returned list of routines by typing
> "data.table::set<TAB>" to get a list of completions.
>
> but, what if i know the name *contains*, but doesn't start with,
"set"?
>
> is there an obvious way to find this? something like the unix-y
> : ls /bin | grep -i "set"
> ?
>
> cheers, Greg
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>
[[alternative HTML version deleted]]
Duncan Murdoch
2021-Jun-23 17:10 UTC
[R] interactively getting alist of functions for a given package?
On 23/06/2021 8:37 a.m., Greg Minshall wrote:> hi. > > at the R prompt, i often hit, e.g., "data.table::<TAB>", to try to find > a routine in a give package. > > however, some packages have a *lot* of functions (i'm looking at *you*, > ggplot2...), so if i know the routine name starts with, e.g., "set", i > can filter the returned list of routines by typing > "data.table::set<TAB>" to get a list of completions. > > but, what if i know the name *contains*, but doesn't start with, "set"? > > is there an obvious way to find this? something like the unix-y > : ls /bin | grep -i "set"Bert gave you an answer that depends on ls(). Whether there's something like "set<TAB>" that can return "asset" probably depends on your front end, and may be customizable using the facilities described in ?rcompgen. Duncan Murdoch
Jake Elmstedt
2021-Jun-24 14:01 UTC
[R] interactively getting alist of functions for a given package?
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)