2017-07-05 29 views
1

說我有下面的函數在R:R:添加一個參數match.call輸出

tst <- function(x,y){ 
    foo <- match.call() 
    print(foo) 
} 

tst(4,5) 

這給出tst(x = 4, y = 5)。真棒。現在,我想添加一個參數。

tst <- function(x,y){ 
    foo <- match.call() 
    foo[[length(foo)+1]] <- 6 
    print(foo) 
} 

tst(4,5) 

這打印tst(x = 4, y = 5, 6),這是偉大的。但是,我需要添加一個參數名稱,以便該函數知道如何處理它。例如,我希望它是tst(x = 4, y = 5, z = 6)。我只是嘗試foo[[length(foo)+1]] <- "z = 6",但這顯然不起作用。我也玩過parseeval沒有成功。有什麼建議麼?

+0

爲什麼你需要一個'call'輸出?你打算如何使用它?首先獲得名單不是更好嗎? –

+0

它源於[this](https://stackoverflow.com/questions/44912496/geom-smooth-with-facet-grid-and-different-fitting-functions/44913300#44913300)的問題。我想修改答案以包含'nls'。與給定答案中的'lm'和'loess'不同,'nls'需要額外的參數,比如'start'。所以,在mysmooth函數中,我想添加這些參數。 – Lyngbakr

回答

2

實際上,你可以像對待列表中的電話,並給它命名參數​​:

> tst = 
function(x,y){ 
foo = match.call() 
foo[["this"]] ="that" 
print(foo) 
} 
> tst(x=2,y=3) 
tst(x = 2, y = 3, this = "that")