Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-09 Thread Duncan Murdoch

On 09/02/2023 12:41 p.m., Alexandre Courtiol wrote:

Hi Igor,

I had the same issue using terra rather than sf a couple of weeks ago.

I thought of solving the issue as follow:

 1.

store the shapefiles under extdata.

 2.

create a function that loads the files:

.build_internal_files  <-  function() {
   ## This function should not be called by the user.
   ## It performs the lazy loading of the data since terra cannot handle 
rda files

   assign("CountryBorders",terra::vect(system.file("extdata/CountryBorders.shp",package  =  
"IsoriX")),envir  =  as.environment("package:IsoriX"))
   assign("OceanMask",terra::vect(system.file("extdata/OceanMask.shp",package  =  
"IsoriX")),envir  =  as.environment("package:IsoriX"))
}

 3. call that function automatically upon attach using |.onAttach()|:

.onAttach  <-  function(libname,pkgname) {
 .build_internal_files()## lazy loading of the internal data
}

It seems to work...

Note that .onAttach() is a standard way of defining a function that is 
recognised by R and ran when the package is attached.


Would these files ever be used when your package is not attached, e.g. 
through a function imported by a different package?  If so, you should 
use .onLoad(), not .onAttach().


Duncan Murdoch



++

Alex


On Thu, 9 Feb 2023 at 11:11, Duncan Murdoch > wrote:


On 09/02/2023 3:56 a.m., Ivan Krylov wrote:
 > В Wed, 8 Feb 2023 11:32:36 -0300
 > Igor L mailto:igorlal...@gmail.com>> пишет:
 >
 >> spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')
 >>
 >> plot(spatial_aisp) # works
 >>
 >> # Same data from .rda file after use usethis::use_data(spatial_aisp,
 >> overwrite = TRUE)
 >>
 >> x <- ispdata::spatial_aisp
 >>
 >> plot(x) # do not work
 >
 > Does this break in a new R session, but start working when you
load the
 > sf namespace? I think that your package needs to depend on sf in
order
 > for this to work. Specifying it in Imports may be enough to make the
 > plot.sf S3 method available to the user.

Specifying a package in the Imports field of DESCRIPTION guarantees
that
it will be available to load, but doesn't load it.  Importing something
from it via the NAMESPACE triggers a load, as does executing code like
pkg::fn, or explicitly calling loadNamespace("pkg"), or loading a
package that does one of these things.


 > You may encounter other problems if you go this way, like R CMD check
 > complaining that you don't use the package you're importing. Loading
 > the data from a file on demand would also load the sf namespace and
 > thus solve the problem.

Workarounds for the check complaints are discussed here, among other
places: https://stackoverflow.com/a/75384338/2554330
 .

Duncan Murdoch

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




--
Alexandre Courtiol, www.datazoogang.de 


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


Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-09 Thread Igor L
Thank you all for your help.

Adam, I followed your suggestion, but I still can't figure out why the data
is only available locally when I run the devtools::load_all() function.

When I install the package from GitHub, the data does not appear (even
using the data() function).

Extdata: https://github.com/igorlaltuf/ispdata/tree/main/inst/extdata
Import: https://github.com/igorlaltuf/ispdata/blob/main/R/spatial_cisp.R

And is there any way to reduce the size of files in gpkg format? The
package increased from 5 to 10 megabytes. I tried to make it smaller with
the function sf::st_write("inst/extdata/spatial_aisp.gpkg", compress =
"deflate", append = F), but the size remained the same.

Thanks again for all the help.

Em qui., 9 de fev. de 2023 às 14:41, Alexandre Courtiol <
alexandre.court...@gmail.com> escreveu:

