昨日は、例えば、静的関数と変数を作成することができますどのように(ローカル)ビルVenablesから学んだlocal()はRの他のアプローチとどのように違いますか?
コマンドプロンプトから次のように振る舞うexample <- local({
hidden.x <- "You can't see me!"
hidden.fn <- function(){
cat("\"hidden.fn()\"")
}
function(){
cat("You can see and call example()\n")
cat("but you can't see hidden.x\n")
cat("and you can't call ")
hidden.fn()
cat("\n")
}
})
:私はこれを見てきました
> ls()
[1] "example"
> example()
You can see and call example()
but you can't see hidden.x
and you can't call "hidden.fn()"
> hidden.x
Error: object 'hidden.x' not found
> hidden.fn()
Error: could not find function "hidden.fn"
異なるアプローチが採用されたStatic Variables in Rで議論されている種類のもの。
この2つの方法の長所と短所は何ですか?
'local'が便利なもう一つのパターンはメモです。 The R Infernoのどこかに例があります。 – otsaw
クローラがシングルトンであるというあなたの考えは面白いです。なぜなら、ローカルの代わりに、無名の関数を引数なしで即座に評価することがあるからです。ファクトリパターンはベースURLに対してクロージャを使用するかもしれません。 – hadley