2016-05-09 4 views
2

引数に3つのドット構造を使用する2つの関数があるとします。リスト()をRの省略記号に変換するには?

私は最初の関数の省略記号を取得し、2番目の関数の新しい引数リストを作成したいと考えています。

新しく作成したリストを2番目の関数に渡すにはどうすればよいですか?ここで

はサンプルコードです:

first.function <- function(..., name) { 
    argument.list <- list(...) 

    new.args <- list() 
    for (i in 1:length(argument.list)) { 
    new.args[[i]] <- argument.list[[i]]^2 
    } 
    new.args[[length(new.args) + 1]] <- name 

    do.call(second.function, new.args) 
} 

second.function <- function(..., name) { 
    print(paste("This is the name:", name)) 
    print(paste("These are the arguments:", ...)) 
} 

first.function(1, 2, 3, name = "Test") 

私はdo.callてみましたが、私は、エラー・メッセージがあります:ペーストで

エラー( "これは名前です:"、名前を):引数「名前は」 既定値はありませんし、不足している

第二の機能は、エリーとは別の引数としてname引数を認識しないためですpsisの議論。

期待される結果である:

これは名前である:試験

これらの引数:1、4、9

答えて

5

ちょうどパラメータ名:

first.function <- function(..., name) { 
    argument.list <- list(...) 

    new.args <- lapply(argument.list, `^`, 2) 
    new.args[["name"]] <- name 

    do.call(second.function, new.args) 
} 

first.function(1, 2, 3, name = "Test") 
#[1] "This is the name: Test" 
#[1] "These are the arguments: 1 4 9" 

ここに言語定義の関連セクションへのリンクがあります:4.3.2 Argument matching

関連する問題