How to see what package a function comes from? - julia

How can see from which package a function (or some other object) originates? For example the function
take!()

This can be done with the #which macro:
#which take!
Base

Related

How do I call the new function in R when the object is defined in another package?

Suppose I'm creating a package and I'd like to define a function that creates an object where the object is defined in another package.
For example:
get_empty_mtx <- function() return(new("dgCMatrix"))
If I type library(Matrix), this will work, but when I'm making my own package, I like to use :: when referencing things from other packages. I can't do Matrix::new("dgCMatrix") as new is not a function from the Matrix package.
You can use the getClassDef function to get a class definition from a specific package and then call new() on that. For example
new(getClassDef("dgCMatrix", getNamespace("Matrix")))
and new(getClassDef("dgCMatrix", "Matrix")) also seems to work despite the documtation saying where should be an environment.

contains() function not found

I was going through some sample code then I got stuck in this:
for (word in sampleMail) {
if (contains(categoryFeatures, word)) {
categoryFeatures[[word]] = categoryFeatures[[word]] + singleOccurrence
this is not working as it says
Error: could not find function "contains"
as of in the whole code contains() is not defined so i think it's not a user defined function, I searched also but i am not getting a definition of contains in R , there are other ways like %in%. But I want to know is this function contains() is an existing function in R? If yes, there any specific package I have to import to use contains()?
It's a function of dplyr package. You could install dplyr package then the function 'contains' will work. Good luck!

Get namespace of function

I'm developing a package where I wish to add an edit history to an object. The package allows other packages to register functions for editing the object. I'm looking for a way to record the version of the package that registered the function that was used for the edit.
The question is: Given a function how do you get the package from where it was exported? My idea is to investigate its search path, but search() only reports the search path for the current environment and thus not for a function, which is what I need.
Any pointers to other approaches is greatly appreciated.
The context for getting the package is this:
registerFunction <- function(fun) {
package <- getPackage(fun) ## This is what I need
version <- getPackageVersion(package)
register(fun, package, version)
}
You can use getAnywhere For example, if you're looking for the namespace for the stringr function str_locate you can do
getAnywhere("str_locate")$where
# [1] "package:stringr" "namespace:stringr"
This will work as long as stringr is "visible on the search path, registered as an S3 method or in a namespace but not exported."
The result is a named list, and you can see what's available from getAnywhere with names
names(getAnywhere("str_locate"))
# [1] "name" "objs" "where" "visible" "dups"
You can use:
environment(fun=someFunctionName)
It will return the environment of the function passed as parameter, specifying also the namespace, i.e. the package name.

N Choose K function in R not working--what am I missing?

I was trying to acquaint myself with R's nChooseK function but I can't get it to work. I thought it was part of the standard setup (i.e. no additional package needed).
Please help. Here is what I tried:
> nChooseK(10,2)
Error: could not find function "nChooseK"
> n<-4;k<-2
> print(nChooseK(n,k))
Error in print(nChooseK(n, k)) : could not find function "nChooseK"
the last one was an example I saw here: R basic nChooseK
The function is in the R.basic package which is not part of the default R installation. You probably meant to use just choose().
As joran mentions the function nChooseK is a part of R.basic. You can tell this from the example you posted by looking at the top of the page:
You'll notice the "R.basic" in the curley braces which tells you that that function is a part of the "R.basic" package. So to use nChooseK you'll first need to load that package
library(R.basic)
If you don't have R.basic installed yet then you'll need to install it
install.packages("R.basic", contriburl="http://www.braju.com/R/repos/")
library(R.basic)
But as noted the choose function in base R does the same thing
choose(37, 12)
#[1] 1852482996
nChooseK(37, 12)
#[1] 1852482996

Add objects to package namespace

I'd like to push a function inside a package namespace so it can access internal objects of that package (let's use stats package as an example). I've tried using
myfun <- function(x) print(x)
env = loadNamespace("stats")
assign("myfun", myfun , env)
But it is locked. So I've tried to unlock my object
unlockBinding("myfun", env)
Since myfun doesn't exist yet, I can't unlock it.
Any help ?
Along the line of #Hadley's solution, but using the environment of the namespace, how about:
environment(myfun) <- asNamespace('stats')
Why not just set the environment of your new function to the right place?
myfun <- function(x) print(x)
environment(myfun) <- as.environment("package:stats")
You can access internal objects of a package using the triple colon operator :::. Take a look at, for example, as.roman and utils:::.roman2numeric. (Compare this to utils::.roman2numeric.) This could help you avoid having to put your function inside the namespace.
You might also want to look at dont.lockBindings in the mvbutils package, which stops namespaces being locked.

Resources