> Hi Igor,
>
> I had the same issue using terra rather than sf a couple of weeks ago.
>
> I thought of solving the issue as follow:
>
>
>1.
>
>store the shapefiles under extdata.
>2.
>
>create a function that loads the files:
>
> .build_internal_files <- function() {
>   ## This function should not be called by the user.
>   ## It performs the lazy loading of the data since terra cannot handle rda 
> files
>   assign("CountryBorders", 
> terra::vect(system.file("extdata/CountryBorders.shp", package = "IsoriX")), 
> envir = as.environment("package:IsoriX"))
>   assign("OceanMask", terra::vect(system.file("extdata/OceanMask.shp", 
> package = "IsoriX")), envir = as.environment("package:IsoriX"))
> }
>
>
>1. call that function automatically upon attach using .onAttach():
>
> .onAttach <- function(libname, pkgname) {
> .build_internal_files() ## lazy loading of the internal data
> }
>
> It seems to work...
>
> Note that .onAttach() is a standard way of defining a function that is
> recognised by R and ran when the package is attached.
>
> ++
>
> Alex
>
> On Thu, 9 Feb 2023 at 11:11, Duncan Murdoch 
> wrote:
>
>> On 09/02/2023 3:56 a.m., Ivan Krylov wrote:
>> > В Wed, 8 Feb 2023 11:32:36 -0300
>> > Igor L  пишет:
>> >
>> >> spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')
>> >>
>> >> plot(spatial_aisp) # works
>> >>
>> >> # Same data from .rda file after use usethis::use_data(spatial_aisp,
>> >> overwrite = TRUE)
>> >>
>> >> x <- ispdata::spatial_aisp
>> >>
>> >> plot(x) # do not work
>> >
>> > Does this break in a new R session, but start working when you load the
>> > sf namespace? I think that your package needs to depend on sf in order
>> > for this to work. Specifying it in Imports may be enough to make the
>> > plot.sf S3 method available to the user.
>>
>> Specifying a package in the Imports field of DESCRIPTION guarantees that
>> it will be available to load, but doesn't load it.  Importing something
>> from it via the NAMESPACE triggers a load, as does executing code like
>> pkg::fn, or explicitly calling loadNamespace("pkg"), or loading a
>> package that does one of these things.
>>
>>
>> > You may encounter other problems if you go this way, like R CMD check
>> > complaining that you don't use the package you're importing. Loading
>> > the data from a file on demand would also load the sf namespace and
>> > thus solve the problem.
>>
>> Workarounds for the check complaints are discussed here, among other
>> places:  https://stackoverflow.com/a/75384338/2554330 .
>>
>> Duncan Murdoch
>>
>> __
>> R-package-devel@r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>>
>
>
> --
> Alexandre Courtiol, www.datazoogang.de
>

[[alternative HTML version deleted]]

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


Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-09 Thread Alexandre Courtiol
Hi Igor,

I had the same issue using terra rather than sf a couple of weeks ago.

I thought of solving the issue as follow:


   1.

   store the shapefiles under extdata.
   2.

   create a function that loads the files:

.build_internal_files <- function() {
  ## This function should not be called by the user.
  ## It performs the lazy loading of the data since terra cannot
handle rda files
  assign("CountryBorders",
terra::vect(system.file("extdata/CountryBorders.shp", package =
"IsoriX")), envir = as.environment("package:IsoriX"))
  assign("OceanMask", terra::vect(system.file("extdata/OceanMask.shp",
package = "IsoriX")), envir = as.environment("package:IsoriX"))
}


   1. call that function automatically upon attach using .onAttach():

.onAttach <- function(libname, pkgname) {
.build_internal_files() ## lazy loading of the internal data
}

It seems to work...

Note that .onAttach() is a standard way of defining a function that is
recognised by R and ran when the package is attached.

++

Alex

On Thu, 9 Feb 2023 at 11:11, Duncan Murdoch 
wrote:

> On 09/02/2023 3:56 a.m., Ivan Krylov wrote:
> > В Wed, 8 Feb 2023 11:32:36 -0300
> > Igor L  пишет:
> >
> >> spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')
> >>
> >> plot(spatial_aisp) # works
> >>
> >> # Same data from .rda file after use usethis::use_data(spatial_aisp,
> >> overwrite = TRUE)
> >>
> >> x <- ispdata::spatial_aisp
> >>
> >> plot(x) # do not work
> >
> > Does this break in a new R session, but start working when you load the
> > sf namespace? I think that your package needs to depend on sf in order
> > for this to work. Specifying it in Imports may be enough to make the
> > plot.sf S3 method available to the user.
>
> Specifying a package in the Imports field of DESCRIPTION guarantees that
> it will be available to load, but doesn't load it.  Importing something
> from it via the NAMESPACE triggers a load, as does executing code like
> pkg::fn, or explicitly calling loadNamespace("pkg"), or loading a
> package that does one of these things.
>
>
> > You may encounter other problems if you go this way, like R CMD check
> > complaining that you don't use the package you're importing. Loading
> > the data from a file on demand would also load the sf namespace and
> > thus solve the problem.
>
> Workarounds for the check complaints are discussed here, among other
> places:  https://stackoverflow.com/a/75384338/2554330 .
>
> Duncan Murdoch
>
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel
>


-- 
Alexandre Courtiol, www.datazoogang.de

[[alternative HTML version deleted]]

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


Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-09 Thread Duncan Murdoch

On 09/02/2023 3:56 a.m., Ivan Krylov wrote:

В Wed, 8 Feb 2023 11:32:36 -0300
Igor L  пишет:


spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')

plot(spatial_aisp) # works

# Same data from .rda file after use usethis::use_data(spatial_aisp,
overwrite = TRUE)

x <- ispdata::spatial_aisp

plot(x) # do not work


Does this break in a new R session, but start working when you load the
sf namespace? I think that your package needs to depend on sf in order
for this to work. Specifying it in Imports may be enough to make the
plot.sf S3 method available to the user.


