4

This is my first question, so apologies if this isn't perfectly appropriate. A colleague of mine recently sent me a script where she loaded all of her packages using lapply, instead of repeated calls to library, e.g.

packages_list <- c("dplyr", "ggplot2")

lapply(packages_list, library, character.only = TRUE)

Instead, this is how I normally do it:

library(dplyr)
library(ggplot2)

Is using lapply computationally faster somehow? I can see how that for a script requiring large numbers of packages, using lapply might save me from repeatedly typing out library, but with auto-complete this doesn't seem like a huge benefit.

Most of the people in my faculty are beginners with R (we are biologists), and many of my students don't even know how apply works...I think using lapply at the beginning of the script is possibly confusing for complete novices. I'm just curious if you expert coders out there could provide more illuminating input. Thank you!

kwiksetdr
  • 43
  • 4
  • Using `lapply` is 100% a matter of taste and will not provide any reasonable returns regarding speed of a script if you are doing anything interesting in that script versus running separate library statements. – lmo Apr 21 '18 at 22:16
  • i suspect you mentioned the only advantage in the question -- more compact for large numbers of packages. – lefft Apr 21 '18 at 22:16
  • On the other hand, `lapply` is typically a faster looping mechanism, so for quick methods, it is faster than a `for` loop. See my answer on [this post](https://stackoverflow.com/questions/21991130/simulating-a-random-walk/45900693) for an example of `for` versus `lapply`. – lmo Apr 21 '18 at 22:20
  • 1
    I think this is bordering on an opinion question which might get closed, but my opinion is to avoid extra packages when ever possible! – rawr Apr 21 '18 at 22:20
  • 2
    I agree that this is essentially a style question and personally, I don’t much like it (using lapply to load packages). – joran Apr 21 '18 at 22:31
  • This is completely a style question. And your colleague's got style. Reduce the duplication – Russ Hyde Apr 21 '18 at 22:47
  • 1
    Someone should post some combination of these comments as an answer (since no-one has voted to close as opinion-based ...) ... I agree that there's a tradeoff between compactness and clarity. In some contexts I like having the standalone vector of packages as used as well. – Ben Bolker Apr 21 '18 at 23:14
  • Thank you for your comments everyone. Having read the site rules over, I believe this does border on being an opinion question (I did explicitly ask about computation speed, so there's that), but I've found all of the opinions stated here to be incredibly valuable. I'd be happy to accept a summarized answer. – kwiksetdr Apr 22 '18 at 00:24

1 Answers1

1

There's no speed advantage in using:

packages_list <- c("dplyr", "ggplot2")
lapply(packages_list, library, character.only = TRUE)

... over:

library( dplyr)
library( ggplot2 )

It's also less compact until the number of packages exceeds about 8. You should give weight to @BenBolker whose is a highly valued contributor, not just to SO but also the R-help and R-SIG-ME Mailing Lists. (More opinion follows) The preferred approach would be to write the script in a text editor or an IDE that supports copying library( ) if a large number of packages are needed.

IRTFM
  • 258,963
  • 21
  • 364
  • 487