Add objects to package namespace
Along the line of @Hadley's solution, but using the environment of the namespace, how about:
environment(myfun) <- asNamespace('stats')
Change variable in package namespace
The problem seems to have been with the format of the assign statements. Instead of
assign(".myvar", "bar", pos = "package:mypackage")
I used,
assign(".myvar", "bar", pos = asNamespace("mypackage"))
and this resolved the issue.
Will python add name in packages namespace with from * import *?
Yes, the package.__init__
module will have access to the name module
:
$ mkdir package
$ touch package/__init__.py
$ touch package/module.py
$ python
Python 2.7.5 (default, Oct 28 2013, 20:45:48)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from package import module
>>> import sys
>>> sys.modules['package']
<module 'package' from 'package/__init__.py'>
>>> dir(sys.modules['package'])
['__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', 'module']
>>> sys.modules['package'].module
<module 'package.module' from 'package/module.py'>
The second paragraph you refer to talks about what names are bound by a from foo import bar
import statement. bar
is bound, foo
is not. Or, in terms of the above demonstration:
>>> module
<module 'package.module' from 'package/module.pyc'>
>>> package
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'package' is not defined
The from package import module
statement added a module
name to the namespace, but package
was not bound.
R: How do I add an extra function to a package?
There are two parts to this answer - first a generic answer to your question, and 2nd a specific answer for the particular function that you reference, in which the problem is something slightly different.
1) generic solution to accessing internal functions when you edit a package function
You should already have access to the package namespace, since you loaded it, so it is only the unexported functions that will give you issues.
I usually just prepend the package name with the :::
operator to the non exported functions. I.e., find every instance of a call to some_internal_function()
, and replace it with PackageName:::some_internal_function()
. If there are several different internal functions called within the function you are editing, you may need to do this a few times for each of the offending function calls.
The help page for :::
does contain these warnings
Beware -- use ':::' at your own risk!
and
It is typically a design mistake to use ::: in your code since the
corresponding object has probably been kept internal for a good
reason. Consider contacting the package maintainer if you feel the
need to access the object for anything but mere inspection.
But for what you are doing, in terms of temporarily hacking another function from the same package for your own use, these warnings should be safe to ignore (at you own risk, of course - as it says in the manual)
2) In the case of blscrapeR ::bls_map_county()
The offending line in this case is
ggplot2::ggplot() + geom_map(...
in which the package writers have specified the ggplot2 namespace for ggplot()
, but forgotten to do so for geom_map()
which is also part of ggplot2 (and not an internal function in blscrapeR ).
In this case, just load ggplot2, and you should be good to go.
You may also consider contacting the package maintainer to inform them of this error.
How to distinguish package namespace environment from other environment objects
isNamespace
?
isNamespace(y)
# [1] TRUE
isNamespace(x)
# [1] FALSE
And, for future reference, apropos
is often helpful when you've got a question like this.
apropos("namespace")
# [1] "..getNamespace" ".BaseNamespaceEnv" ".getNamespace"
# [4] ".methodsNamespace" "asNamespace" "assignInMyNamespace"
# [7] "assignInNamespace" "attachNamespace" "fixInNamespace"
# [10] "getFromNamespace" "getNamespace" "getNamespaceExports"
# [13] "getNamespaceImports" "getNamespaceInfo" "getNamespaceName"
# [16] "getNamespaceUsers" "getNamespaceVersion" "isBaseNamespace"
# [19] "isNamespace" "loadedNamespaces" "loadingNamespaceInfo"
# [22] "loadNamespace" "namespaceExport" "namespaceImport"
# [25] "namespaceImportClasses" "namespaceImportFrom" "namespaceImportMethods"
# [28] "packageHasNamespace" "parseNamespaceFile" "requireNamespace"
# [31] "setNamespaceInfo" "unloadNamespace"
Show names of everything in a package
ls("package:foreach", all.names=TRUE)
only shows what's attached to the search path, which only includes the objects exported from the namespace. Use ls
on the results of getNamespace
instead:
ls(getNamespace("foreach"), all.names=TRUE)
Related Topics
What Leads the First Element of a Printed List to Be Enclosed with Backticks in R V3.5.1
Data.Table and Parallel Computing
How to Change the Color Value of Just One Value in Ggplot2's Scale_Fill_Brewer
What Is the Meaning of the Dollar Sign "$" in R Function()
Making a Stacked Area Plot Using Ggplot2
Setting Document Title in Rmarkdown from Parameters
How to Increase the Number of Columns Using R in Linux
How to Add Multiple Columns to a Data.Frame in One Go
Seeking Workaround for Gtable_Add_Grob Code Broken by Ggplot 2.2.0
Create Columns from Factors and Count
How to Get Coefficients and Their Confidence Intervals in Mixed Effects Models
Same Function Over Multiple Data Frames in R
There Is Pmin and Pmax Each Taking Na.Rm, Why No Psum
R - Converting Date and Time Fields to Posixct with Hhmmss Format
Extract Elements Common in All Column Groups