Include library calls in functions?

Is it good to include every library that I need to execute a function inside this function?

For example, my file global.rcontains several functions that I need for a brilliant application. Currently, I have all the necessary packages at the top of the file. When I switch projects / copying these functions, I have to download packages / include them in new code. Otherwise, all necessary packages are contained in this function. Of course, I need to test all the features with a new R session, but I think this can help in the long run.

When I tried to download the package twice, it will not download the package again, but checks that it is already downloaded. My main question is: can it slow down my functions if I restructure this way?

I saw this practice only once, the library calls inside the function, so I'm not sure.

+4
source share
1 answer

As one commenter points out, you should avoid loading packages inside a function, since

  • Now the function has a global effect - as a rule, this can be avoided.
  • very much .

The first point is a big one. As with most optimizations, worry only about the second question if this is a problem.

Now that we have established the principle, what is the possible solution.

  • packages.R, library, . script. BTW, func.R. / SO

  • , ::, . package::funcA(...) , .

  • R, . R.

+5

All Articles