21

I'm new to SO but interested in posting some R solutions where I can. There are potentially lots of solutions to the problems that are posted. When is it appropriate to post an answer that requires a different package than in the original question? How should I provide a code solution showing a new package?

As an example, I answered this question using dplyr. My answer included a library(dplyr) line to indicate that the package would be needed.

1
  • 13
    Why is this specific to R?
    – user193661
    Commented Dec 5, 2015 at 6:28

4 Answers 4

13

I try not to use other packages for a few reasons:

  1. I think a good grasp of what is available within base R helps me get the most out of R
  2. The more packages I use, the more the risk of incompatibilities, especially when dealing with specialized data structures
  3. There's just something ugly about an R-script that requires as many packages as it has lines.
  4. Packages come and go. There are packages that are stable but not maintained, and others that are today's hot stuff, and then there are those that are very specialized. Building a new package into my workflow is therefore a bit of a risk.

So, I usually try to answer R questions using base-R functions as much as possible. But, when is it acceptable to use a new or different package?

  1. If the new package offers the capability to both 1) replace bits or all of the orginal code and 2) answer the question
  2. If the OP is using old / deprecated packages
  3. If the question talks about a further workflow where there are specific needs that the new package might help with.

So, let's say there's a package that looks helpful. So, how should you provide a code solution? Personally I would submit an answer that includes:

  1. A solution using the original packages as far as possible (or refers to another answer in base R or the original packages)
  2. An example using a specific package, complete with the require(blah) and any information about the repository if it's not on CRAN.
  3. A little bit of background about why the new package is better than the current choice

The choice of package is very personal and the OP and community can choose the answer with the package that best fits their workflow; the up- or down votes that you get will tell you the result. At the end of the day an answer that suggests a new package may turn out to be very useful, or not. It's a little bit the luck of the draw. Good luck!

1
  • Just for interest, why the down votes? Commented Dec 7, 2015 at 18:40
34

As a general advice, if you are going to answer any open source question, please don't be this guy

enter image description here

And specifically to R, my rule of thumb is simple:

Can you provide a concise, efficient, vectorized (and preferably not too verbose) answer with base R - certainly use base R. As this the most maintained and (probably) most tested resource we have.

If not, certainly use an external package - preferably from CRAN, rather than GitHub, because these packages have to be maintained and meet some requirements before being able to get into CRAN.

As a general rule - packages aren't a bad thing and they are an integral part of an open source software. Though I strongly believe that every R user has to know base R before he knows anything else (unlike some package developers who believe that if one uses their package - they don't need to know anything else). Because without it, you are like an R handicap who can't solve anything once it gets out of your package related comfort zone - in other words, you are not a real R programmer by any means.


2
  • 6
    Ha, you didn't loose your sense of humor ;)
    – rene
    Commented Dec 7, 2015 at 11:11
  • 4
    Your code isn't portable :^) — BonerLord
    – user764357
    Commented Dec 7, 2015 at 22:21
30

Since you're talking about an open source package that's readily available to anyone with R installed and an Internet connection, I'd say your answer is fine. Asking for library recommendations is off-topic, so people are actively encouraged to just ask how to solve their specific problem instead. Answers that show how to implement a solution are great, but so are ones that show us how to solve a problem using an existing library. (Just be sure to show how to use the library to solve the problem, instead of just making a recommendation.)

9

I think your approach was perfect. I prefer answers that only require one of the core/recommended packages unless the question demands it for goals of efficiency or the questioner specifically requests it. In the early days of ggplot (then ggplot2), that meant I would try to offer a base graphics or lattice solution. These days ggplot2 is mainstream and many people consider it close to expected. Questions that indicated the need to bypass performance hurdles might require data.table or Rcpp solutions, and I sit back and wait for replies from more experienced experts if they are over my somewhat limited skillset in those areas.

On the other hand, I would never downvote a solution because it required an extra package, although I might not upvote it when there was a perfectly solid answer that required no extra packages. I, therefore, withhold upvotes if tidyverse is used when there is a much more simple base solution offered. I might downvote a question if I tried it and tehn discovered that a required package had not been loaded by either the OP or the answer.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .