Von:    Duncan Murdoch <murdoch.dun...@gmail.com>
An:     g.maub...@weinwolf.de, r-help@r-project.org, 
Datum:  13.10.2016 12:34
Betreff:        Re: Antwort: Re: [R] Visibility of libraries called from 
within functions



On 13/10/2016 6:21 AM, g.maub...@weinwolf.de wrote:
> Hi Duncan,
>
> many thanks for your reply.
>
> Your suggestion of using requireNamespace() together with explicit
> namespace calling using the "::" operator is what I was looking for:
>
> -- cut --
>
> f_test <- function() {
>     requireNamespace("openxlsx")
>     cat("Loaded packages AFTER loading library")
>     print(search())
>     xlsx::read.xlsx(file = "c:/temp/test.xlsx",
>                     sheetName = "test")
> }

Not sure if that's a typo in your message or a real error, but you 
require "openxlsx" and then use "xlsx".

It's a typo!


>
> cat("Loaded packages BEFORE function call ----------------------------")
> search()
>
> f_test()
>
> cat("Loaded packages AFTER function call -----------------------------")
> search()
>
> -- cut  --
>
> When reading ?requireNamespace I did not really get how R operates 
behind
> the scenes.
>
> Using "library" attaches the namespace to the search path. Using
> "requireNamespace" does not do that.
>
> But how does R find the namespace then? What kind of list or directory
> used R to to store the namespace and lookup the correct function or
> methods of this namespace?

R has an internal list of packages that are loaded.  Functions in them 
are only visible to user code if the package is *also* on the search 
list, or if the package name prefix is used with ::.

Can I have a look at this internal list like I can do with search() for 
pachages or ls() for objects?

If xlsx is loaded, xlsx::read.xlsx will just use it; if it is not 
loaded, the package will be loaded to make the call.  So you don't need 
the requireNamespace call if you can be sure that xlsx will be found. 
You would normally use its return value (FALSE if the package is not 
found) to test whether it will be safe to make the xlsx::read.xlsx call.

Got it!



Duncan Murdoch

>
> Kind regards
>
> Georg
>
>
>
>
> Von:    Duncan Murdoch <murdoch.dun...@gmail.com>
> An:     g.maub...@weinwolf.de, r-help@r-project.org,
> Datum:  13.10.2016 10:43
> Betreff:        Re: [R] Visibility of libraries called from within
> functions
>
>
>
> On 13/10/2016 4:18 AM, g.maub...@weinwolf.de wrote:
>> Hi All,
>>
>> in my R programs I use different libraries to work with Excel sheets, 
i.
>> e. xlsx, excel.link.
>>
>> When running chunks of code repeatedly and not always in the order the
>> program should run for development purposes I ran into trouble. There
> were
>> conflicts between the methods within these functions causing R to 
crash.
>>
>> I thought about defining functions for the different task and calling
> the
>> libraries locally to there functions. Doing this test
>>
>> -- cut --
>>
>> f_test <- function() {
>>     library(xlsx)
>>     cat("Loaded packages AFTER loading library")
>>     print(search())
>> }
>>
>> cat("Loaded packages BEFORE function call 
----------------------------")
>> search()
>>
>> f_test()
>>
>> cat("Loaded packages AFTER function call 
-----------------------------")
>> search()
>>
>> -- cut --
>>
>> showed that the library "xlsx" was loaded into the global environment
> and
>> stayed there although I had expected R to unload the library when
> leaving
>> the function. Thus confilics can occur more often.
>>
>> I had a look into ?library and saw that there is no argument telling R
> to
>> hold the library in the calling environment.
>>
>> How can I load libraries locally to the calling functions?
>
> You can detach at the end of your function, but that's tricky to get
> right:  the package might have been on the search list before your
> function was called.  It's better not to touch the search list at all.
>
> The best solution is to use :: notation to get functions without putting
> them on the search list.  For example, use
>
> xlsx::write.xlsx(data, file)
>
> If you are not sure if your user has xlsx installed, you can use
> requireNamespace() to check.
>
> 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.

Reply via email to