Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Suppress output from attr() in function result

Tags:

r

attributes

In the source code for data.frame, the last three lines of code set the attributes and return the result.

    ...
    attr(value, "row.names") <- row.names
    attr(value, "class") <- "data.frame"
    value
}

In a function I wrote, the result is a named list created by lapply. Before I set any attributes in the function body, result is as follows.

> x <- data.frame(a = 1:5, b = letters[1:5])    
> (g <- grep.dataframe("a|c", x))
# ...    
# $b
#   value row
# 1     a   1
# 2     c   3
> attributes(g)  # I want "list" in here...
# $names
# [1] "a" "b"

I'd like "class" to be included in the attributes list, so I add attr(res, "class") <- "list" (res is the final result) just before res. "class" now shows up in the attributes list. However,it also prints out with the result of the function, which I don't want. I tried wrapping it with invisible, but that didn't work.

Why do the manually assigned attributes print with the function result, but are suppressed in a new data frame I create?

> (h <- grep.dataframe("a|c", x))
# ...    
# $b
#   value row
# 1     a   1
# 2     c   3

# attr(,"class")  # ...This prints with the result. I don't want that.
# [1] "list"
> attributes(h)   # ...But I want these attributes
# $names
# [1] "a" "b"

# $class
# [1] "list"
like image 324
Rich Scriven Avatar asked Oct 21 '22 07:10

Rich Scriven


1 Answers

The ?class documentation offers some pointers:

Many R objects have a class attribute, a character vector giving the names of the classes from which the object inherits. If the object does not have a class attribute, it has an implicit class, "matrix", "array" or the result of mode(x) (except that integer vectors have implicit class "integer"). (Functions oldClass and oldClass<- get and set the attribute, which can also be done directly.)

When a generic function fun is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function is found, a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used (if it exists). If there is no class attribute, the implicit class is tried, then the default method.

From that and running a few simple tests, I gather that:

  • a list is one of these implicit classes: see attributes(list(1)), typeof(list(1))
  • when print is called on a list, it is using print.default
  • print.default prints the attributes of an object

So you could define a print.list that will handle your special case:

 print.list <- function(x, ...) {
    if (is.list(x)) attr(x, "class") <- NULL
    print.default(x, ...)
 }

res <- list(1)
attr(res, "class") <- "list"
res
# [[1]]
# [1] 1

attributes(res)
# $class
# [1] "list"
like image 181
flodel Avatar answered Nov 04 '22 02:11

flodel