That is an interesting fix Duncan suggested and it sounds now like everything 
WORKED as intended in data.table except that any checker being used externally 
is not able to del with things that look like a variable but are actually not a 
variable currently visible except within a function that is doing deferred 
evaluation. In this case, it recognizes the raw unevaluated string as being the 
name of a column within the data.table.

I would guess this can cause similar anomalies in the dplyr package when it 
does the same kinds of non-standard evaluation and I wonder even about the 
with() command or the within() which internally expand columns out so that 
commands using them work.

But setting the variable to NULL bothers me. Yes, it shuts up a check to see if 
the variable exists. But is there any reason you could not have a global 
variable and any number of local variables with the same name as the column, 
especially if non-standard evaluation only looks for the column and ignores 
other instances of the name in other environments? Setting it to NULL 
unconditionally could mess up some programs.

There are ways to set it conditionally and save the previous value and restore 
it after but this gets to be lots more work ...


-----Original Message-----
From: R-help <r-help-boun...@r-project.org> On Behalf Of Naresh Gurbuxani
Sent: Tuesday, January 28, 2025 4:25 PM
To: Duncan Murdoch <murdoch.dun...@gmail.com>
Cc: r-help@r-project.org
Subject: Re: [R] R CMD check says no visible binding for global variable

This solution worked.
Thanks

Sent from my iPhone

> On Jan 28, 2025, at 3:09 PM, Duncan Murdoch <murdoch.dun...@gmail.com> wrote:
> 
> On 2025-01-28 1:55 p.m., Naresh Gurbuxani wrote:
>> Data.frame is returned by SQL query.  It does have column names.  In the 
>> function, I make small changes to some columns.
>> Something like:
>> Myquery <- “SELECT date, price, stock FROM stocktab WHERE stock = ‘ABC’ AND 
>> date > ‘2025-01-01’;”
>> Prices <- dbGetQuery(con, myquery)
>> SetDT(Prices)
>> Prices[, date = as.Date(date)]
> 
> If Prices were a regular dataframe at this point, then the message would be 
> correct. You can't calculate `as.Date(date)` without telling R where to look 
> for the `date` variable.
> 
> However, you have set it to be a data.table instead.  They use nonstandard 
> evaluation and look up `date` in the columns of `Prices`, and things work.  
> However, R's checks don't know this, so you still get the complaint.
> 
> The fix given by others is easiest:  sometime before this add a line
> 
>  date <- NULL
> 
> and it will satisfy the check code.
> 
> Duncan Murdoch
>> R CMD check say “no visible binding for global variable ‘date’”
>> Sent from my iPhone
>> On Jan 28, 2025, at 1:24 AM, Sorkin, John <jsor...@som.umaryland.edu> wrote:
>> 
>> There you go, once again helping strengthen ;)
>> John
>> Get Outlook for iOS<https://aka.ms/o0ukef>
>> ________________________________
>> From: R-help <r-help-boun...@r-project.org> on behalf of 
>> avi.e.gr...@gmail.com <avi.e.gr...@gmail.com>
>> Sent: Tuesday, January 28, 2025 12:01:25 AM
>> To: 'Naresh Gurbuxani' <naresh_gurbux...@hotmail.com>; r-help@r-project.org 
>> <r-help@r-project.org>
>> Subject: Re: [R] R CMD check says no visible binding for global variable
>> Naresh,
>> I am not sure how you are creating your data.frame so it has no, I think, 
>> column names. There are two scenarios including one where it is not really a 
>> valid data.frame and one where it can be handled before any other use as 
>> shown below. If it cannot be used, you might need to modify how your SQL or 
>> the function you call creates it so it includes either names it chooses or 
>> that you supply.
>> One silly solution if to give your data frame names before using it later. 
>> In prticulr, if you know what the columns contain, you can choose suitable 
>> names like this if you have exactly three columns:
>>> colnames(mydata) <- c("first", "second", "third")
>>> mydata
>>   first second third
>> 1     1      2     3
>> If you have a varying number of columns and don't care what the names are, 
>> you can make n names that look like temp1, temp2, ... tempn like this:
>>> paste0("temp", 1:ncol(mydata))
>> [1] "temp1" "temp2" "temp3"
>> Obviously, you substitute in whatever your data.frame is called.
>> So the code to add names for columns looks like:
>>> colnames(mydata) <- paste0("temp", 1:ncol(mydata))
>>> mydata
>>   temp1 temp2 temp3
>> 1     1     2     3
>> -----Original Message-----
>> From: R-help <r-help-boun...@r-project.org> On Behalf Of Naresh Gurbuxani
>> Sent: Monday, January 27, 2025 5:46 PM
>> To: r-help@r-project.org
>> Subject: [R] R CMD check says no visible binding for global variable
>> I have written a function which returns an SQL query result as a data.frame. 
>>  Each column of data.frame is a variable not explicitly defined.
>> For every column name, R CMD check says ‘no visible binding for global 
>> variable <name>. Status: 1 NOTE
>> Is it possible to tell R CMD check that these variables are OK?
>> Thanks,
>> Naresh
>> Sent from my iPhone
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide 
>> https://www.r-project.org/posting-guide.html<https://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help<https://stat.ethz.ch/mailman/listinfo/r-help>
>> PLEASE do read the posting guide 
>> https://www.r-project.org/posting-guide.html<https://www.r-project.org/posting-guide.html>
>> and provide commented, minimal, self-contained, reproducible code.
>>    [[alternative HTML version deleted]]
>> ______________________________________________
>> R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
>> https://stat.ethz.ch/mailman/listinfo/r-help
>> PLEASE do read the posting guide https://www.r-project.org/posting-guide.html
>> and provide commented, minimal, self-contained, reproducible code.
> 
______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide https://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to