簡體   English   中英

R:為match.call輸出添加一個參數

[英]R: adding an argument to match.call output

假設我在R中有以下功能:

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而沒有成功。 有什么建議么?

您實際上可以將調用視為列表並為其指定一個命名參數:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM