Note that there are some explanatory texts on larger screens.

plurals
  1. POR does not recall object in memory
    text
    copied!<p>I'm building a function with multiple steps, where an object is created at each step. A certain step fails (temp3) and cannot find the previous steps object (Error: object 'temp2' not found). I'm not sure why - I have similar functions that follow EXACTLY the same structure, each step following on the previously created object, within the function, which runs fine. When you run that code outside of the function it works (so the code seems fine), and using debug() the step that is supposedly not creating the data (temp2) is actually storing is into the local memory (so I can see the object “temp2”), but for some reason R does not seem to recognize it or use it. I'm stumped! Maybe I'm just not getting how R is evaluating steps and recalling objects within the local memory? Have I gone about writing the function in the wrong way?</p> <p>I can easily prepare a worked example if it would be of more use since this function recalls odd packages etc, but at the moment I think its more an issue of how I misunderstand how R assigns objects to the local memory within a function. A similar query is here, <a href="https://stackoverflow.com/questions/7537273/how-does-r-handle-object-in-function-call">How does R handle object in function call?</a>, but indeed I am assigning each new object within the function. Could you please help?</p> <pre><code> glm.random&lt;-function(df){ reps=5 output&lt;-matrix(NA, ncol=1, nrow=0) while (length(output[,1])&lt;reps) { temp1 &lt;- ddply(df,.(study_id),randomRows,1) temp2 &lt;- subset(temp1,select = c(continent,taxatype, metric,nullm, yi_pos)) temp3 &lt;- glmulti(yi_pos ~ ., data = temp2, family = gaussian( link = log), crit = aic, plotty = F, report = F) temp4 &lt;- noquote(paste(summary(temp3)$bestmodel[1])) output&lt;-rbind(output,temp4) } write.table(output, "output.glm.random1.txt", append=TRUE, sep="\t", quote=FALSE) } </code></pre> <p>In Reply:</p> <p>Hi again,</p> <p>Andrie – 1). So I delete the use of subset (but curious here, what ‘unexpected results’ do you refer to?). 2). I have been finding it difficult with the data at hand, but I see your point and need to improve my coding approach here 3). Good tip! But here its done just to check that thing are working – I would likely just use that output object for more analysis.</p> <p>Gavin 1) Will do! 2+3) So seems the error lies in creating (or recalling) ‘temp1’.</p> <p>Below what I hope is some reproducible code. If it helps, the approach I’m trying to duplicate is found in Gibson et al. 2011 Nature 478:378. (See Detailed Methods “Generalized linear models.”). </p> <pre><code>Thank you! #rm(list = ls()) library("plyr") library("glmulti") # random rows function randomRows = function(df,n){ return(df[sample(nrow(df),n),]) } # Dataframe example study_id &lt;- c(1,1,1,1,2,2,3,3,3,4) continent &lt;- c("AF","AF","AF","AF","AF","AF", "AS", "AS", "AS", "SA") taxatype &lt;- c("bird","bird","bird","mam","mam","arthro", "arthro", "arthro", "arthro", "arthro") metric&lt;- c("sppr","sppr","sppr","sppr","abund","abund", "abund", "abund", "abund", "abund") extra.data&lt;- c(34:43) yi_pos&lt;- runif(1:10) df&lt;- data.frame(study_id=study_id, continent=continent,metric=metric, taxatype=taxatype,extra.data = extra.data, yi_pos = yi_pos) df # Function. Goal:repeat x10000 (but here reps =5) ( Select one random value per study_id, run glmulti{glmulti}, select best ranked model, concatenate to an output and export). glm.random&lt;-function(df){ reps=5 output&lt;-matrix(NA, ncol=1, nrow=0) while (length(output[,1])&lt;reps) { temp1 &lt;- ddply(df,.(study_id),randomRows,1) temp3 &lt;- glmulti(yi_pos ~ continent+taxatype+metric, data = temp1, family = gaussian( link = log), crit = aic, plotty = F, report = F) temp4 &lt;- noquote(paste(summary(temp3)$bestmodel[1])) output&lt;-rbind(output,temp4) } write.table(output, "output.glm.random1.txt", append=TRUE, sep="\t", quote=FALSE) } # run function to obtain error glm.random(df) # debug(glm.random) # glm.random(df) # undebug(glm.random) </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload