[R] Visibility of libraries called from within functions

G.Maubach at weinwolf.de G.Maubach at weinwolf.de
Thu Oct 13 10:18:08 CEST 2016


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



More information about the R-help mailing list