Re: [R-pkg-devel] sf subsetting with square bracket

2019-02-05 Thread Duncan Murdoch

On 05/02/2019 3:57 a.m., Berry Boessenkool wrote:


I was pointed to a similar problem in data.table:
https://github.com/Rdatatable/data.table/issues/2341
It turns out it is sufficient to import (any random) single function 
from sf.

The "[" method will then be available as well.
E.g. with the following line included, the sf methods are available 
without the package being attached first:

#' @importFrom sf st_sf

Is this cleaner than requireNamespace("sf") because it only loads a 
single function?


It loads the entire package, and it does it unconditionally.  (You can't 
*load* just one function; you get the whole namespace loaded if you ask 
for one function.  However, you will have access to just that one 
function from your own functions.)  You would need to list "sf" in the 
Imports: section, and every user of your package would have to have "sf" 
installed.


That would be appropriate if your package can't function without working 
with "sf" objects.  I don't think it's the appropriate solution for 
"osmplotr", because they want to remain small, and the "sf" package is 
big.  They want to be able to operate even if the user chooses not to 
install "sf", and apparently all but one of their functions can do so.


Duncan Murdoch



Both approaches work fine. Thanks, Duncan, for digging into this!

Btw: sf people are aware: https://github.com/r-spatial/sf/issues/970


Kind regards,
Berry



*From:* Duncan Murdoch 
*Sent:* Monday, February 4, 2019 15:11
*To:* Berry Boessenkool; R package devel
*Subject:* Re: [R-pkg-devel] sf subsetting with square bracket
On 03/02/2019 8:26 p.m., Duncan Murdoch wrote:

On 03/02/2019 5:21 p.m., Berry Boessenkool wrote:


Hi,
my package in question can only be online next week, but here's a script
for a MWE:
https://gist.github.com/brry/7728b9b2d35afad7f1fc5978c3315009
The script uses devtools and osmplotr to
- create a package with a bare minimum dummy function
- run the code showing the problem
- create a solution using the ::: import
- clean up, removing the package and the folder for it


Thanks.  I can confirm the issue.  I think the problem is that neither
your sample package nor osmplotr declares a dependency on sf, and it is
not loaded.  I don't know if it makes sense for your package to do so,
but osmplotr probably should, since it is creating "sf" objects.

If I force sf to be loaded by changing your dummy function to

dummy <- function(map,obj)
{
 requireNamespace("sf")
 add_osm_objects(map, obj[1:10,], col='green')
}

then things are fine.  In your real package, you'll want to be prepared
to handle the case where "sf" is not installed.

sf is a big package, so maybe it's intentional to not require it.  I
don't know these packages at all, but it seems to me that if osmplotr is
returning objects of class "sf", then it should probably make sure that
the "sf" package is loaded so that methods can be found.


I've reported this on the osmplotr Github page:
https://github.com/ropensci/osmplotr/issues/46.  It appears that osmdata
has the same issue, but the maintainers know about it and are working on it.

In the meantime I'd add the requireNamespace("sf") call to your package
just to be defensive.

Duncan Murdoch


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] sf subsetting with square bracket

2019-02-04 Thread Duncan Murdoch

On 03/02/2019 8:26 p.m., Duncan Murdoch wrote:

On 03/02/2019 5:21 p.m., Berry Boessenkool wrote:


Hi,
my package in question can only be online next week, but here's a script
for a MWE:
https://gist.github.com/brry/7728b9b2d35afad7f1fc5978c3315009
The script uses devtools and osmplotr to
- create a package with a bare minimum dummy function
- run the code showing the problem
- create a solution using the ::: import
- clean up, removing the package and the folder for it


Thanks.  I can confirm the issue.  I think the problem is that neither
your sample package nor osmplotr declares a dependency on sf, and it is
not loaded.  I don't know if it makes sense for your package to do so,
but osmplotr probably should, since it is creating "sf" objects.

If I force sf to be loaded by changing your dummy function to

dummy <- function(map,obj)
{
requireNamespace("sf")
add_osm_objects(map, obj[1:10,], col='green')
}

then things are fine.  In your real package, you'll want to be prepared
to handle the case where "sf" is not installed.

