[R] How to link two R packages together

Xuan Zhao xuan.zhao at sentrana.com
Thu Aug 2 16:45:22 CEST 2012


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:
> 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
>> of the other package (PKG2).
>
> 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:
>> On Wed, Aug 1, 2012 at 10:28 PM, Bert Gunter <gunter.berton at gene.com> wrote:
>>> On Wed, Aug 1, 2012 at 6:45 PM, Michael Weylandt 
>>> <michael.weylandt at gmail.com> wrote:
>>>> Isn't this what package dependencies are for?
>>>
>>> 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.
>>
>> 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
>>
>>>
>>> -- Bert
>>>
>>>>
>>>> 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:
>>>>
>>>>> 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-
>>>>> t p4638765.html Sent from the R help mailing list archive at 
>>>>> Nabble.com.
>>>>>
>>>>> ______________________________________________
>>>>> R-help at r-project.org mailing list
>>>>> 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 at r-project.org mailing list
>>>> 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.
>>>
>>>
>>>
>>> --
>>>
>>> Bert Gunter
>>> Genentech Nonclinical Biostatistics
>>>
>>> Internal Contact Info:
>>> Phone: 467-7374
>>> Website:
>>> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/p
>>> d
>>> b-biostatistics/pdb-ncb-home.htm
>>>
>>> ______________________________________________
>>> R-help at r-project.org mailing list
>>> 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.
>>
>>
>>
>> --
>> Joshua Wiley
>> Ph.D. Student, Health Psychology
>> Programmer Analyst II, Statistical Consulting Group University of 
>> California, Los Angeles https://joshuawiley.com/
>
>
>
> --
>
> Bert Gunter
> Genentech Nonclinical Biostatistics
>
> Internal Contact Info:
> Phone: 467-7374
> Website:
> http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb
> -biostatistics/pdb-ncb-home.htm



--
Joshua Wiley
Ph.D. Student, Health Psychology
Programmer Analyst II, Statistical Consulting Group University of California, Los Angeles https://joshuawiley.com/


More information about the R-help mailing list