[datatable-help] which parent.frame is more correct

Michael Nelson michael.nelson at sydney.edu.au
Mon Mar 4 00:47:29 CET 2013


In answering http://stackoverflow.com/a/15102156/1385941

I confidently stated at parent.frame(3) was the correct frame to use, and I stand by that, but am slightly confused over how parent.frame 1 and 3 differ in how they are evaluated. 

More specifically, I don't understand why `parent.frame(1)` works as it does. 

Take for example

x <- 3:4

dt <- data.table(x = 1:5,y=5:1, key = 'x')

foo <-function(){
  x <- 1:2
  for(n in 1:5) {
    print(dt[list(get('x',parent.frame(n)))])
  }
}

foo()

# n= 1
# uses parent.frame of foo
# 
#    x y
# 1: 3 3
# 2: 4 2


# n= 2 
# some kind of self join of  data.table 
# output equivalent of (dt[dt[list(x)]])

#    x y y.1
# 1: 1 5   5
# 2: 2 4   4
# 3: 3 3   3
# 4: 4 2   2
# 5: 5 1   1


# n= 3
# uses parent.frame of call to `[.data.table`

#    x y
# 1: 1 5
# 2: 2 4


# n >= 4
# uses parent.frame of foo again (makes sense I think)
#    x y
# 1: 5 1
# 2: 4 2
________________________________________
From: datatable-help-bounces at lists.r-forge.r-project.org [datatable-help-bounces at lists.r-forge.r-project.org] on behalf of Matthew Dowle [mdowle at mdowle.plus.com]
Sent: Monday, 4 March 2013 10:26 AM
To: victor.kryukov at gmail.com
Cc: datatable-help at lists.r-forge.r-project.org
Subject: Re: [datatable-help] Error in a package that imports data.table

Hi,

Did you include data.table in either the Imports or Depends field of
your package's DESCRIPTION file?

I've just improved data.table FAQ 6.9 to make that clearer.

If it still doesn't work, does your package fully pass "R CMD check"?

Matthew


On 03.03.2013 22:25, Victor Kryukov wrote:
> Hello,
>
> I'm developing an R package which will be used internally at my
> company, and I have troubles using data.table. I'm very new to
> package
> development and I'm not really sure whether the errors I see are
> related to data.table or not, but here it is anyway.
>
> I have a function that imports data from .csv files and cleans the
> data (subsets, converting fields to numeric etc.). As the end of the
> function definition, I convert the resulting data.frame to data.table
> and return the result:
>
> ProcessData <- function(…) {
>       ...
>       df <- data.table(df)
>       df
> }
>
> When I use this function standalone, after
>
> library(data.package)
>
> everything works as expected. However, when I'm defining this
> function as a part of a package and later call it, I'm getting the
> following error:
>
> Error in rbind(deparse.level, ...) :
>   could not find function ".rbind.data.table"
>
> Please note that in the package .R files, I'm not importing
> data.table directly with library(data.package) but rather have
> `import(data.table)` statement in my NAMESPACE, as recommended here
> https://github.com/hadley/devtools/wiki/Namespaces.
>
> When I import data.table directly with library(data.table) after
> importing my package, everything works as expected.
>
> I suspect there may be something going wrong with namespaces in
> data.table.
>
> My environment: I'm using R 2.15.3 on Mac and have tested the above
> on both data.table 1.8.6 and 1.8.7. Please let me know if I need to
> provide more info. Any help will be much appreciated!
>
> Regards,
> Victor
>
> _______________________________________________
> datatable-help mailing list
> datatable-help at lists.r-forge.r-project.org
>
> https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help

_______________________________________________
datatable-help mailing list
datatable-help at lists.r-forge.r-project.org
https://lists.r-forge.r-project.org/cgi-bin/mailman/listinfo/datatable-help


More information about the datatable-help mailing list