簡體   English   中英

如何使用該調用的另一個參數以及在R中的函數環境內部創建的對象求值該函數調用的一個參數?

[英]How to evaluate one argument of a function call using another argument of that call, and an object created inside the function environment in R?

我正在嘗試編寫一個將表達式作為參數的函數,然后在1)函數的另一個參數和2)在函數內部創建的對象的上下文中評估該表達式。

我在使環境正常工作時遇到了一些問題。 有誰知道如何做到這一點?

myfun <- function(es = .5, model = es * x[, 1]){
  x <- matrix(rnorm(300), ncol = 3)
  mu <- eval(model)
  mu
}

myfun(es = .8, model = es * x[, 1] + es * x[, 1]^2 + es * x[, 1]^3)

錯誤結果:eval(model)中的錯誤:找不到對象'es'

有什么建議么?

嘗試substitute

myfun <- function(es = .5, model = es * x[, 1]){
    x <- matrix(rnorm(300), ncol = 3)
    mu <- eval(substitute(model))
    mu
}

暫無
暫無

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

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