2

I have a problem similar to, yet distinct from, How can I handle R CMD check "no visible binding for global variable" notes when my ggplot2 syntax is sensible?.

In that scenario everything works smoothly by using aes_string instead of aes. However, this is not possible with plyr afaik.

The problem arises when I reference column names in my data frame via ddply for example.

ddply(mydf, .(VarA, VarB, VarC, VarD), summarize, sum = sum(VarE))
#
# MyPackage: no visible binding for
#   global variable ‘VarA’

This code is completely valid and sane and even though I understand the use of the NOTE's they still clutter up the other messages in the output windows making package development a pain and actually enforcing the developers to ignore NOTEs.

What is the correct way to get rid of these notes? Or alternatively what is the correct way to write the code in a way that R CMD check accepts without giving NOTE's?

Best, Michael

Community
  • 1
  • 1
Dr. Mike
  • 2,451
  • 4
  • 24
  • 36

2 Answers2

5

There are several workarounds. The easiest is to just assign NULL to all variables with no visible binding.

VarA <- VarB <- VarC <- VarD <- VarE <- NULL

A more elegant solution is to use as.quoted and substitute. UPDATE by @Dr. Mike: the call to as.quoted need to be encapsulated with c().

ddply(mydf, 
      as.quoted(c('VarA', 'VarB', 'VarC', 'VarD')), 
      summarize, 
      sum = sum(substitute(VarE)))
shadow
  • 21,823
  • 4
  • 63
  • 77
0

You can avoid CMD check warnings by declaring global variables:

globalVariables(c("VarA", "VarB"))

Please read ?globalVariables before using, and make sure the appropriate R version is added to your DESCRIPTION.

Avoiding the use of globals by quoting is always preferable.

Axeman
  • 32,068
  • 8
  • 81
  • 94