[R] Custom Function Not Completely working

Lopez, Dan lopez235 at llnl.gov
Thu Oct 2 19:46:43 CEST 2014


Thank you Duncan!

Dan


-----Original Message-----
From: Duncan Murdoch [mailto:murdoch.duncan at gmail.com] 
Sent: Monday, September 29, 2014 6:23 PM
To: Lopez, Dan; R help (r-help at r-project.org)
Subject: Re: [R] Custom Function Not Completely working

On 29/09/2014, 9:07 PM, Lopez, Dan wrote:
> Hi R Experts,
> 
> I'm in the process of creating a set of convenience functions for myself. I am somewhat new to writing functions in r.
> 
> In this example I wanted to load both existing files called KuhnPerform.Rdata and KuhnPerform.Rhistory. However it only works for loading the .Rhistory file and not the .Rdata file. Can someone please tell me how to fix this so that it loads both .Rdata and .Rhistory?
> 
> I checked dir() that these files are in my working directory and are 
> spelled correctly
> 
> loads<-function(filename="KuhnPerform"){
> load(paste0(filename,".Rdata")) # Load a previously saved workspace.
> loadhistory(paste0(filename,".Rhistory")) # Load a previously saved history file.
> }

You're loading the objects from the Rdata file into the evaluation frame of your function, and they go away afterwards.  You need to specify where they should be loaded using the envir argument, e.g.


load(paste0(filename,".Rdata"), envir = parent.frame())

(The funny thing here is that the default for envir is parent.frame(), but it means something different in that context.)

Duncan Murdoch

> 
> Dan
> Workforce Analyst
> LLNL
> 
> 
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> 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