r-spatial / stars

Spatiotemporal Arrays, Raster and Vector Data Cubes
https://r-spatial.github.io/stars/
Apache License 2.0
563 stars 94 forks source link

Exporting s3 methods from namespace #662

Closed pepijn-devries closed 10 months ago

pepijn-devries commented 10 months ago

Hi,

Thanks for all your work on this great package.

Many of the stars S3 methods are not exported in the NAMESPACE file. Consequently, when I'm developing a package that imports from stars, and call a generic like as.data.frame on a stars object, I get:

Error in UseMethod("as.data.frame") : 
  no applicable method for 'as.data.frame' applied to an object of class "stars"

Alternatively, importing the s3 method as.data.frame.stars or calling stars:::as.data.frame.stars won't work either as these function are not exported by stars. I guess my issue is related to issues https://github.com/r-spatial/stars/issues/504 and https://github.com/r-spatial/stars/issues/622. The difference is that I can't load the package with library when developing a package.

Is it possible to export these functions, or is there a reason not to?

Thanks for considering.

edzer commented 10 months ago

I see

$ grep as.data.frame NAMESPACE 
S3method(as.data.frame,dimensions)
S3method(as.data.frame,stars)
S3method(as.data.frame,stars_proxy)
pepijn-devries commented 10 months ago

Thanks for the quick response. Yes, I did see those, but those statements do not export the specific functions. Apparently, it's a bit of a hassle to get s3 methods of other packages into a different namespace.

I ended up using https://vctrs.r-lib.org/reference/s3_register.html and added this to my code:

.onLoad <- function(...) {
  s3_register("stars::as.data.frame", "stars")
}

That seemed to have done the trick...

pepijn-devries commented 10 months ago

Nope, I was too quick with my response. The code above won't build. Again, because as.data.frame.stars is not exported from stars. I did get it working by preceding the call to as.data.frame with:

requireNamespace("stars", quietly = TRUE)

Sometimes the solution can be so easy 😄 /