假設我在環境e中定義了兩個類似的大對象x,y(datatable)。 我想用類似的方法改變x或y的很大一部分,而不用在f的執行環境中創建x或y的副本。例如:將值分配給R中另一個環境中的對象的一部分
e <- new.env()
e$x <- c(1,2,3) # imagine this to be BIG (ie. dataframe with 200k vars each 500k rows)
e$y <- c(4,5,6) # same here
e$v <- 2 # minor variables
f <- function(var_str, env, input){
# do some computation on parts of var_str which is either "x" or "y"
# and store these right back into e$x or e$y, respectively.
# ie
str <- paste0(var_str,"[2:3] <- (",var_str,"[2:3])^2 + rep(v,2) + ", deparse(input1),"^3/c(100,101)")
eval(parse(text=str), envir= e)
# this does work but I can image there is an easier/more elegant way
# of doing this.
}
我想定義在全球環境的功能,該功能適用於E $ x和E $ y隨輸入不同的變量。 IE瀏覽器。執行
f("x", e, c(1,2))
f("y", e, c(3,4))
有沒有人有一個優雅的解決方案。
也許你可以得到你想要使用R6類:https://cran.r-project.org/web/packages/R6/vignettes/Introduction.html –