sf is a big package, so maybe it's intentional to not require it.  I
don't know these packages at all, but it seems to me that if osmplotr is
returning objects of class "sf", then it should probably make sure that
the "sf" package is loaded so that methods can be found.


I've reported this on the osmplotr Github page: 
https://github.com/ropensci/osmplotr/issues/46.  It appears that osmdata 
has the same issue, but the maintainers know about it and are working on it.


In the meantime I'd add the requireNamespace("sf") call to your package 
just to be defensive.


Duncan Murdoch

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] sf subsetting with square bracket

2019-02-03 Thread Duncan Murdoch

On 03/02/2019 5:21 p.m., Berry Boessenkool wrote:


Hi,
my package in question can only be online next week, but here's a script 
for a MWE:

https://gist.github.com/brry/7728b9b2d35afad7f1fc5978c3315009
The script uses devtools and osmplotr to
- create a package with a bare minimum dummy function
- run the code showing the problem
- create a solution using the ::: import
- clean up, removing the package and the folder for it


Thanks.  I can confirm the issue.  I think the problem is that neither 
your sample package nor osmplotr declares a dependency on sf, and it is 
not loaded.  I don't know if it makes sense for your package to do so, 
but osmplotr probably should, since it is creating "sf" objects.


If I force sf to be loaded by changing your dummy function to

dummy <- function(map,obj)
{
  requireNamespace("sf")
  add_osm_objects(map, obj[1:10,], col='green')
}

then things are fine.  In your real package, you'll want to be prepared 
to handle the case where "sf" is not installed.


sf is a big package, so maybe it's intentional to not require it.  I 
don't know these packages at all, but it seems to me that if osmplotr is 
returning objects of class "sf", then it should probably make sure that 
the "sf" package is loaded so that methods can be found.


Duncan Murdoch




btw:  sf 0.7.2,   R 3.4.3,   x86_64-w64-mingw32/x64 (64-bit)
*
*
Kind regards,
Berry



*From:* Duncan Murdoch 
*Sent:* Sunday, February 3, 2019 21:41
*To:* Berry Boessenkool; R package devel
*Subject:* Re: [R-pkg-devel] sf subsetting with square bracket
On 03/02/2019 3:02 p.m., Berry Boessenkool wrote:


Hi,

if sf is not loaded, subsetting an sf object with square brackets loses the sf 
class in the geometry column:
sfobj[somerows,]
For usage in a package, I do not want to call library(sf) first.

[ is an S3 method in sf:
https://github.com/r-spatial/sf/blob/master/NAMESPACE#L6
https://github.com/r-spatial/sf/blob/master/R/sf.R#L299

In my package (with an sf object from osmplotr), using
sf:::"[.sf"(obj, somerows, allcolumns)
works fine, but of course Rcmd check complains about the Unexported object 
imported by a ':::' call.

How can I use the sf [ method without completely loading sf?
(and also without depending on dplyr::select)

I guess I'm missing some really clever method import.
Can someone point me in the right direction?


You need to show us something reproducible if you want help.  Is your
package on Github or some other public repository?

Duncan Murdoch


__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] sf subsetting with square bracket

2019-02-03 Thread Duncan Murdoch

On 03/02/2019 3:02 p.m., Berry Boessenkool wrote:


Hi,

if sf is not loaded, subsetting an sf object with square brackets loses the sf 
class in the geometry column:
sfobj[somerows,]
For usage in a package, I do not want to call library(sf) first.

[ is an S3 method in sf:
https://github.com/r-spatial/sf/blob/master/NAMESPACE#L6
https://github.com/r-spatial/sf/blob/master/R/sf.R#L299

In my package (with an sf object from osmplotr), using
sf:::"[.sf"(obj, somerows, allcolumns)
works fine, but of course Rcmd check complains about the Unexported object 
imported by a ':::' call.

How can I use the sf [ method without completely loading sf?
(and also without depending on dplyr::select)

I guess I'm missing some really clever method import.
Can someone point me in the right direction?


You need to show us something reproducible if you want help.  Is your 
package on Github or some other public repository?


Duncan Murdoch

__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel