Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What/Where are the attributes of a function object?

Tags:

function

r

By playing around with a function in R, I found out there are more aspects to it than meets the eye.

Consider ths simple function assignment, typed directly in the console:

f <- function(x)x^2

The usual "attributes" of f, in a broad sense, are (i) the list of formal arguments, (ii) the body expression and (iii) the environment that will be the enclosure of the function evaluation frame. They are accessible via:

> formals(f)
$x
> body(f)
x^2
> environment(f)
<environment: R_GlobalEnv>

Moreover, str returns more info attached to f:

> str(f)
function (x)  
 - attr(*, "srcref")=Class 'srcref'  atomic [1:8] 1 6 1 19 6 19 1 1
  .. ..- attr(*, "srcfile")=Classes 'srcfilecopy', 'srcfile' <environment: 0x00000000145a3cc8>

Let's try to reach them:

> attributes(f)
$srcref
function(x)x^2

This is being printed as a text, but it's stored as a numeric vector:

> c(attributes(f)$srcref)
[1]  1  6  1 19  6 19  1  1

And this object also has its own attributes:

> attributes(attributes(f)$srcref)
$srcfile


$class
[1] "srcref"

The first one is an environment, with 3 internal objects:

> mode(attributes(attributes(f)$srcref)$srcfile)
[1] "environment"
> ls(attributes(attributes(f)$srcref)$srcfile)
[1] "filename"      "fixedNewlines" "lines" 
> attributes(attributes(f)$srcref)$srcfile$filename
[1] ""
> attributes(attributes(f)$srcref)$srcfile$fixedNewlines
[1] TRUE
> attributes(attributes(f)$srcref)$srcfile$lines
[1] "f <- function(x)x^2" ""

There you are! This is the string used by R to print attributes(f)$srcref.

So the questions are:

  1. Are there any other objects linked to f? If so, how to reach them?

  2. If we strip f of its attributes, using attributes(f) <- NULL, it doesn't seem to affect the function. Are there any drawbacks of doing this?

like image 356
Ferdinand.kraft Avatar asked Apr 09 '13 18:04

Ferdinand.kraft


People also ask

What are the attributes of function object in Python?

In python, functions too are objects. So they have attributes like other objects. All functions have a built-in attribute __doc__, which returns the doc string defined in the function source code. We can also assign new attributes to them, as well as retrieve the values of those attributes.

What are attributes in a function?

Function attributes are extensions implemented to enhance the portability of programs developed with GNU C. Specifiable attributes for functions provide explicit ways to help the compiler optimize function calls and to instruct it to check more aspects of the code.

What are attributes of an object?

An object characteristic that is always present and occupies storage, even if the attribute does not have a value. In this respect, an attribute is similar to a field in a fixed-length data structure. A distinguishing feature of attributes is that each attribute has its own methods for setting and getting its value.

What forms the attributes of a function?

Like other objects, a function is defined by a set of attributes. It shares many of the attributes of variables, including identifier, title, units, description, and definition, inputs, and outputs. It has a unique attribute, Parameters , which specifies the parameters available to the function.


2 Answers

As far as I know, srcref is the only attribute typically attached to S3 functions. (S4 functions are a different matter, and I wouldn't recommend messing with their sometimes numerous attributes).

The srcref attribute is used for things like enabling printing of comments included in a function's source code, and (for functions that have been sourced in from a file) for setting breakpoints by line number, using utils::findLineNum() and utils::setBreakpoint().

If you don't want your functions to carry such additional baggage, you can turn off recording of srcref by doing options(keep.source=FALSE). From ?options (which also documents the related keep.source.pkgs option):

‘keep.source’: When ‘TRUE’, the source code for functions (newly defined or loaded) is stored internally allowing comments to be kept in the right places. Retrieve the source by printing or using ‘deparse(fn, control = "useSource")’.

Compare:

options(keep.source=TRUE)
f1 <- function(x) {
    ## This function is needlessly commented
    x
}

options(keep.source=FALSE)
f2 <- function(x) {
    ## This one is too
    x
}

length(attributes(f1))
# [1] 1
f1
# function(x) {
#     ## This function is needlessly commented
#     x
# }

length(attributes(f2))
# [1] 0
f2
# function (x) 
# {
#     x
# }
like image 123
Josh O'Brien Avatar answered Sep 20 '22 18:09

Josh O'Brien


I jst figured out an attribute that compiled functions (package compiler) have that is not available with attributes or str. It's the bytecode.

Example:

require(compiler)

f <- function(x){ y <- 0; for(i in 1:length(x)) y <- y + x[i]; y }

g <- cmpfun(f)

The result is:

> print(f, useSource=FALSE)
function (x) 
{
    y <- 0
    for (i in 1:length(x)) y <- y + x[i]
    y
}

> print(g, useSource=FALSE)
function (x) 
{
    y <- 0
    for (i in 1:length(x)) y <- y + x[i]
    y
}
<bytecode: 0x0000000010eb29e0>

However, this doesn't show with normal commands:

> identical(f, g)
[1] TRUE
> identical(f, g, ignore.bytecode=FALSE)
[1] FALSE
> identical(body(f), body(g), ignore.bytecode=FALSE)
[1] TRUE
> identical(attributes(f), attributes(g), ignore.bytecode=FALSE)
[1] TRUE

It seems to be accessible only via .Internal(bodyCode(...)):

> .Internal(bodyCode(f))
{
    y <- 0
    for (i in 1:length(x)) y <- y + x[i]
    y
}

> .Internal(bodyCode(g))
<bytecode: 0x0000000010eb29e0>
like image 39
Ferdinand.kraft Avatar answered Sep 18 '22 18:09

Ferdinand.kraft