2017-10-16 70 views
3

有沒有辦法在調用new.env()時初始化三個名稱「indian」,「pizza」和「ramen」作爲參數?R:設置環境名稱作爲參數new.env()

eats <- new.env(
    indian = "8elements", 
    pizza = "blue line", 
    ramen = "orenchi") 

這將是一個替代方案:

eats <- new.env() 
eats$indian = "8elements" 
eats$pizza = "blue line" 
eats$ramen = "orenchi" 

回答

5

這裏有一些選擇:

1)創建一個列表,並將其轉換爲環境

eats <- list2env(list(
    indian = "8elements", 
    pizza = "blue line", 
    ramen = "orenchi")) 

2)另一個po ssibility是使用local這樣的:

eats <- local({ 
    indian = "8elements" 
    pizza = "blue line" 
    ramen = "orenchi" 
    environment() 
}) 

3)原癌包創建其實際上環境所以原對象:

library(proto) 
eats <- proto(
    indian = "8elements", 
    pizza = "blue line", 
    ramen = "orenchi")