Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

accessing .SD via data.table::.SD gives NULL #2306

Closed
spennihana opened this issue Aug 16, 2017 · 11 comments
Closed

accessing .SD via data.table::.SD gives NULL #2306

spennihana opened this issue Aug 16, 2017 · 11 comments

Comments

@spennihana
Copy link

spennihana commented Aug 16, 2017

I wasn't able to find an answer to this via searching SO/issues, so I'll just ask:

Is this legal way of accessing .SD:

someFun <- function(dt) {
  stopifnot(!is.null(dt))
  5
}

r <- data.table::data.table(iris)
r[ , someFun(data.table::.SD)    # Not OK
   , by=.(Sepal.Length)
 ]

# Error: !is.null(dt) is not TRUE

r[ , someFun(.SD)     # OK
   , by=.(Sepal.Length)
 ]

In someFun, the arg dt is NULL. Not a huge pain since I can do an explicit package import, but curious never-the-less.

edit: fixed up example

@MichaelChirico
Copy link
Member

MichaelChirico commented Aug 16, 2017 via email

@spennihana
Copy link
Author

Thanks for your response; it's still bothersome that there's no way of telling data.table to get the groups without explicitly importing the package.

At first I was confused by what you meant by "not exported" (https://github.com/Rdatatable/data.table/blob/master/NAMESPACE#L27), but I see that you mean it's built on-the-fly.

I want to import and use data.table in this way, without explicitly setting it in my DESCRIPTION:depends. Any trick here?

@MichaelChirico
Copy link
Member

MichaelChirico commented Aug 17, 2017 via email

@spennihana
Copy link
Author

when developing a package id like to access via the package namespace. Among other things it avoids noisy startup messages that I don't want to appear.

@jangorecki
Copy link
Member

@spennihana did you tried just .SD? You have to put to imports/depends every package that you use by ::, you don't have to import its functions or attach to search path. Unless you escape that function call with something like requireNamespace, then Suggests is enough.

@spennihana
Copy link
Author

spennihana commented Aug 17, 2017

Tried bare .SD too, no luck.

It is desirable for all packages to be in Imports. I do not want them in Depends.

Yes, this R package is following a strict style of <pkg>::<fn>. But then we came across the current issue. I could look at requireNamespace to special-case data.table as a way of muting the startup messages; but it's unfortunate that this needs special handling...

@spennihana
Copy link
Author

Actually, I got a hint on how to handle this from Matt via: #850

I will set these to NULL just before usage.

Thanks!

@f2ostie
Copy link

f2ostie commented Feb 21, 2018

I'm having the same issue. Could somebody elaborate on the solution?

I imported data.table in the DESCRIPTION:

Imports:
  data.table (>= 1.10.0),
	RODBC (>= 1.3.0)

but

  r <- data.table::data.table(iris)
  r[ , print(.SD), by=.(Sepal.Length)]

just prints NULL.

Tried setting .SD to NULL before function to no avail.

 r <- data.table::data.table(iris)
.SD <- NULL
r[ , print(.SD), by=.(Sepal.Length)]

Can anybody fix my example? (Tried data.table::.SD but gave same result)

@HughParsonage
Copy link
Member

HughParsonage commented Feb 21, 2018

Have you imported data.table in your package's NAMESPACE?

@f2ostie
Copy link

f2ostie commented Feb 21, 2018

I have done that now and it indeed works, thanks!

@jangorecki
Copy link
Member

jangorecki commented Feb 22, 2018

@f2ostie see also the new vignette: https://rdatatable.gitlab.io/data.table/library/data.table/doc/datatable-importing.html

MichaelChirico pushed a commit that referenced this issue Jan 13, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants