From whuber at embl.de  Mon Dec 28 10:02:33 2009
From: whuber at embl.de (Wolfgang Huber)
Date: Mon, 28 Dec 2009 10:02:33 +0100
Subject: [Rd] Locating code that is outside of functions in R scripts
In-Reply-To: <971536df0912231651p1e65f22dvb1041afab78163fc@mail.gmail.com>
References: <fb287d9d14fc581a6136541c2685b3b7@localhost>
	<971536df0912231651p1e65f22dvb1041afab78163fc@mail.gmail.com>
Message-ID: <4B387429.6090907@embl.de>

Dear Gheine,

you could also start creating your own package with these functions, and 
manage the code using R's well thought-out package management 
functionalities, with namespaces etc. At some point, this might be less 
effort than (re-)inventing tools as below.

		Best wishes
		Wolfgang




Gabor Grothendieck wrote:
> This sources the indicated file into the local environment (i.e. the
> environment within src) and then only copies out the objects that are
> functions into the workspace so that you are only left with them.
> 
> src <- function(file) {
>    source(file, local = TRUE)
>    for(nm in ls(all = TRUE)) if (is.function(get(nm))) assign(nm,
> get(nm), .GlobalEnv)
> }
> 
> # usage:
> src("myfile.R")
> 
> On Wed, Dec 23, 2009 at 7:04 PM,  <gheine at mathnmaps.com> wrote:
>> Working with a number of scripts (text files with R commands) that I
>>
>> "source" into R sessions from time to time.
>>
>> The source() command is most convenient (at least for me)
>>
>> if it only loads function definitions, and doesn't otherwise change the
>>
>> interactive environment.  For example, I might have a file that looks like
>>
>>
>>
>> func1<-function() {
>>
>>  code
>>
>>  code
>>
>>  code
>>
>>  }
>>
>> # this is a comment
>>
>>
>>
>> A<-"this is code outside the function definitions";
>>
>>
>>
>> func2<-function() {
>>
>>   code
>>
>> # a comment with a spurious }
>>
>>   code
>>
>>   code
>>
>>   }
>>
>>
>>
>> I would like a quick-and-dirty script that finds the line beginnning with
>>
>> "A", since it is R code outside of any function definition.
>>
>> On the other hand it would ignore the two comment lines, and would not be
>>
>> fooled by the spurious "}" in the second comment line.
>>
>> Probably not too hard to put something like this together, but am making
>>
>> this post in case it is something that has already been done.
>>
>>
>>
>>
>>
>> Thanks!
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
> 
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel

-- 

Best wishes
      Wolfgang


--
Wolfgang Huber
EMBL
http://www.embl.de/research/units/genome_biology/huber/contact



[Rd] Locating code that is outside of functions in R scripts
Wolfgang Huber