user6335904
user6335904

Reputation:

How do I print a list using a print function I defined earlier in the script?

[homework question]

I'm creating an S3 constructor/object/script. I have a list in my script that needs to be printed like:

ID: 9876  Name: Virgil  Gender: M
LDL: 248* HDL: 45+  Triglycerides: 148

I defined a print function earlier.

print.ChlorReads <- function(theObject) {
  cat("ID: ", theObject$id, " Name: ", theObject$name, " Gender: ", theObject$gender,
      "\n", "LDL: ", theObject$ldl, " HDL: ", theObject$hdl, " Triglycerides: ", theObject$trigl)
}

How do I properly use that script so that it prints each item in the list in the format specified above?

I can get it to print the list via print(lst), but, the list looks like this:

[[1]]
$id
[1] 1111

$name
[1] "Charlene"

$gender
[1] "F"

$ldl
[1] 111

$hdl
[1] 81

$trigl
[1] 136

attr(,"class")
[1] "Patient"

[[2]]
$id
[1] 2222

$name
[1] "Charles"

$gender
[1] "M"

$ldl
[1] 141

$hdl
[1] 78

$trigl
[1] 123

attr(,"class")
[1] "Patient"

[[3]]
$id
[1] 3333

$name
[1] "Kate"

$gender
[1] "F"

$ldl
[1] 121

$hdl
[1] 63

$trigl
[1] 141

attr(,"class")
[1] "Patient"

[[4]]
$id
[1] 4444

$name
[1] "Alice"

$gender
[1] "F"

$ldl
[1] 97

$hdl
[1] 79

$trigl
[1] 111

attr(,"class")
[1] "Patient"

[[5]]
$id
[1] 5555

$name
[1] "Jason"

$gender
[1] "M"

$ldl
[1] 121

$hdl
[1] 85

$trigl
[1] 123

attr(,"class")
[1] "Patient"

[[6]]
$id
[1] 6666

$name
[1] "Spencer"

$gender
[1] "M"

$ldl
[1] 111

$hdl
[1] 81

$trigl
[1] 127

attr(,"class")
[1] "Patient"

[[7]]
$id
[1] 7777

$name
[1] "Mary"

$gender
[1] "F"

$ldl
[1] 108

$hdl
[1] 86

$trigl
[1] 120

attr(,"class")
[1] "Patient"

[[8]]
$id
[1] 8888

$name
[1] "Jarrod"

$gender
[1] "M"

$ldl
[1] 252

$hdl
[1] 38

$trigl
[1] 411

attr(,"class")
[1] "Patient"

[[9]]
$id
[1] 9999

$name
[1] "Cary"

$gender
[1] "M"

$ldl
[1] 121

$hdl
[1] 47

$trigl
[1] 139

attr(,"class")
[1] "Patient"

[[10]]
$id
[1] 1234

$name
[1] "Michael"

$gender
[1] "M"

$ldl
[1] 123

$hdl
[1] 49

$trigl
[1] 134

attr(,"class")
[1] "Patient"

[[11]]
$id
[1] 2345

$name
[1] "Charlotte"

$gender
[1] "F"

$ldl
[1] 135

$hdl
[1] 69

$trigl
[1] 361

attr(,"class")
[1] "Patient"

[[12]]
$id
[1] 3456

$name
[1] "Blake"

$gender
[1] "F"

$ldl
[1] 95

$hdl
[1] 57

$trigl
[1] 126

attr(,"class")
[1] "Patient"

Edit: Here's the output of dput(lst), as asked for in the comments:

list(structure(list(id = 1111L, name = "Charlene", gender = "F", 
    ldl = 111L, hdl = 81L, trigl = 136L), .Names = c("id", "name", 
"gender", "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
    id = 2222L, name = "Charles", gender = "M", ldl = 141L, hdl = 78L, 
    trigl = 123L), .Names = c("id", "name", "gender", "ldl", 
"hdl", "trigl"), class = "Patient"), structure(list(id = 3333L, 
    name = "Kate", gender = "F", ldl = 121L, hdl = 63L, trigl = 141L), .Names = c("id", 
"name", "gender", "ldl", "hdl", "trigl"), class = "Patient"), 
    structure(list(id = 4444L, name = "Alice", gender = "F", 
        ldl = 97L, hdl = 79L, trigl = 111L), .Names = c("id", 
    "name", "gender", "ldl", "hdl", "trigl"), class = "Patient"), 
    structure(list(id = 5555L, name = "Jason", gender = "M", 
        ldl = 121L, hdl = 85L, trigl = 123L), .Names = c("id", 
    "name", "gender", "ldl", "hdl", "trigl"), class = "Patient"), 
    structure(list(id = 6666L, name = "Spencer", gender = "M", 
        ldl = 111L, hdl = 81L, trigl = 127L), .Names = c("id", 
    "name", "gender", "ldl", "hdl", "trigl"), class = "Patient"), 
    structure(list(id = 7777L, name = "Mary", gender = "F", ldl = 108L, 
        hdl = 86L, trigl = 120L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
        id = 8888L, name = "Jarrod", gender = "M", ldl = 252L, 
        hdl = 38L, trigl = 411L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
        id = 9999L, name = "Cary", gender = "M", ldl = 121L, 
        hdl = 47L, trigl = 139L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
        id = 1234L, name = "Michael", gender = "M", ldl = 123L, 
        hdl = 49L, trigl = 134L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
        id = 2345L, name = "Charlotte", gender = "F", ldl = 135L, 
        hdl = 69L, trigl = 361L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"), structure(list(
        id = 3456L, name = "Blake", gender = "F", ldl = 95L, 
        hdl = 57L, trigl = 126L), .Names = c("id", "name", "gender", 
    "ldl", "hdl", "trigl"), class = "Patient"))

Upvotes: 0

Views: 95

Answers (2)

coatless
coatless

Reputation: 20746

Given the example of a list with patient entries, we should use two different print methods. One print method that deals with the list structure by dispatching each elements print command to another that deals with the class of patient.

To conform with print.*() S3 methods - a.k.a pass a CRAN check on package submit - you must have the name of the object as x and then ...

# Prints the patient information (one item in the list)
print.Patient = function(x, ...){

  cat("ID: ", x$id, " Name: ", x$name, " Gender: ", x$gender,
      "\nLDL: ", x$ldl, " HDL: ", x$hdl, " Triglycerides: ", x$trigl,"\n", sep="")

}

# Print function defined for list type
print.ChlorReads = function(x, ...) {

  # Process each item in the list
  for(i in seq_along(x)){

     # Call the method to print patient info
     print.Patient(x[[i]]) 

  }
}

Now, the next bit you need to do is define the class of lst to be of ChlorReads by:

class(lst) = c('ChlorReads', class(lst))

The inclusion of the present class via class(lst) enables a fallback to a generic S3 supported type.

Thus, after sourcing the print statements and the above sample object given in the opening post, I get the desired results with:

class(lst) = c('ChlorReads', class(lst))

print(lst)

This gives:

ID: 1111 Name: Charlene Gender: F
LDL: 111 HDL: 81 Triglycerides: 136
ID: 2222 Name: Charles Gender: M
LDL: 141 HDL: 78 Triglycerides: 123
ID: 3333 Name: Kate Gender: F
LDL: 121 HDL: 63 Triglycerides: 141
ID: 4444 Name: Alice Gender: F
LDL: 97 HDL: 79 Triglycerides: 111
ID: 5555 Name: Jason Gender: M
LDL: 121 HDL: 85 Triglycerides: 123
ID: 6666 Name: Spencer Gender: M
LDL: 111 HDL: 81 Triglycerides: 127
ID: 7777 Name: Mary Gender: F
LDL: 108 HDL: 86 Triglycerides: 120
ID: 8888 Name: Jarrod Gender: M
LDL: 252 HDL: 38 Triglycerides: 411
ID: 9999 Name: Cary Gender: M
LDL: 121 HDL: 47 Triglycerides: 139
ID: 1234 Name: Michael Gender: M
LDL: 123 HDL: 49 Triglycerides: 134
ID: 2345 Name: Charlotte Gender: F
LDL: 135 HDL: 69 Triglycerides: 361
ID: 3456 Name: Blake Gender: F
LDL: 95 HDL: 57 Triglycerides: 126

Upvotes: 0

RHertel
RHertel

Reputation: 23818

You may need to loop over the length of the list that you are passing as a parameter.

Try this small modification:

print.ChlorReads <- function(theObject) {
for (i in 1:length(theObject)){
  cat("List entry: ", i, "\nID: ", theObject[[i]]$id, " Name: ", theObject[[i]]$name, " Gender: ", theObject[[i]]$gender,
      "\n", "LDL: ", theObject[[i]]$ldl, " HDL: ", theObject[[i]]$hdl, " Triglycerides: ", theObject[[i]]$trigl,"\n")
 }
}

Upvotes: 1

Related Questions