writing a conditional function to load packages

Hi all,

I'm trying to write a function to speed up a task I do every time I open R, which is load my favorite packages. I am trying to make it do the job of two commands in one; which is to say, load either just {tidyverse} and {janitor} or, if I toggle on an option, load a whole list of packages I use for running analysis. I can't get it to work for some reason though. Command so far is this:

prime_r=function(analyzing_data=TRUE){
  if(analyzing_data=TRUE) {pacman::p_load(tidyverse, janitor, bayestestR, bayesplot, performance, effectsize, rstanarm, see, parameters, insight, report, flextable)}
  if(analyzing_data=FALSE) {pacman::p_load(tidyverse, janitor)}
}

I can't even write the function into R's environment though; each time I try I get the following error:

Error: unexpected '}' in "}"

Anyone know what's wrong?

A few suggestions you could try. First, stick R conventions. Assign objects with the assignment operator <- instead of =.

Second, conditions to test for equality are conducted with double =, or == TRUE.

Adding the extra equals sign fixed it. Thanks!

That is an entirely subjective decision and not an R convention. I happen to prefer <- but this is a matter of taste. One of the original R co-authors happens to hate it.

It does seem to be the norm in the R community, but I cannot stand the inefficiency of using two keystrokes to accomplish something I can do with one. I'll never switch over to the arrow personally.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.