0

I created an R package and submitted it to CRAN. The CMD check output 1 NOTE:

fun: no visible binding for global variable 'id'

CRAN suggested that I fix it.

fun() (one of my functions) is a custom import to read files created by another software. These files are very large datasets. Therefore, since I'm optimizing the import, I use data.table and set key, which not only help performance, but are required to run subsequent functions, such as foverlaps().

I'm using:

data.table::setDT(dataImport)

data.table::setkey(dataImport, id)

Why does setkey() creates this note?

How can I set key without creating a note?

The following QA (What is the purpose of setting a key in data.table?) is very useful to understanding setkey(), but neither this nor data.table documentation seem to provide any cues (that I spotted) for the arising of this particular issue. Thank you

Update -Below if the gist of the function where I'm facing this issue.

readoqcsv <- function(x) { dataImport <- utils::read.table(file = x, header = TRUE, sep = "," , dec = "." , colClasses = c("character", "integer", "character", "character", "integer", "NULL", "NULL", "NULL", "integer", "NULL", "NULL", "NULL", "NULL", "factor", "NULL", "NULL", "NULL", "NULL", "NULL"), comment.char = "")

data.table::setDT(dataImport)


dataImport$timeStampO <- strptime(dataImport$timeStampO, "%m/%d/%y  %H:%M")

dataImport$session_started <- strptime(dataImport$session_started,"%m/%d/%y  %H:%M")

dataImport$session_finished <- strptime(dataImport$session_finished,"%m/%d/%y %H:%M")

dataImport$id <- c(1:nrow(dataImport))

Below is the line that (if run) causes the NOTE:

# data.table::setkey(dataImport, id)

return(dataImport) }

Community
  • 1
  • 1
jpinelo
  • 1,414
  • 5
  • 16
  • 28
  • 2
    I usually add a line in my function something along the lines of `id <- NULL`. – A5C1D2H2I1M1N2O1R2T1 Aug 27 '15 at 07:02
  • For example, what I did [here](https://github.com/mrdwab/splitstackshape/blob/master/R/stratified.R#L87). – A5C1D2H2I1M1N2O1R2T1 Aug 27 '15 at 07:03
  • 1
    Also: https://stat.ethz.ch/R-manual/R-devel/library/utils/html/globalVariables.html – A5C1D2H2I1M1N2O1R2T1 Aug 27 '15 at 07:58
  • @Ananda Thank you, this is useful. Still didn't solve my issue though... Found something interesting with new information and perhaps related, you might be interested: http://stackoverflow.com/questions/31132552/no-visible-global-function-definition-for-median – jpinelo Aug 27 '15 at 11:04
  • @jpinelo provide the link to your code or reproducible example – jangorecki Aug 27 '15 at 17:57
  • @ jangorecki. I've added the code as suggested. Thank you for your interest. – jpinelo Aug 27 '15 at 18:56
  • 1
    Ananda's solution should've worked. Not sure why. Maybe another fix here is to use `setkeyv()` with `cols = "id"` instead. `setkey()` is designed with interactive cases in mind. – Arun Aug 27 '15 at 19:03
  • @Arun. setkeyv() did it. Thank you. Perhaps it was me who could't implement Ananda's solution well. In any case I prefer to not use setkey() inside a function, if it was created to use interactively. Thanks very much. – jpinelo Aug 27 '15 at 19:23
  • Glad it worked. If you could answer it yourself and accept it, it'd be great. – Arun Aug 27 '15 at 20:06
  • @A5C1D2H2I1M1N2O1R2T1 where do you add that line of code (can it go anywhere inside the function)? And what does it do? (i.e. why does that solve the problem) - I'm curious – stevec May 02 '19 at 17:07

1 Answers1

1

What worked in my case was to replace the function setkey() with setkeyv(), as suggested by Arun.

This creates a CMD check NOTE:

data.table::setkey(dataImport, id)

This runs NOTE free:

data.table::setkeyv(dataImport, cols = "id")

Based on data.table documentation, using setkeyv() seems to equivalent. @Arun please correct me if I'm wrong! In terms of results, my function still passes my automated tests for class, colnames and ncol.

In any case, Arun mentioned that setkey() was created with interactive use in mind, therefore it does not seem appropriate to be used in a function and/or package.

There are a few other suggestions out there for the CMD NOTES "no visible binding for global variable", such as the informative QAs:

How can I handle R CMD check "no visible binding for global variable" notes when my ggplot2 syntax is sensible?

No visible binding for global variable Note in R CMD check

Threads on the issue of "no visible binding..." tend to address it as one single issue. Which it seems to be based on its source (the symptom), the CMD check. However, despite many actions raising the same flag (symptom), deeper down they might be generated by different actions (conditions).

I like the solution used here, pointed out by Arun, because it addresses the specific case of the use of this function (the condition, rather than the symptom). Unlike the other solutions, probably equally legitimate, if more general, referred to on the links above.
Of course that this approach implies figuring out what exactly is causing the note, and finding an alternative.

Community
  • 1
  • 1
jpinelo
  • 1,414
  • 5
  • 16
  • 28