1
我有一個函數,我想包圍另一個函數,通過參數作爲...
參數參數。我有麻煩學習如何構建底層的函數調用lazyeval
這裏有一個體面的MWE,點參數的非標準評估
library(dplyr)
pythag <- function(a, b){
sqrt(a^2 + b^2)
}
pythag_wrapper <- function(data, ...){
dplyr::mutate_(data,
root = lazyeval::interp(~pythag(x), x = ...)
)
}
在此我pythag_wrapper
會做一些額外的數據改寫(munging)。在我的情況下pythag
有兩個以上的參數。該功能工作得很好,並按預期。
test_data <- dplyr::data_frame(a = runif(10), b = runif(10), c = runif(10))
test_data %>%
dplyr::mutate(
root = pythag(a = b, b = c)
)
## # A tibble: 10 × 4
## a b c root
## <dbl> <dbl> <dbl> <dbl>
## 1 0.19805337 0.05567241 0.9956758 0.9972311
## 2 0.22642799 0.18871552 0.8690659 0.8893195
## 3 0.09352032 0.57328658 0.7475573 0.9420719
## 4 0.40589832 0.71270806 0.8014196 1.0724860
## 5 0.35896302 0.85889027 0.8197176 1.1872782
## 6 0.66409819 0.02206298 0.1304790 0.1323312
## 7 0.45102742 0.76048535 0.5501899 0.9386410
## 8 0.48249177 0.93670363 0.8280114 1.2502066
## 9 0.05545819 0.12281684 0.9219704 0.9301148
## 10 0.47588862 0.40196106 0.0192433 0.4024214
我已經試過各種lazyeval::interp
,lazy_eval::lazy_dots
等的組合,但我不明白到底什麼是應該發生的,更不用說如何解決我的問題。
pythag_wrapper(test_data, a = "a", b = "b")
## Error: object 'x' not found
這讓我遠一點(和解決MWE),但我的實際情況UseMethod返回'錯誤(「as.lazy_dots」): 對於「function」類的對象應用'as.lazy_dots'沒有適用的方法,它仍然很呆板。 – gregmacfarlane
你能解釋爲什麼你試圖重寫函數調用嗎?這是否是最簡單的方法去解決這類問題? – gregmacfarlane