Loading R files from source?

0 views
Skip to first unread message

Daniel Gates

unread,
Jun 4, 2012, 9:03:00 PM6/4/12
to treevo-...@googlegroups.com
Greetings all,

I've been poking around in the doSimulation.R file today, annotating and attempting to understand what's going on under the hood but I've run into a bit of a hiccup. What I'm trying to do is simply call the file doSimulation.R outside of the TreEvo package, I've tried moving it to a directory with an input file and calling it from with a source('doSimulation.R') command in the file with the arguments that need to be passed to the function doSimulation() but when I attempt to run the doSimulation() function i get an error saying that the function "states" cannot be found... I've also attempted to just hash out the doSimulation<-function() call, and manually define all the input and walk through line by line but once again I run into more errors. Does anyone have good advice on how I can work through this file and see what objects are getting passed around with the intent of modifying loops without having to keep adding things like print() statements and reloading the entire TreEvo R-package?

Sorry if that may have been confusing, I'll be happy to clarify any questions you might have about what I've tried/ haven't tried

Cheers!
-Dan

Brian O'Meara

unread,
Jun 4, 2012, 10:59:02 PM6/4/12
to treevo-...@googlegroups.com
It's because doSimulation.R relies on other functions in TreEvo, so calling it by itself won't work. One way to figure this out:

Function "states" cannot be found. First, see where states is called in doSimulation by grepping in the R directory:

grep states doSimulation.R

You see it's used as a function, states( something ), five times but never defined.

Then doing

grep states *.R | grep function

shows that states() is defined as a function in SetMethods.R . So loading doSimulation.R by itself won't work for running code, as it won't include the states() function (among others). 

You can source all the R code in the current working directory by doing 

lapply(system('ls *.R',intern=TRUE),source)

which should give you all the functions you need, while using your modified versions (assuming we're in your branch directory) rather than the ones on the main R-forge trunk. You can also simply install your local version of the package:

install.packages("/Users/bomeara/Documents/MyDocuments/Active/TreEvo/pkg-dgates-branch", type="source")

You might modify the DESCRIPTION file or something else in your branch so you can verify that the one you're using when you later call library("TreEvo") is your modified version (i.e., change version number, have it print "Hello, Dan, I already feel less sluggish", etc.).

Hope that helps,
Brian

_______________________________________
Brian O'Meara
Assistant Professor
Dept. of Ecology & Evolutionary Biology
U. of Tennessee, Knoxville
http://www.brianomeara.info

Students wanted: Applications due Dec. 15, annually
Postdoc collaborators wanted: Check NIMBioS' website
Calendar: http://www.brianomeara.info/calendars/omeara

Brian O'Meara

unread,
Jun 4, 2012, 11:16:58 PM6/4/12
to treevo-...@googlegroups.com
On Mon, Jun 4, 2012 at 10:59 PM, Brian O'Meara <bom...@utk.edu> wrote:

install.packages("/Users/bomeara/Documents/MyDocuments/Active/TreEvo/pkg-dgates-branch", type="source")

Correction: you have to pass repos=NULL as an argument to install.packages as well.

Best,
Brian
Reply all
Reply to author
Forward
0 new messages