[R-pkg-devel] set generic and methods when slot is a function

2016-03-28 Thread Glenn Schultz
All,
I am creating a mortgage prepayment model package.  The idea is to create a 
class FRMModelFunction and each slot in the class is a function that may be use 
in the prepayment. So I would like to create a generic and method that will 
allow me to call the slot by name.  However, I think that I am missing 
something in my setup

If I run the below code I can do the following

Omega(Model) which returns the function

Omega <- Omega(Model) which then assigns the function and allows me to pass 
values and get a result from Omega(x = 1)

However I can also get a result by directly accessing the slot

Model@Omega(x=1)

What I want to do is set a generic and method so that I can do the following

Omega(x=1)

The below is close but still not what I would like to have.  Is what  I have 
described possible and if so what have I missed?

Glenn 


setGeneric("Omega", function(object,...) {standardGeneric("Omega")})

 setMethod("Omega", signature = "FRMModelFunction", 
   definition = function(object){object@Omega})

 setClass("FRMModelFunction",
  representation(
Omega = "function"))

 Model <- new("FRMModelFunction",
   Omega = function(x = numeric()) {return(max(0, min(1, x)))}
   )
__
R-package-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-package-devel


Re: [R-pkg-devel] set generic and methods when slot is a function

2016-03-28 Thread Kasper Daniel Hansen
This syntax
  Omega(x = 1)
seems weird.  How do you know that this call should refer back to the Model
object and not some other hypothetical Model2 object?  The code you're
defining should make something like this work
  Omega(Model)(x=1)
where you can see the direct dependence on the Model object.

(unrelated: One experience from Bioconductor, is that it is good to have
explicit constructors for S4 objects instead of using new("...").)

Best,
Kasper

On Mon, Mar 28, 2016 at 2:09 PM, Glenn Schultz  wrote:

> All,
> I am creating a mortgage prepayment model package.  The idea is to create
> a class FRMModelFunction and each slot in the class is a function that may
> be use in the prepayment. So I would like to create a generic and method
> that will allow me to call the slot by name.  However, I think that I am
> missing something in my setup
>
> If I run the below code I can do the following
>
> Omega(Model) which returns the function
>
> Omega <- Omega(Model) which then assigns the function and allows me to
> pass values and get a result from Omega(x = 1)
>
> However I can also get a result by directly accessing the slot
>
> Model@Omega(x=1)
>
> What I want to do is set a generic and method so that I can do the
> following
>
> Omega(x=1)
>
> The below is close but still not what I would like to have.  Is what  I
> have described possible and if so what have I missed?
>
> Glenn
>
>
> setGeneric("Omega", function(object,...) {standardGeneric("Omega")})
>
>  setMethod("Omega", signature = "FRMModelFunction",
>definition = function(object){object@Omega})
>
>  setClass("FRMModelFunction",
>   representation(
> Omega = "function"))
>
>  Model <- new("FRMModelFunction",
>Omega = function(x = numeric()) {return(max(0, min(1, x)))}
>)
> __
> 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


Re: [R-pkg-devel] “unable to find an inherited method” error in choroplethr package

2016-03-28 Thread arilamstein
Unfortunately I am still getting the same error:

install.packages(c("choroplethr", "acs"))
library(acs)
api.key.install("f8b2a6df01479981aef39577b3c4466f5a4c8274")
detach("package:acs", unload=TRUE)

