简体   繁体   English

如何在.GlobalEnv中的R中提供用户定义函数的输出作为data.frame

[英]how to provide output of user-defined-function as data.frame in R in .GlobalEnv

I have created a function 'mywsobj' which takes one input & 2 output 我创建了一个函数“ mywsobj”,该函数需要一个输入和两个输出

user input: environment 用户输入:环境

output1: a data.frame name "wsobj" (data of list of objects,their classes,their memory usage) output to console output2: a barplot of list of objects and their memory usage based. output1:一个数据框名称“ wsobj”(对象列表,其类,其内存使用情况的数据)输出到控制台output2:对象列表及其基于内存使用情况的barplot。

So far all ok, 到目前为止一切正常,

My question1: but How to save that "wsobj" data.frame from inside the function in that user-specified input environment or at least in the .GlobalEnv ? 我的问题1:但是如何在用户指定的输入环境或至少在.GlobalEnv中从函数内部保存“ wsobj” data.frame? I tried reading things like: use of <<, use of pos/parent.environment etc. but things are so far beyond me. 我尝试阅读以下内容:使用<<,使用pos / parent.environment等,但是事情远远超出了我。

My question2: Is it possible to specify project-specific/user-specfic environment in R/specially in Rstudio server ? 我的问题2:是否可以在R /特别是在Rstudio服务器中指定特定于项目/特定于用户的环境?

Though my code here may not matter still it is below: 虽然我的代码可能没关系,但仍在下面:

    # creating SOME data
dfAtoZ<- data.frame(LETTERS) 
df1to1Cr <- data.frame(1:10000000)
vec1to1Cr <- as.vector(1:10000000)
mat1to1Cr <- as.matrix(1:10000000)
set.seed<-10
randvec<-runif(1000,min=-100,max=100)
# creating MY function
mywsobj<-function(myenvironmentName)
{#step1 creating vector of object names
wslist <- vector(length=length(ls(myenvironmentName)))
for(i in 1:length(ls(myenvironmentName)))
{wslist[i]<-ls(myenvironmentName)[i]}
# wslist<-cbind(unlist(ls()))
#step2 creating vector of object classes
wsclass <- vector(length=length(wslist))
wsmemKb <- vector(mode="numeric",length=length(wslist))
for(i in 1:length(wslist))
{wsclass[i]<-class(get(wslist[i]))
wsmemKb[i]<- object.size(get(wslist[i]))/1000*1024}
#step4 combining them in a data.frame
wobj<-data.frame(cbind(wslist,wsclass,wsmemKb))
# library(sqldf)
# sqldf("pragma table_info(wobj)") shows col 3(wsmem) still non-numeric
wobj[,3] <- as.numeric( as.character(wobj[,3]) )
# create data to return matrix of memory consumption
objmemsize <- rev(sort(sapply(ls(envir=myenvironmentName), 
                function (object.name)object.size(get(object.name))/1000)))
# draw bar plot
barplot(objmemsize,main="Memory usage by object in myenvironment", 
          ylab="KiloByte", xlab="Variable name", 
            col=heat.colors(length(objmemsize)))
# result <- sqldf("select * from wobj order by 1/wsmemKb")
return(wobj)
#   return(data.frame(myenvironmentName,wobj))
#   return(assign("myname",wobj,envir = .GlobalEnv))
#   attach(wobj,pos=2,"wobj")
return(barplot)
}
# use of mywsobj function
mywsobj(.GlobalEnv)
# saving output of mywsobj function
output<-as.data.frame(mywsobj(.GlobalEnv))   

Not sure if this is what you're after. 不确定这是否是您要的。 But, you can assign values to that environment with $ . 但是,您可以使用$将值分配给该环境。

my_fun <- function(in.env) {
    # you may want to check if input argument is an environment

    # do your computations
    set.seed(45)
    x <- sample(10)
    y <- runif(10)
    in.env$val <- sum(x*y)
}

my_fun(my.env <- new.env())
ls(my.env)
[1] "val"
my.env$val
# [1] 22.30493

Alternatively, you can also use assign as follows: 另外,您也可以按以下方式使用assign

my_fun <- function(in.env) {
    # you may want to check if input argument is an environment

    # do your computations
    set.seed(45)
    x <- sample(10)
    y <- runif(10)
    assign("val", sum(x*y), envir=in.env)
}

# assign to global environment
my_fun(globalenv())
> val
# [1] 22.30493

# assign to local environment, say, v
v <- new.env()
my_fun(v)
> v$val
# [1] 22.30493

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM