[Rd] Defining a method that behaves like '$'?

Romain Francois romain.francois at dbmail.com
Fri Jul 9 14:27:53 CEST 2010


Le 09/07/10 14:18, Renaud Gaujoux a écrit :
>
> Hi,
>
> is there a way to define a method say '$$' that would behave like '$'
> and allow calls like 'a$$name'?
> Thanks.
>
> Renaud

No. This is not grammatically valid syntax:

 > parse( text = 'a$$name' )
Erreur dans parse(text = "a$$name") : '$' inattendu(e) dans "a$$"


But you can define custom methods for $.

setClass( "Foo", representation( n = "integer" ) )
setMethod( "$", "Foo", function(x, name ){
	function( ) rnorm( x at n )
} )
foo <- new( "Foo", n = 10L )
foo$bla( )


Romain

-- 
Romain Francois
Professional R Enthusiast
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr
|- http://bit.ly/aJHNLV : Rmetrics slides
|- http://bit.ly/98Uf7u : Rcpp 0.8.1
`- http://bit.ly/c6YnCi : graph gallery collage



More information about the R-devel mailing list