18

R で関数をいじってみると、目に見える以上の側面があることがわかりました。

コンソールに直接入力された単純な関数の割り当てを考えてみましょう。

f <- function(x)x^2

の通常の「属性」はf、広い意味で、(i) 形式引数のリスト、(ii) 本体の式、および (iii) 関数評価フレームの囲みとなる環境です。次の方法でアクセスできます。

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

さらに、strに添付された詳細情報を返します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>

それらに到達しようとしましょう:

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

これはテキストとして出力されていますが、数値ベクトルとして格納されています。

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

また、このオブジェクトには独自の属性もあります。

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


$class
[1] "srcref"

最初のものは環境で、3 つの内部オブジェクトがあります。

> 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" ""

そこにいます!これは、R が print に使用する文字列attributes(f)$srcrefです。

質問は次のとおりです。

  1. にリンクされている他のオブジェクトはありますfか? もしそうなら、彼らに到達する方法は?

  2. fを使用してその属性を取り除いattributes(f) <- NULLても、関数には影響しないようです。これを行うことの欠点はありますか?

4

2 に答える 2

15

私の知る限り、srcref通常 S3 関数に関連付けられる唯一の属性です。(S4関数は別の問題であり、時には多数の属性をいじることはお勧めしません).

このsrcref属性は、関数のソース コードに含まれるコメントの出力を有効にしたり、(ファイルから取得された関数の場合) と を使用して行番号でブレークポイントを設定したりするために使用されutils::findLineNum()ますutils::setBreakpoint()

srcref関数にそのような追加の荷物を持たせたくない場合は、 を実行しての記録をオフにすることができますoptions(keep.source=FALSE)。から?options(関連するオプションも文書化していkeep.source.pkgsます):

'keep.source': 'TRUE' の場合、(新しく定義された、または読み込まれた) 関数のソース コードが内部に保存され、コメントを適切な場所に保持できます。出力するか、「deparse(fn, control = "useSource")」を使用してソースを取得します。

比較:

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
# }
于 2013-04-09T20:51:39.193 に答える
4

コンパイルされた関数 ( package compiler) がattributesorでは利用できない属性を見つけましたstr。ですbytecode

例:

require(compiler)

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

g <- cmpfun(f)

結果は次のとおりです。

> 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>

ただし、これは通常のコマンドでは表示されません。

> 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

経由でのみアクセスできるようです.Internal(bodyCode(...))

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

> .Internal(bodyCode(g))
<bytecode: 0x0000000010eb29e0>
于 2013-04-16T16:24:22.487 に答える