Specifying a package in the Imports field of DESCRIPTION guarantees that 
it will be available to load, but doesn't load it.  Importing something 
from it via the NAMESPACE triggers a load, as does executing code like 
pkg::fn, or explicitly calling loadNamespace("pkg"), or loading a 
package that does one of these things.




You may encounter other problems if you go this way, like R CMD check
complaining that you don't use the package you're importing. Loading
the data from a file on demand would also load the sf namespace and
thus solve the problem.


Workarounds for the check complaints are discussed here, among other 
places:  https://stackoverflow.com/a/75384338/2554330 .


Duncan Murdoch

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


Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-09 Thread Ivan Krylov
В Wed, 8 Feb 2023 11:32:36 -0300
Igor L  пишет:

> spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')
> 
> plot(spatial_aisp) # works
> 
> # Same data from .rda file after use usethis::use_data(spatial_aisp,
> overwrite = TRUE)
> 
> x <- ispdata::spatial_aisp
> 
> plot(x) # do not work

Does this break in a new R session, but start working when you load the
sf namespace? I think that your package needs to depend on sf in order
for this to work. Specifying it in Imports may be enough to make the
plot.sf S3 method available to the user.

You may encounter other problems if you go this way, like R CMD check
complaining that you don't use the package you're importing. Loading
the data from a file on demand would also load the sf namespace and
thus solve the problem.

-- 
Best regards,
Ivan

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


Re: [R-pkg-devel] Problem with package containing spatial data

2023-02-08 Thread Adam H. Sparks
Hi Igor,
I’ve a couple packages that have spatial data in them that I maintain. I’ve 
opted to package up the data as a geopackage file and import it as an {sf} 
object. Edzer Pebesma suggests this to circumvent changes in the {sf} package 
from affecting native {sf} objects that are distributed as part of a package. 
Maybe this is an option for you?

In https://github.com/adamhsparks/extractOz, see /inst/extdata for the 
geopackage file and https://github.com/adamhsparks/extractOz/blob/main/R/data.R 
for handling the data importing and making available in the R session on 
package load.

Best,
Adam

> On 8 Feb 2023, at 10:32 pm, Igor L  wrote:
> 
> Hello all,
> 
> I'm developing a package that contains spatial data about public safety in
> Rio de Janeiro.
> 
> The problem is that when I use the usethis::use_data function which
> transforms the shapefile data into a file with the .rda extension, I cannot
> use the geometry attribute to create a map.
> 
> E.g.:
> 
> # Raw-data script:
> 
> spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')
> 
> plot(spatial_aisp) # works
> 
> # Same data from .rda file after use usethis::use_data(spatial_aisp,
> overwrite = TRUE)
> 
> x <- ispdata::spatial_aisp
> 
> plot(x) # do not work
> 
> Error message:
> Error in data.matrix(x) :
>  'list' object cannot be coerced to type 'double'
> 
> 
> This is happening with all spatial data in the package. I'm using lazydata:
> true and have already disabled file compression options, but the problem
> persists.
> 
> Any ideas?
> 
> Scripts can be accessed at https://github.com/igorlaltuf/ispdata
> 
> Thanks!
> -- 
> *Igor Laltuf Marques*
> Economist (UFF)
> Master in Urban and Regional Planning (IPPUR-UFRJ)
> Researcher at ETTERN and CiDMob Laboratories
> https://igorlaltuf.github.io/
> 
>   [[alternative HTML version deleted]]
> 
> __
> R-package-devel@r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-package-devel


[[alternative HTML version deleted]]

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


[R-pkg-devel] Problem with package containing spatial data

2023-02-08 Thread Igor L
Hello all,

I'm developing a package that contains spatial data about public safety in
Rio de Janeiro.

The problem is that when I use the usethis::use_data function which
transforms the shapefile data into a file with the .rda extension, I cannot
use the geometry attribute to create a map.

E.g.:

# Raw-data script:

spatial_aisp <- sf::st_read('data-raw/shp_aisp/lm_aisp_2019.shp')

plot(spatial_aisp) # works

# Same data from .rda file after use usethis::use_data(spatial_aisp,
overwrite = TRUE)

x <- ispdata::spatial_aisp

plot(x) # do not work

Error message:
Error in data.matrix(x) :
  'list' object cannot be coerced to type 'double'


This is happening with all spatial data in the package. I'm using lazydata:
true and have already disabled file compression options, but the problem
persists.

Any ideas?

Scripts can be accessed at https://github.com/igorlaltuf/ispdata

Thanks!
-- 
*Igor Laltuf Marques*
Economist (UFF)
Master in Urban and Regional Planning (IPPUR-UFRJ)
Researcher at ETTERN and CiDMob Laboratories
https://igorlaltuf.github.io/

[[alternative HTML version deleted]]

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