G.Maubach at weinwolf.de
2016-Oct-13 08:18 UTC
[R] Visibility of libraries called from within functions
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? Kind regards Georg
Duncan Murdoch
2016-Oct-13 08:43 UTC
[R] Visibility of libraries called from within functions
On 13/10/2016 4:18 AM, G.Maubach at 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
G.Maubach at weinwolf.de
2016-Oct-13 10:21 UTC
[R] Antwort: Re: Visibility of libraries called from within functions
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") } 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? Kind regards Georg Von: Duncan Murdoch <murdoch.duncan at gmail.com> An: G.Maubach at weinwolf.de, r-help at 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.Maubach at 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. Therewere> conflicts between the methods within these functions causing R to crash. > > I thought about defining functions for the different task and callingthe> 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 environmentand> stayed there although I had expected R to unload the library whenleaving> the function. Thus confilics can occur more often. > > I had a look into ?library and saw that there is no argument telling Rto> 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