library(choroplethr)
get_state_demographics()
Error in (function (classes, fdef, mtable)  :
unable to find an inherited method for function ‘geography’ for
signature ‘"matrix"’

Someone suggested that I import the acs class in my NAMESPACE. But even
after adding:

importClassesFrom(acs,acs)

I get the same error.


On Sun, Mar 27, 2016 at 11:52 AM, Uwe Ligges <
lig...@statistik.tu-dortmund.de> wrote:

> Has this been resolved now?
>
> I cannot reproduce the problem as I get:
>
> > library(choroplethr)
> >
> > ?get_state_demographics
> starting httpd help server ... done
> >
> > get_state_demographics()
> Error in (function (classes, fdef, mtable)  :
>   unable to find an inherited method for function ‘geography’ for
> signature ‘"logical"’
> In addition: Warning message:
> In acs::acs.fetch(geography = state_geo, table.number = "B03002",  :
>   'key' required to access Census API site for download;
>   See http://www.census.gov/developers/ to request a key
>   and/or use 'key=' (or run 'api.key.install()') to avoid this error.
>
>
> Best,
> Uwe Ligges
>
>
>
> On 25.03.2016 21:55, arilamst...@gmail.com wrote:
>
>> Recently the functionality in the choroplethr
>>  package
>> that depends on the acs
>>  package has
>> stopped working. I've been able to narrow down the cause, but cannot
>> figure
>> out how to fix it. Any help would be appreciated. To reproduce the error
>> you can type:
>>
>> install.packages("choroplethr")
>>
>> library(choroplethr)
>>
>> ?get_state_demographics
>>
>> get_state_demographics()
>>
>> Error in (function (classes, fdef, mtable)  :
>>
>> unable to find an inherited method for function ‘geography’ for signature
>> "matrix"’
>>
>> (Note: you will need to set a census API key for this example to "work".
>> See here
>> <
>> https://cran.r-project.org/web/packages/choroplethr/vignettes/e-mapping-us-census-data.html
>> >
>> for instructions).
>>
>> I believe that this error was introduced by version 2.0 of the acs
>> package,
>> which was just released. I suspect that choroplethr is either not
>> importing
>> something that it should be importing, or acs is not exporting something
>> that it should be exporting. For example, the problem can be fixed by
>> simply loading the acs package first:
>>
>> library(acs)
>>
>> get_state_demographics()
>>
>> 
>>
>> The acs package uses both S3 and S4, which I do not have much experience
>> with. I have been reading Hadley's excellent chapter on Namespaces
>> , and tweaked various import
>> directives in the choroplethr package (the actual code that crashes is
>> here
>> <
>> https://github.com/arilamstein/choroplethr/blob/master/R/get_state_demograhpics.R#L42
>> >).
>> But I am still not sure how to fix this problem.. Any help would be
>> appreciated.
>>
>> [[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

Re: [R-pkg-devel] “unable to find an inherited method” error in choroplethr package

2016-03-28 Thread Uwe Ligges
I just tried your code with acs 2.0 and choroplethr 3.5.1 under R-3.3.0 
alpha and got a result rather than an error...


Are you using a different version of anything?

Best,
Uwe Ligges



On 28.03.2016 22:49, arilamst...@gmail.com wrote:

Unfortunately I am still getting the same error:

install.packages(c("choroplethr", "acs"))
library(acs)
api.key.install("f8b2a6df01479981aef39577b3c4466f5a4c8274")
detach("package:acs", unload=TRUE)

library(choroplethr)
get_state_demographics()
Error in (function (classes, fdef, mtable)  :
 unable to find an inherited method for function ‘geography’
for signature ‘"matrix"’

Someone suggested that I import the acs class in my NAMESPACE. But even
after adding:

importClassesFrom(acs,acs)

I get the same error.


On Sun, Mar 27, 2016 at 11:52 AM, Uwe Ligges
mailto:lig...@statistik.tu-dortmund.de>> wrote:

Has this been resolved now?

I cannot reproduce the problem as I get:

 > library(choroplethr)
 >
 > ?get_state_demographics
starting httpd help server ... done
>
> get_state_demographics()
Error in (function (classes, fdef, mtable)  :
   unable to find an inherited method for function ‘geography’ for
signature ‘"logical"’
In addition: Warning message:
In acs::acs.fetch(geography = state_geo, table.number = "B03002",  :
   'key' required to access Census API site for download;
   See http://www.census.gov/developers/ to request a key
   and/or use 'key=' (or run 'api.key.install()') to avoid this error.


Best,
Uwe Ligges



On 25.03.2016 21 :55, arilamst...@gmail.com
 wrote:

Recently the functionality in the choroplethr

package
that depends on the acs
 package has
stopped working. I've been able to narrow down the cause, but
cannot figure
out how to fix it. Any help would be appreciated. To reproduce
the error
you can type:

install.packages("choroplethr")

library(choroplethr)

?get_state_demographics

get_state_demographics()

Error in (function (classes, fdef, mtable)  :

unable to find an inherited method for function ‘geography’ for
signature
"matrix"’

(Note: you will need to set a census API key for this example to
"work".
See here


for instructions).

I believe that this error was introduced by version 2.0 of the
acs package,
which was just released. I suspect that choroplethr is either
not importing
something that it should be importing, or acs is not exporting
something
that it should be exporting. For example, the problem can be
fixed by
simply loading the acs package first:

library(acs)

get_state_demographics()



The acs package uses both S3 and S4, which I do not have much
experience
with. I have been reading Hadley's excellent chapter on Namespaces
, and tweaked various import
directives in the choroplethr package (the actual code that
crashes is here

).
But I am still not sure how to fix this problem.. Any help would be
appreciated.

 [[alternative HTML version deleted]]

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




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

Re: [R-pkg-devel] “unable to find an inherited method” error in choroplethr package

2016-03-28 Thread arilamstein
I am using R 3.2.4 for mac. Here is my sessionInfo():

> sessionInfo()
R version 3.2.4 (2016-03-10)
Platform: x86_64-apple-darwin13.4.0 (64-bit)
Running under: OS X 10.10.5 (Yosemite)

locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8

attached base packages:
[1] stats graphics  grDevices utils datasets  methods   base

other attached packages:
[1] choroplethr_3.5.1 XML_3.98-1.4  plyr_1.8.3stringr_1.0.0

loaded via a namespace (and not attached):
 [1] Rcpp_0.12.4 RColorBrewer_1.1-2  bitops_1.0-6
 tools_3.2.4
 [5] rpart_4.1-10gtable_0.2.0lattice_0.20-33 png_0.1-7

 [9] WDI_2.4 DBI_0.3.1   mapproj_1.2-4
parallel_3.2.4
[13] proto_0.3-10gridExtra_2.2.1 dplyr_0.4.3
cluster_2.0.3
[17] maps_3.1.0  RgoogleMaps_1.2.0.7 grid_3.2.4
 nnet_7.3-12
[21] R6_2.1.2jpeg_0.1-8  survival_2.38-3
foreign_0.8-66
[25] RJSONIO_1.3-0   sp_1.1-1ggmap_2.6.1
latticeExtra_0.6-28
[29] Formula_1.2-1   reshape2_1.4.1  ggplot2_2.1.0
magrittr_1.5
[33] Hmisc_3.17-2scales_0.4.0acs_2.0
splines_3.2.4
[37] assertthat_0.1  colorspace_1.2-6geosphere_1.5-1
stringi_1.0-1
[41] acepack_1.3-3.3 RCurl_1.95-4.8  munsell_0.4.3
rjson_0.2.15
>

On Mon, Mar 28, 2016 at 2:01 PM, Uwe Ligges  wrote:

> I just tried your code with acs 2.0 and choroplethr 3.5.1 under R-3.3.0
> alpha and got a result rather than an error...
>
> Are you using a different version of anything?
>
> Best,
> Uwe Ligges
>
>
>
> On 28.03.2016 22:49, arilamst...@gmail.com wrote:
>
>> Unfortunately I am still getting the same error:
>>
>> install.packages(c("choroplethr", "acs"))
>> library(acs)
>> api.key.install("f8b2a6df01479981aef39577b3c4466f5a4c8274")
>> detach("package:acs", unload=TRUE)
>>
>> library(choroplethr)
>> get_state_demographics()
>> Error in (function (classes, fdef, mtable)  :
>>  unable to find an inherited method for function ‘geography’
>> for signature ‘"matrix"’
>>
>> Someone suggested that I import the acs class in my NAMESPACE. But even
>> after adding:
>>
>> importClassesFrom(acs,acs)
>>
>> I get the same error.
>>
>>
>> On Sun, Mar 27, 2016 at 11:52 AM, Uwe Ligges
>> > > wrote:
>>
>> Has this been resolved now?
>>
>> I cannot reproduce the problem as I get:
>>
>>  > library(choroplethr)
>>  >
>>  > ?get_state_demographics
>> starting httpd help server ... done
>> >
>> > get_state_demographics()
>> Error in (function (classes, fdef, mtable)  :
>>unable to find an inherited method for function ‘geography’ for
>> signature ‘"logical"’
>> In addition: Warning message:
>> In acs::acs.fetch(geography = state_geo, table.number = "B03002",  :
>>'key' required to access Census API site for download;
>>See http://www.census.gov/developers/ to request a key
>>and/or use 'key=' (or run 'api.key.install()') to avoid this error.
>>
>>
>> Best,
>> Uwe Ligges
>>
>>
>>
>> On 25.03.2016 21 :55, arilamst...@gmail.com
>>
>>  wrote:
>>
>> Recently the functionality in the choroplethr
>> 
>> package
>> that depends on the acs
>>  package
>> has
>> stopped working. I've been able to narrow down the cause, but
>> cannot figure
>> out how to fix it. Any help would be appreciated. To reproduce
>> the error
>> you can type:
>>
>> install.packages("choroplethr")
>>
>> library(choroplethr)
>>
>> ?get_state_demographics
>>
>> get_state_demographics()
>>
>> Error in (function (classes, fdef, mtable)  :
>>
>> unable to find an inherited method for function ‘geography’ for
>> signature
>> "matrix"’
>>
>> (Note: you will need to set a census API key for this example to
>> "work".
>> See here
>> <
>> https://cran.r-project.org/web/packages/choroplethr/vignettes/e-mapping-us-census-data.html
>> >
>> for instructions).
>>
>> I believe that this error was introduced by version 2.0 of the
>> acs package,
>> which was just released. I suspect that choroplethr is either
>> not importing
>> something that it should be importing, or acs is not exporting
>> something
>> that it should be exporting. For example, the problem can be
>> fixed by
>> simply loading the acs package first:
>>
>> library(acs)
>>
>> get_state_demographics()
>>
>> 
>>
>> The acs package uses both S3 and S4, which I do not have much
>> experience
>> with. I have been reading Hadley's excellent chapter on Namespaces
>> 

Re: [R-pkg-devel] set generic and methods when slot is a function

2016-03-28 Thread Glenn Schultz
Kasper,

Thanks! I that was the clue I was looking for.  Simple change to the code makes 
it work perfectly as you describe.  I also have constructor function for Model 
beyond the minimal example.  I also like you reference to Bioconductor as that 
is the model I am trying to follow.

Glenn

 setGeneric("Omega", function(object) {standardGeneric("Omega")})
  
  setMethod("Omega", signature = "FRMModelFunction", 
definition = function(object){object@Omega})
  
  setClass("FRMModelFunction",
   representation(
 Omega = "function"))

  Model <- new("FRMModelFunction",
Omega = function(x = numeric()) {return(max(0, min(1, x)))}
)
  


> On Mar 28, 2016, at 2:14 PM, Kasper Daniel Hansen 
>  wrote:
> 
> This syntax 
>   Omega(x = 1)
> seems weird.  How do you know that this call should refer back to the Model 
> object and not some other hypothetical Model2 object?  The code you're 
> defining should make something like this work
>   Omega(Model)(x=1)
> where you can see the direct dependence on the Model object.
> 
> (unrelated: One experience from Bioconductor, is that it is good to have 
> explicit constructors for S4 objects instead of using new("...").)
> 
> Best,
> Kasper
> 
> On Mon, Mar 28, 2016 at 2:09 PM, Glenn Schultz  > wrote:
> All,
> I am creating a mortgage prepayment model package.  The idea is to create a 
> class FRMModelFunction and each slot in the class is a function that may be 
> use in the prepayment. So I would like to create a generic and method that 
> will allow me to call the slot by name.  However, I think that I am missing 
> something in my setup
> 
> If I run the below code I can do the following
> 
> Omega(Model) which returns the function
> 
> Omega <- Omega(Model) which then assigns the function and allows me to pass 
> values and get a result from Omega(x = 1)
> 
> However I can also get a result by directly accessing the slot
> 
> Model@Omega(x=1)
> 
> What I want to do is set a generic and method so that I can do the following
> 
> Omega(x=1)
> 
> The below is close but still not what I would like to have.  Is what  I have 
> described possible and if so what have I missed?
> 
> Glenn
> 
> 
> setGeneric("Omega", function(object,...) {standardGeneric("Omega")})
> 
>  setMethod("Omega", signature = "FRMModelFunction",
>definition = function(object){object@Omega})
> 
>  setClass("FRMModelFunction",
>   representation(
> Omega = "function"))
> 
>  Model <- new("FRMModelFunction",
>Omega = function(x = numeric()) {return(max(0, min(1, x)))}
>)
> __
> 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