Skip to content

How to link two R packages together

14 messages · xuan zhao, Roberto, R. Michael Weylandt +3 more

#
Hi,
I have built two R packages. One of them (PKG1) needs to use the functions
of the other package (PKG2). 
So I need to link these two packages together, so that the functions of PKG2
can be available to PKG1. And when I load one package using
'library("PKG1")', PKG2 can be loaded at the same.
Any ideas welcome.




--
View this message in context: http://r.789695.n4.nabble.com/How-to-link-two-R-packages-together-tp4638765.html
Sent from the R help mailing list archive at Nabble.com.
#
Isn't this what package dependencies are for?

See the description of the DESCRIPTION file in Writing R Extensions

Michael
On Aug 1, 2012, at 5:27 PM, xuan zhao <xuan.zhao at sentrana.com> wrote:

            
#
On Wed, Aug 1, 2012 at 6:45 PM, Michael Weylandt
<michael.weylandt at gmail.com> wrote:
No. It's what package imports are for (preferably).
As always, the OP should RTFM -- in this case the one to which you
refer on the next line, especially the NAMESPACES section.

-- Bert

  
    
#
On Wed, Aug 1, 2012 at 10:28 PM, Bert Gunter <gunter.berton at gene.com> wrote:
But note that the original question included, "when I load one package
using 'library("PKG1")', PKG2 can be loaded at the same." which
imports does not exactly do. They become available to the package, but
not to the user, so if you really need both packages loaded to the
search path, then you need a dependency, not imports.

Cheers,

Josh

  
    
#
Josh:

You may be right ... but I still do not think so. Note that the post
begins with:

One of them (PKG1) needs to use the functions
This is exactly what imports are for. I believe that, because he/she
failed to RTFM, he/she is not being accurate in his/her use of the
relevant terms. Certainly, to me at least, it is unclear. Moreover, as
I understand it (see the manual) imports are preferred over
dependencies, as I indicated in my reply.

Anyway, in either case, the central advice is that which both Michael
and I gave: RTFM. Perhaps it's a generation gap (I am old), but I
believe there has been a trend for new R users to post here without
making any significant effort to consult the docs. This strikes me as
intellectually lazy and, frankly, inconsiderate to those like yourself
-- or BDR -- who are willing to give up their time and effort to help
those with legitimate questions. You and others may consider this an
overreaction of course.

Sorry for the rant, but it seems relevant to your close parsing of the thread.

-- Cheers,
Bert
On Wed, Aug 1, 2012 at 10:35 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:

  
    
#
Hi All,
Thank you so much for all the help you have provided, I really appreciate it!
The problem is solved, I just added PKG2 to the dependency of PKG1 in the description file, also (I don't know if it's necessary or not), I added "import(PKG2)" to the NAMESPACE file. In addition to that, I install PKG2 under a path R can recognize, namely, belong to ".libPaths()". 
I have tried other ways besides install it under the path R can recognize, like  adding a file ~/.R/check.Environ, whose contents are: 'R_LIBS_SITE=${R_LIBS_SITE-'directoryunderwhichPKG2isinstalled'}', but it still doesn't work. 
I looked through the manual, they just say the file 'check.Environ' should be put under '~/.R', but I am not should what the "~/" should be. Is that my home directory or what? Should that be the host? I am not the host of the server, does that matter?
Thank you so much for the help!
Yours,
Xuan

-----Original Message-----
From: Bert Gunter [mailto:gunter.berton at gene.com] 
Sent: Thursday, August 02, 2012 9:42 AM
To: Joshua Wiley
Cc: Michael Weylandt; r-help at r-project.org; Xuan Zhao
Subject: Re: [R] How to link two R packages together

Josh:

You may be right ... but I still do not think so. Note that the post begins with:

One of them (PKG1) needs to use the functions
This is exactly what imports are for. I believe that, because he/she failed to RTFM, he/she is not being accurate in his/her use of the relevant terms. Certainly, to me at least, it is unclear. Moreover, as I understand it (see the manual) imports are preferred over dependencies, as I indicated in my reply.

