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

2021-06-24 Thread Greg Minshall
Jake and Deepayan,

both very nice and useful solutions.  thank you.

Greg

__
R-help@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.


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

2021-06-24 Thread Deepayan Sarkar
On Thu, Jun 24, 2021 at 2:10 PM Greg Minshall  wrote:
>
> Duncan,
>
> > Bert gave you an answer that depends on ls().  Whether there's
> > something like "set" that can return "asset" probably depends on
> > your front end, and may be customizable using the facilities described
> > in ?rcompgen.
>
> thanks.  i am just using command line R on linux.  i tried setting
> `rc.setting(fuzzy=TRUE, func=TRUE, ipck=TRUE)`, but
> `data.table::set` still only shows names that start with "set".
> Bert's `ls` answer works, but did you have an idea of how else this
> might be made to work with rcompgen?

That's not currently supported. It would not be difficult to get the
completion mechanism to return these matches; e.g.,

utils:::findMatches("set", ls(getNamespace("data.table")))

You can even experiment a bit with something like (only for normal completions)

complete.partial <- function(.CompletionEnv)
{
text <- .CompletionEnv[["token"]]
comps <- apropos(text)
.CompletionEnv[["comps"]] <- comps
}

rc.options(custom.completer = complete.partial)

The problem is that readline's interface doesn't really allow you to
choose one of these completions easily, and you will need to
explicitly type out at least the prefix.

Another problem with namespace completion in particular is that
readline will first complete to a non-trivial common prefix, if any.
This means that if

data.table::set

gives multiple completions, all starting with "data.table::", readline
will delete the "set" part, and subsequent attempts will just try to
complete "data.table::".

So adding an option to allow apropos-type matches is not difficult,
but given that RStudio has its own completion, and the limited
functionality of readline, not sure it's worth the effort.

Best,
-Deepayan


> cheers, Greg
>
> __
> R-help@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.

__
R-help@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.


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

2021-06-24 Thread Jake Elmstedt
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"   

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

2021-06-24 Thread Greg Minshall
Duncan,

> Bert gave you an answer that depends on ls().  Whether there's
> something like "set" that can return "asset" probably depends on
> your front end, and may be customizable using the facilities described
> in ?rcompgen.

thanks.  i am just using command line R on linux.  i tried setting
`rc.setting(fuzzy=TRUE, func=TRUE, ipck=TRUE)`, but
`data.table::set` still only shows names that start with "set".
Bert's `ls` answer works, but did you have an idea of how else this
might be made to work with rcompgen?

cheers, Greg

__
R-help@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.


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

2021-06-23 Thread Duncan Murdoch

On 23/06/2021 2:51 p.m., Jeff Newmiller wrote:

RStudio seems to have done this. If you have it, try typing

ggplot2::line

and the popup will suggest (among other options) geom_line.


Yes, though it may not be quite right.  Back in May when I typed

 library(rgl

their autocompletion gave me

 library(Rglpk)

(See https://github.com/rstudio/rstudio/issues/9293).  Today that's not 
happening, so maybe it's been fixed, or is only intermittent.


Duncan Murdoch





On June 23, 2021 10:10:07 AM PDT, Duncan Murdoch  
wrote:

On 23/06/2021 8:37 a.m., Greg Minshall wrote:

hi.

at the R prompt, i often hit, e.g., "data.table::", 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" 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" that can return "asset" probably depends on your front
end, and may be customizable using the facilities described in
?rcompgen.

Duncan Murdoch

__
R-help@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.




__
R-help@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.


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

2021-06-23 Thread Jeff Newmiller
RStudio seems to have done this. If you have it, try typing

ggplot2::line

and the popup will suggest (among other options) geom_line.

On June 23, 2021 10:10:07 AM PDT, Duncan Murdoch  
wrote:
>On 23/06/2021 8:37 a.m., Greg Minshall wrote:
>> hi.
>> 
>> at the R prompt, i often hit, e.g., "data.table::", 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" 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" that can return "asset" probably depends on your front 
>end, and may be customizable using the facilities described in
>?rcompgen.
>
>Duncan Murdoch
>
>__
>R-help@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.

-- 
Sent from my phone. Please excuse my brevity.

__
R-help@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.


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

2021-06-23 Thread Duncan Murdoch

On 23/06/2021 8:37 a.m., Greg Minshall wrote:

hi.

at the R prompt, i often hit, e.g., "data.table::", 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" 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" that can return "asset" probably depends on your front 
end, and may be customizable using the facilities described in ?rcompgen.


Duncan Murdoch

__
R-help@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.


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

2021-06-23 Thread Greg Minshall
Bert,

> ?ls  and note the "pattern" argument.
...
> ls("package:base", pat =".*set.*")

awesome -- thanks!

cheers, Greg

__
R-help@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.


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

2021-06-23 Thread Bert Gunter
?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  wrote:

> hi.
>
> at the R prompt, i often hit, e.g., "data.table::", 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" 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@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]]

__
R-help@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.


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

2021-06-23 Thread Greg Minshall
hi.

at the R prompt, i often hit, e.g., "data.table::", 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" 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@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.