2017-06-30 48 views
0

我正在編寫圍繞rBayesianOptimizationcaret R包的包裝函數。下面描述的問題是主包裝函數內部步驟的一部分。我需要將特定模型的超參數(以下代表input)傳遞給將在高斯過程模型中採樣的函數。超參數當前存儲在一個字符向量中。R函數使用字符向量動態生成另一個R函數

有沒有辦法使用字符向量在R中動態生成函數?我需要一個實際上創建另一個函數的函數。例如,

# start with this... 
input <- c("a","b") 

# ...and an intermediate function generates this 
test_function <- function(a=a,b=b) { 
    d <- data.frame(a=a,b=b) 
} 

此外,解決方案需要能夠處理各種長度和內容的輸入。所以像這樣的東西也應該工作,

# start with this... 
input <- c("c","d","e","f") 

# ...and an intermediate function generates this 
test_function <- function(c=c,d=d,e=e,f=f) { 
    d <- data.frame(c=c,d=d,e=e,f=f) 
} 

這是一個玩具的例子,但它足以回答我的問題。

回答

1

如果我理解你的問題吧,你要尋找的是創建了一個函數的函數,其中第二個函數接受參數與名稱爲input(給第一個功能)和價值,也許數字,由這些論點標記。如果這是正確的,那麼這個怎麼樣?

create_fun <- function(input) { 
    funargs <- paste(input, collapse=', ') 
    dfargs <- paste0(input, '=', input, collapse=', ') 
    funstr <- paste0(
    sprintf("function(%s) {", funargs), 
    sprintf("data.frame(%s)", dfargs), 
    "}") 
    eval(parse(text=funstr)) 
} 
# example applications: 
create_fun(c('a','b'))(a=1, b=8) 
## a b 
## 1 1 8 
create_fun(c('x','y','z'))(y=1, z=8, x=4) 
## x y z 
## 1 4 1 8 
+0

這是完美的。我更新了我的原始問題以澄清 –

1

這是您發佈的簡單示例的簡單答案。這聽起來像你真正的工作可能需要更復雜的方法,但這可能有幫助。我相信還有其他的方法可以更快地做到這一點。

input <- c("c","d","e","f") 

test_function <- function(inputVec) { 
    df <- data.frame(inputVec[1], stringsAsFactors = FALSE) 
    names(df)[1] <- inputVec[1] 
    if(length(inputVec) == 1) { 
    return(df) 
    } 
    else if(length(inputVec) > 1) { 
    for(i in seq(2, length(inputVec), 1)) { 
     dfNew <- data.frame(inputVec[i], stringsAsFactors = FALSE) 
     names(dfNew) <- inputVec[i] 
     df <- dplyr::bind_cols(df, dfNew) 
    } 
    return(df) 
    } 
} 

testDF <- test_function(inputVec = input) 

print(testDF) 
## c d e f 
##1 c d e f 
+0

這是做我所問的(因此+1),但我意識到我沒有問我在原來的問題到底是什麼意思。 @pangia的迴應是我想到的。我編輯了我的問題來反映這一點。 –