Anyway, in either case, the central advice is that which both Michael and I gave: RTFM. Perhaps it's a generation gap (I am old), but I believe there has been a trend for new R users to post here without making any significant effort to consult the docs. This strikes me as intellectually lazy and, frankly, inconsiderate to those like yourself
-- or BDR -- who are willing to give up their time and effort to help those with legitimate questions. You and others may consider this an overreaction of course.

Sorry for the rant, but it seems relevant to your close parsing of the thread.

-- Cheers,
Bert
On Wed, Aug 1, 2012 at 10:35 PM, Joshua Wiley <jwiley.psych at gmail.com> wrote:

  
    
#
Hi Bert,

Likewise, you may well be right.  In fact, if I were going to assign
probabilities of being correct given the original post, I would give
you about .7 and me about .3.  I commented for completeness of
discussion.  I definitely see your point, and thank you for a
thoughtful reply to my post. I agree that many questions seem to me
that a quick read of the docs would solve them. That brings up an
interesting point, though.  In a population of 100, if 1-2 people have
a preventable illness, we might be inclined to blame them; what if
40-50 people have the preventable illness?  It seems to me lack of
reading the docs and following the posting guide are at epidemic
proportions. I do not know if it is a generation gap, true laziness,
lack of knowledge that the docs and posting guide _should_ be read
first, or ...

This is slightly OT, but I remember one time when I was working with
someone in person (not in R), and there was a question about how to
customize some graphic.  I immediately went to open up the help files
but the person I was working with said, "I don't want look at the
documentation" and instead starting searching through a cookbook of
different graphs to try to find the answer.  I was shocked because it
seemed like an innate visceral response to the official docs. Perhaps
our useRs are afflicted with this same condition.

Cheers,

Josh
On Thu, Aug 2, 2012 at 6:42 AM, Bert Gunter <gunter.berton at gene.com> wrote:

  
    
#
Hi Xuan,

I would expect ~/R/ to be the R_HOME directory, though perhaps I am
wrong.  Also, you only need a depends or an imports, not both.  At a
simplified level, the difference is that importing PKG2 in PKG1 makes
the (exported) functions in PKG2 available to PKG1. Depends also makes
the (exported) functions in PKG2 available to PKG1, but it _also_
makes them available to the user.  This can be good or bad, with the
recommendation now being not to. The reason it can be good is if you
want your users to have access to functions in both packages without
explicitly loading both. However, let's say that function foo() is
defined in PKG2 and in PKG3. PKG1 does not know about PKG3, and the
user had PKG3 loaded prior to loading PKG1. Then when PKG1 loads, PKG2
also loads, and because all the exported functions from PKG2 are
included, PKG2::foo() masks PKG3::foo(), which can be annoying for
users if your package adds unnecessary functions to the search path
potentially masking the versions they wanted to use.

That was a rather convoluted explanation, but hopefully it is somewhat clear.

Sincerely,

Josh
On Thu, Aug 2, 2012 at 7:02 AM, Xuan Zhao <xuan.zhao at sentrana.com> wrote:

  
    
#
I will reply inline here, but I believe any further questions about
package development should be posted to the R-devel list rather than
here (please someone correct me if this is wrong).

-- Bert
On Thu, Aug 2, 2012 at 7:02 AM, Xuan Zhao <xuan.zhao at sentrana.com> wrote:
"The ?Imports? field lists packages whose namespaces are imported from
(as specified in the
?NAMESPACE? file) but which do not need to be attached."

"Packages declared in the ?Depends? field should not
also be in the ?Imports? field."  [in the DESCRIPTION file]"

So the answer is no -- you should not do this, although as you
apparently did not specify the imports in the DESCRIPTION file, it
should have no effect.

 I added "import(PKG2)" to the NAMESPACE file.
So remove this.

 In addition to that, I install PKG2 under a path R can recognize,
namely, belong to ".libPaths()".

Of course. See ?library ?INSTALL  ?install.packages (in R).  This is
the 2nd paragraph of the installation manual, in which ALL your
questions are answered.

-- Bert

  
    
#
Hi Josh,
Thank you so much for your detailed explanation. It's very clear, and now I can understand the difference between depends and imports.
Thank you so much for your help!! I really really appreciate it!
Yours,
Xuan

-----Original Message-----
From: Joshua Wiley [mailto:jwiley.psych at gmail.com] 
Sent: Thursday, August 02, 2012 10:40 AM
To: Xuan Zhao
Cc: r-help at r-project.org
Subject: Re: [R] How to link two R packages together

