有没有办法在调用new.env()时将“indian”,“pizza”和“ramen”三个名称初始化为参数?
eats <- new.env(
indian = "8elements",
pizza = "blue line",
ramen = "orenchi")
这可以替代:
eats <- new.env()
eats$indian = "8elements"
eats$pizza = "blue line"
eats$ramen = "orenchi"
答案 0 :(得分:5)
以下是一些替代方案:
1)创建一个列表并将其转换为环境
eats <- list2env(list(
indian = "8elements",
pizza = "blue line",
ramen = "orenchi"))
2)另一种可能性是使用local
,如下所示:
eats <- local({
indian = "8elements"
pizza = "blue line"
ramen = "orenchi"
environment()
})
3) proto包创建的proto对象实际上是环境:
library(proto)
eats <- proto(
indian = "8elements",
pizza = "blue line",
ramen = "orenchi")