6

Is it good practice to include every library I need to execute a function within that function?

For example, my file global.r contains several functions I need for a shiny app. Currently I have all needed packages at the top of the file. When I'm switching projects/copying these functions I have to load the packages/include them in the new code. Otherwise all needed packages are contained in that function. Of course I have to check all functions with a new R session, but I think this could help in the long run.

When I tried to load a package twice it won't load the package again but checks it's already loaded. My main question is if it would slow my functions if I restructure in that way?

I only saw that practice once, library calls inside functions, so I'm not sure.

csgillespie
  • 59,189
  • 14
  • 150
  • 185
Sebastian
  • 2,430
  • 4
  • 23
  • 40
  • 5
    As much as possible a function should not have global effects, i.e. effects outside the function itself, but loading a package is a global effect so it is undesirable to put that in a function. Instead it should be specified globally outside the function. – G. Grothendieck Mar 29 '16 at 13:01
  • 1
    @G.Grothendieck: that should be an answer. :) – Joshua Ulrich Mar 29 '16 at 13:03
  • You could consider putting your functions into a package. That would offer you better control regarding if you want to load other packages or import all or only some functions from them. – Roland Mar 29 '16 at 13:08
  • I totally see your point G. Grothendieck. To go one step ahead, I could load the needed librarys and detach them in the function? @Roland I should have a look into that, thanks for the advice – Sebastian Mar 29 '16 at 13:09
  • 1
    Detaching them is a global effect. – G. Grothendieck Mar 29 '16 at 13:16

1 Answers1

7

As one of the commenters suggest, you should avoid loading packages within a function since

  1. The function now has a global effect - as a general rule, this is something to avoid.
  2. There is a very small performance hit.

The first point is the big one. As with most optimisation, only worry about the second point if it's an issue.

Now that we've established the principle, what are the possible solution.

  1. In small projects, I have a file called packages.R that includes all the library calls I need. This is sourced at the top of my analysis script. BTW, all my functions are in a file call func.R. This workflow was stolen/adapted from a previous SO question

  2. If you're only importing a single function, you could use the :: trick, e.g. package::funcA(...) That way you avoid loading the package.

  3. For larger projects, I create an R package that handles all necessary imports. The benefit of creating a package is detailed in this answer on structuring large R projects.

Community
  • 1
  • 1
csgillespie
  • 59,189
  • 14
  • 150
  • 185
  • Thank you for your insights. I'm going to structure my functions in an internal responsitory, as this project got really messy in the last months. – Sebastian Mar 29 '16 at 13:19