Hi Xuan,

I would expect ~/R/ to be the R_HOME directory, though perhaps I am wrong.  Also, you only need a depends or an imports, not both.  At a simplified level, the difference is that importing PKG2 in PKG1 makes the (exported) functions in PKG2 available to PKG1. Depends also makes the (exported) functions in PKG2 available to PKG1, but it _also_ makes them available to the user.  This can be good or bad, with the recommendation now being not to. The reason it can be good is if you want your users to have access to functions in both packages without explicitly loading both. However, let's say that function foo() is defined in PKG2 and in PKG3. PKG1 does not know about PKG3, and the user had PKG3 loaded prior to loading PKG1. Then when PKG1 loads, PKG2 also loads, and because all the exported functions from PKG2 are included, PKG2::foo() masks PKG3::foo(), which can be annoying for users if your package adds unnecessary functions to the search path potentially masking the versions they wanted to use.

That was a rather convoluted explanation, but hopefully it is somewhat clear.

Sincerely,

Josh
On Thu, Aug 2, 2012 at 7:02 AM, Xuan Zhao <xuan.zhao at sentrana.com> wrote:
--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/
#
On Thu, Aug 2, 2012 at 9:02 AM, Xuan Zhao <xuan.zhao at sentrana.com> wrote:
By which you mean .... ?
"~" is a standard UNIX (POSIX?) shorthand for the user's home
directory. On my Mac it is "/Users/mweylandt" and I imagine you'd get
something similar on all Linux and BSD systems -- one easy way to see
where it is: go to your terminal and type "cd && pwd" which will print
it out. No idea what the parallel on Windows is: possibly something
under C:/Documents and Settings but it's been so long since I've had
the misfortune of doing serious work on Windows, I can't remember.

Incidentally, is there a reason you can't put your package in the
"regular" place?

Finally, see the manuals that Bert, Josh, and I have pointed you to:
they are much more authoritative on these matters than any of us.

Best,
Michael
#
Inline.
On Thu, Aug 2, 2012 at 7:39 AM, Joshua Wiley <jwiley.psych at gmail.com> wrote:
This is false, I believe.  From the manual:

"The R INSTALL facilities check if the version of R used is recent
enough for the package being installed,and the list of packages which
is specified will be attached (after checking version requirements)
before the current package, ..."

So including a package in  Depends leads not only to loading, but attaching.

-- Bert

 However, let's say that function foo() is

  
    
#
Hi Michael,
Thank you so much for the help Michael! When I put my package 'PKG2' (on which PKG1 is depending on) in the regular place, perhaps a place R CMD check won't look at, then when I try to build PKG1, they will say PKG2 required but not found, so I have to put it under a directory R looks at, just the paths under '.libPaths()'. 
I read through the manual (the 7th section entitled "tools"of "R internals"), and they suggest changing check.Environ under ~/.R, so I have tried to put check.Environ "~/.R", "~/" is my home directory which is just the path shown after typing cd &&pwd. But It doesn't work, so I thought "~/" might be something else.
Thank you so much for the help, I really appreciate that!
Yours,
Xuan

-----Original Message-----
From: R. Michael Weylandt [mailto:michael.weylandt at gmail.com] 
Sent: Thursday, August 02, 2012 10:46 AM
To: Xuan Zhao
Cc: r-help at r-project.org
Subject: Re: [R] How to link two R packages together
On Thu, Aug 2, 2012 at 9:02 AM, Xuan Zhao <xuan.zhao at sentrana.com> wrote:
By which you mean .... ?
"~" is a standard UNIX (POSIX?) shorthand for the user's home directory. On my Mac it is "/Users/mweylandt" and I imagine you'd get something similar on all Linux and BSD systems -- one easy way to see where it is: go to your terminal and type "cd && pwd" which will print it out. No idea what the parallel on Windows is: possibly something under C:/Documents and Settings but it's been so long since I've had the misfortune of doing serious work on Windows, I can't remember.

Incidentally, is there a reason you can't put your package in the "regular" place?

Finally, see the manuals that Bert, Josh, and I have pointed you to:
they are much more authoritative on these matters than any of us.

Best,
Michael