[R] Custom Function Not Completely working

Duncan Murdoch murdoch.duncan at gmail.com
Tue Sep 30 03:23:24 CEST 2014

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
> 	[[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