[R] object.size() not recognized within .First()

Duncan Murdoch murdoch.duncan at gmail.com
Tue Sep 6 16:44:00 CEST 2011


On 11-09-06 10:24 AM, Geof Givens wrote:
> I have a function called within .First(), as in
>
> .First=function() {
> ...blah...
> BIG(partofblah)   #BIG is my function, n=partofblah when called
> ...foo...
>    }
>
> The partofblah component of blah is a number obtained from readline(),
> which is then an argument to BIG()
>
> BIG=function(n=10,removeask=T) {
>     z<- sapply(ls(pos=1), function(x)object.size(get(x)))
>     ...stuff... }
>
> When .First() executes upon startup and readline() determines
> partofblah, the call to BIG() creates an error as follows:
>
> Hello.  How many huge items to consider deleting?  (Enter=none)
> 3
> Error in FUN(c("a", "a1", "a2", "aa", "aaa", "abline", "ad", "adj11",  :
> could not find function "object.size"
>
> The "Hello" line is a prompt from .First().  "3" is my input.  The error
> occurs when BIG() is executed because object.size() is called
> immediately upon entry to BIG().
>
> After this error, execution of .First() from the command line works fine.
>
> I can't figure out what is going wrong.  Any ideas?



See the help page ?.First:  It is run before the standard packages are 
attached.  You need to say where to find that function if you want to 
use it, i.e. use utils::object.size.

Duncan Murdoch

 >


>
> Thanks,
>
> Geof
>
>
>
> BELOW HERE ARE THE COMPLETE FUNCTIONS:
>
> .First=function() {
>     cat("Hello.  How many huge items to consider deleting?  (Enter=none)\n")
>     g=eval(parse(text=readline()))
>     if (is.numeric(g)) { BIG(n=g)}
>     cat("\nOk. What .Rdata file and directory do you want to use?\n\n")
>     cat("0 = Default (C:/Users/geof/My Documents)\n")
>     cat("1 = ~geof/teach/stat540/2011/R\n")
>     cat("c = choose by browsing\n")
>     thechosen=readline()
>     if (thechosen=="1") {
>
> load("c:\\users\\geof\\csu\\teach\\stat540\\2011\\R\\.RData",.GlobalEnv)
>       setwd("c:\\users\\geof\\csu\\teach\\stat540\\2011\\R") }
>     if (thechosen=="c") {
>     cat("Hello.  Choose .RData file to work with...\n")
>       where=file.choose()
>       rwhere=sub("\\.RData","",where)
>       load(where,.GlobalEnv)
>       setwd(rwhere)  }
>     cat("\n")
>     cat(paste("Okay, I've got you working/saving in ",
>        getwd()[1],"\n",sep="")) }
>
> BIG=function(n=10,removeask=T) {
>     z<- sapply(ls(pos=1), function(x)object.size(get(x)))
>     zlab=names(z)
>     z=as.matrix(rev(sort(z)))
>     zlab=as.matrix(rev(sort(zlab)))
>     myzmat=data.frame(id=1:n,name=zlab[1:n],size=z[1:n])
>     row.names(myzmat)=NULL
>     print(myzmat)
>     cat("\nGive c() vector of id's to delete, or return to exit\n")
>     thechosen=readline()
>     if (thechosen=="") {
>        invisible(return()) } else {
>        byebye=eval(parse(text=thechosen))
>        goodbye=as.character(myzmat$name)[byebye]
>        goodtogo=T
>        if(removeask) {
>          cat("Are you sure to delete (y/n):\n")
>          print(goodbye)
>          a=readline()
>          if(a!="y") goodtogo=F }
>        if(goodtogo) {
>            rm(list=goodbye,pos=1) } } }
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list