[R] Seeking help to define method for show() for an S4 object

Joshua Wiley jwiley.psych at gmail.com
Mon Jun 6 19:23:37 CEST 2011


Hi,

Take a look at ?readLines  Also, you should use function(object) not
function(x) because of how the generic is defined.  Below is an
example.

Cheers,

Josh

setClass("MyClass", sealed = FALSE, representation(
  Slot1 = "vector",
  Slot2 = "vector"))

setMethod(f = "show", signature = "MyClass",
  definition = function(object) {
    cat("These are the values.  Please press enter to see the values.\n")
    readLines(n = 1)
    cat(object at Slot1)
  })

x <- new("MyClass", Slot1 = 1:3, Slot2 = 4:7)
x

On Mon, Jun 6, 2011 at 10:19 AM, Bogaso Christofer
<bogaso.christofer at gmail.com> wrote:
> Dear all, I have created a new S4 class with name "MyClass". Please see
> below for it's definition. Now I want to create a
> method for the show() function for this class. In defining this method, what
> I want is, once user would like see an object of this class
> some values will be displayed. Then R will ask for **press Enter**. Once
> user presses enter then, remaining values will be
> displayed. In the following example, I try to explain this concept.
>
>
>> setClass("MyClass", sealed = FALSE, representation(Slot1 = "vector", Slot2
> = "vector"))
>
> [1] "MyClass"
>
>>
>
>> setMethod("show", "MyClass", definition = function(x) {
>
> +         cat("These are the values. Please press enter to see the
> values.\n")
>
> +         ####### User will presss the Enter ##########
>
> +         ####### Then only following figures will be visible #########
>
> +         cat(x at Slot1)
>
> +        } )
>
> [1] "show"
>
> Warning message:
>
> For function "show", signature "MyClass": argument in method definition
> changed from (x) to (object)
>
>> new("MyClass", Slot1 = 1:3, Slot2 = 4:7)
>
> These are the values. Please press enter to see the values.
>
> 1 2 3>
>
>
>
>
>
>
> Can somebody guide me how I can achieve that?
>
>
> Thanks,
>
>
>
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



-- 
Joshua Wiley
Ph.D. Student, Health Psychology
University of California, Los Angeles
http://www.joshuawiley.com/



More information about the R-help mailing list