Re: [R] How to load functions in R

2008-09-11 Thread bartjoosen

Take a look at ?source


Mihai.Mirauta wrote:
 
 
 Hello,
 
 I am trying to use self created functions in other scripts than the one
 where they are stored.
 For the moment I am using the following structure of commands to do
 that:
 
 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if more
 than one function is stored in the text file
 3. use the functions as normal
 
 Is there another possibility to do the same?
 Thank you,
 
 Mihai Mirauta
 
   [[alternative HTML version deleted]]
 
 __
 R-help@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.
 
 

-- 
View this message in context: 
http://www.nabble.com/How-to-load-functions-in-R-tp19434909p19434944.html
Sent from the R help mailing list archive at Nabble.com.

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Yihui Xie
Hi, you may save your functions somewhere on your disk using save()
and load them next time when you want to use them. See ?save and ?load

Yihui

On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:

 Hello,

 I am trying to use self created functions in other scripts than the one
 where they are stored.
 For the moment I am using the following structure of commands to do
 that:

 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if more
 than one function is stored in the text file
 3. use the functions as normal

 Is there another possibility to do the same?
 Thank you,

 Mihai Mirauta

[[alternative HTML version deleted]]


-- 
Yihui Xie [EMAIL PROTECTED]
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Adaikalavan Ramasamy
I would recommend saving the functions into a separate file and then 
using source() as bartjoosen suggested.


I do not recommend using save() here because the output is non-readable 
(even when using ascii=TRUE option). Which means that you have to load() 
it, then copy-and-paste into an editor before making changes and then 
running it again in R and then save() again.


Another better option is to consider making your own package. It may 
sound complicated but once you mastered it, it makes your functions more 
portable and encourages you to document it. Further, the function 
package.skeleton() simplifies much of it.


Regards, Adai



Yihui Xie wrote:

Hi, you may save your functions somewhere on your disk using save()
and load them next time when you want to use them. See ?save and ?load

Yihui

On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:

Hello,

I am trying to use self created functions in other scripts than the one
where they are stored.
For the moment I am using the following structure of commands to do
that:

1. Load the text file with the functions in the current script:
x=parse(path)
2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if more
than one function is stored in the text file
3. use the functions as normal

Is there another possibility to do the same?
Thank you,

Mihai Mirauta

   [[alternative HTML version deleted]]





__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Yihui Xie
We may just read them in the R console instead of an external editor,
and fix() or edit() them when we need to make any modifications. A
trivial advantage of saving them as an image file in Windows is that
you can double-click the file and R will be started with these objects
loaded automatically. Anyway, to save the functions as ASCII files or
even write a package are also good solutions :-)

Regards,
Yihui

On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy
[EMAIL PROTECTED] wrote:
 I would recommend saving the functions into a separate file and then using
 source() as bartjoosen suggested.

 I do not recommend using save() here because the output is non-readable
 (even when using ascii=TRUE option). Which means that you have to load() it,
 then copy-and-paste into an editor before making changes and then running it
 again in R and then save() again.

 Another better option is to consider making your own package. It may sound
 complicated but once you mastered it, it makes your functions more portable
 and encourages you to document it. Further, the function package.skeleton()
 simplifies much of it.

 Regards, Adai



 Yihui Xie wrote:

 Hi, you may save your functions somewhere on your disk using save()
 and load them next time when you want to use them. See ?save and ?load

 Yihui

 On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:

 Hello,

 I am trying to use self created functions in other scripts than the one
 where they are stored.
 For the moment I am using the following structure of commands to do
 that:

 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if more
 than one function is stored in the text file
 3. use the functions as normal

 Is there another possibility to do the same?
 Thank you,

 Mihai Mirauta

   [[alternative HTML version deleted]]







-- 
Yihui Xie [EMAIL PROTECTED]
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building,
Renmin University of China, Beijing, 100872, China

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Mihai.Mirauta
 
Hello,
It seems that all methods work. 
Source() however loads only the last function. with save(a,b,file=path) i can 
save more than 1 function. 
Thanks a lot,

Mihai
-Ursprüngliche Nachricht-
Von: Yihui Xie [mailto:[EMAIL PROTECTED] 
Gesendet: Donnerstag, 11. September 2008 16:48
An: [EMAIL PROTECTED]
Cc: Mirauta, Mihai; r-help@r-project.org
Betreff: Re: [R] How to load functions in R

We may just read them in the R console instead of an external editor, and 
fix() or edit() them when we need to make any modifications. A trivial 
advantage of saving them as an image file in Windows is that you can 
double-click the file and R will be started with these objects loaded 
automatically. Anyway, to save the functions as ASCII files or even write a 
package are also good solutions :-)

Regards,
Yihui

On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy [EMAIL PROTECTED] 
wrote:
 I would recommend saving the functions into a separate file and then 
 using
 source() as bartjoosen suggested.

 I do not recommend using save() here because the output is 
 non-readable (even when using ascii=TRUE option). Which means that you 
 have to load() it, then copy-and-paste into an editor before making 
 changes and then running it again in R and then save() again.

 Another better option is to consider making your own package. It may 
 sound complicated but once you mastered it, it makes your functions 
 more portable and encourages you to document it. Further, the function 
 package.skeleton() simplifies much of it.

 Regards, Adai



 Yihui Xie wrote:

 Hi, you may save your functions somewhere on your disk using save()
 and load them next time when you want to use them. See ?save and 
 ?load

 Yihui

 On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:

 Hello,

 I am trying to use self created functions in other scripts than the 
 one where they are stored.
 For the moment I am using the following structure of commands to do
 that:

 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if 
 more than one function is stored in the text file 3. use the 
 functions as normal

 Is there another possibility to do the same?
 Thank you,

 Mihai Mirauta

   [[alternative HTML version deleted]]







--
Yihui Xie [EMAIL PROTECTED]
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building, Renmin University of 
China, Beijing, 100872, China

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Martin Maechler
 YX == Yihui Xie [EMAIL PROTECTED]
 on Thu, 11 Sep 2008 22:47:47 +0800 writes:

YX We may just read them in the R console instead of an external editor,
YX and fix() or edit() them when we need to make any modifications. A
YX trivial advantage of saving them as an image file in Windows is that
YX you can double-click the file and R will be started with these objects
YX loaded automatically. Anyway, to save the functions as ASCII files or
YX even write a package are also good solutions :-)
 ^

also good...  baahh...  *THE* good!

I'd strongly second Adai's view here:

*DO* use  *.R source files --- together with an R-syntax aware
   editor, there are many nowadays
and source() them, as a first step;
put them in an own package as a 2nd step.
 
Martin Maechler, ETH Zurich

YX Regards,
YX Yihui

YX On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy
YX [EMAIL PROTECTED] wrote:
 I would recommend saving the functions into a separate file and then 
using
 source() as bartjoosen suggested.
 
 I do not recommend using save() here because the output is non-readable
 (even when using ascii=TRUE option). Which means that you have to load() 
it,
 then copy-and-paste into an editor before making changes and then 
running it
 again in R and then save() again.
 
 Another better option is to consider making your own package. It may 
sound
 complicated but once you mastered it, it makes your functions more 
portable
 and encourages you to document it. Further, the function 
package.skeleton()
 simplifies much of it.
 
 Regards, Adai
 
 
 
 Yihui Xie wrote:
 
 Hi, you may save your functions somewhere on your disk using save()
 and load them next time when you want to use them. See ?save and ?load
 
 Yihui
 
 On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:
 
 Hello,
 
 I am trying to use self created functions in other scripts than the one
 where they are stored.
 For the moment I am using the following structure of commands to do
 that:
 
 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if 
more
 than one function is stored in the text file
 3. use the functions as normal
 
 Is there another possibility to do the same?
 Thank you,
 
 Mihai Mirauta
 
 [[alternative HTML version deleted]]
 
 
 
 



YX -- 
YX Yihui Xie [EMAIL PROTECTED]
YX Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
YX Mobile: +86-15810805877
YX Homepage: http://www.yihui.name
YX School of Statistics, Room 1037, Mingde Main Building,
YX Renmin University of China, Beijing, 100872, China

YX __
YX R-help@r-project.org mailing list
YX https://stat.ethz.ch/mailman/listinfo/r-help
YX PLEASE do read the posting guide 
http://www.R-project.org/posting-guide.html
YX and provide commented, minimal, self-contained, reproducible code.

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Adaikalavan Ramasamy

Strange.

source() should read all the function in that file unless there was a 
syntax error or something else preventing the other function from being 
parsed correctly. Could you send us a simplified example that reproduces 
this problem?


Thanks.

Regards, Adai



[EMAIL PROTECTED] wrote:
 
Hello,
It seems that all methods work. 
Source() however loads only the last function. with save(a,b,file=path) i can save more than 1 function. 
Thanks a lot,


Mihai
-Ursprüngliche Nachricht-
Von: Yihui Xie [mailto:[EMAIL PROTECTED] 
Gesendet: Donnerstag, 11. September 2008 16:48

An: [EMAIL PROTECTED]
Cc: Mirauta, Mihai; r-help@r-project.org
Betreff: Re: [R] How to load functions in R

We may just read them in the R console instead of an external editor, and fix() or 
edit() them when we need to make any modifications. A trivial advantage of saving them 
as an image file in Windows is that you can double-click the file and R will be started with these 
objects loaded automatically. Anyway, to save the functions as ASCII files or even write a package 
are also good solutions :-)

Regards,
Yihui

On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy [EMAIL PROTECTED] 
wrote:
  
I would recommend saving the functions into a separate file and then 
using

source() as bartjoosen suggested.

I do not recommend using save() here because the output is 
non-readable (even when using ascii=TRUE option). Which means that you 
have to load() it, then copy-and-paste into an editor before making 
changes and then running it again in R and then save() again.


Another better option is to consider making your own package. It may 
sound complicated but once you mastered it, it makes your functions 
more portable and encourages you to document it. Further, the function 
package.skeleton() simplifies much of it.


Regards, Adai



Yihui Xie wrote:


Hi, you may save your functions somewhere on your disk using save()
and load them next time when you want to use them. See ?save and 
?load


Yihui

On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:
  

Hello,

I am trying to use self created functions in other scripts than the 
one where they are stored.

For the moment I am using the following structure of commands to do
that:

1. Load the text file with the functions in the current script:
x=parse(path)
2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if 
more than one function is stored in the text file 3. use the 
functions as normal


Is there another possibility to do the same?
Thank you,

Mihai Mirauta

  [[alternative HTML version deleted]]







--
Yihui Xie [EMAIL PROTECTED]
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building, Renmin University of 
China, Beijing, 100872, China




__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Adam D. I. Kramer

Source(file.path) executes the file at file.path in order, just as if you
had typed it in.

So, the source file should in fact name each function in turn:

f1 - function(x) { ... }
f2 - function(x) { ... }
...etc.

So a good way to debug is to just copy and paste lines from your source file
into the R command line, and see if they behave as expected.

--Adam

On Thu, 11 Sep 2008, Adaikalavan Ramasamy wrote:


Strange.

source() should read all the function in that file unless there was a syntax 
error or something else preventing the other function from being parsed 
correctly. Could you send us a simplified example that reproduces this 
problem?


Thanks.

Regards, Adai



[EMAIL PROTECTED] wrote:

 Hello,
It seems that all methods work. Source() however loads only the last 
function. with save(a,b,file=path) i can save more than 1 function. 
Thanks a lot,


Mihai
-Ursprüngliche Nachricht-
Von: Yihui Xie [mailto:[EMAIL PROTECTED] Gesendet: Donnerstag, 11. 
September 2008 16:48

An: [EMAIL PROTECTED]
Cc: Mirauta, Mihai; r-help@r-project.org
Betreff: Re: [R] How to load functions in R

We may just read them in the R console instead of an external editor, and 
fix() or edit() them when we need to make any modifications. A trivial 
advantage of saving them as an image file in Windows is that you can 
double-click the file and R will be started with these objects loaded 
automatically. Anyway, to save the functions as ASCII files or even write 
a package are also good solutions :-)


Regards,
Yihui

On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy 
[EMAIL PROTECTED] wrote:


I would recommend saving the functions into a separate file and then 
using

source() as bartjoosen suggested.

I do not recommend using save() here because the output is non-readable 
(even when using ascii=TRUE option). Which means that you have to load() 
it, then copy-and-paste into an editor before making changes and then 
running it again in R and then save() again.


Another better option is to consider making your own package. It may 
sound complicated but once you mastered it, it makes your functions more 
portable and encourages you to document it. Further, the function 
package.skeleton() simplifies much of it.


Regards, Adai



Yihui Xie wrote:


Hi, you may save your functions somewhere on your disk using save()
and load them next time when you want to use them. See ?save and ?load

Yihui

On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:


Hello,

I am trying to use self created functions in other scripts than the one 
where they are stored.

For the moment I am using the following structure of commands to do
that:

1. Load the text file with the functions in the current script:
x=parse(path)
2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if 
more than one function is stored in the text file 3. use the functions 
as normal


Is there another possibility to do the same?
Thank you,

Mihai Mirauta

  [[alternative HTML version deleted]]








--
Yihui Xie [EMAIL PROTECTED]
Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
Mobile: +86-15810805877
Homepage: http://www.yihui.name
School of Statistics, Room 1037, Mingde Main Building, Renmin University 
of China, Beijing, 100872, China





__
R-help@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.
__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Rolf Turner


On 12/09/2008, at 2:53 AM, [EMAIL PROTECTED] wrote:




Hello,
It seems that all methods work.
Source() however loads only the last function.


This is absolute nonsense.  You are doing something wrong
and/or not understanding what you are doing.  This is
bad practice.


with save(a,b,file=path) i can save more than 1 function.


No kidding!


cheers,

Rolf Turner

##
Attention:\ This e-mail message is privileged and confid...{{dropped:9}}

__
R-help@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.


Re: [R] How to load functions in R

2008-09-11 Thread Henrik Bengtsson
Wow...

source() on a text file is the way to go - forget everything else.  If
you have multiple *.R script files, in a directory, say R/, you can
load them all by:

pathnames - list.files(pattern=[.]R$, path=R/, full.names=TRUE);
sapply(pathnames, FUN=source);

or even shorter:

sapply(list.files(pattern=[.]R$, path=R/, full.names=TRUE), source);

This way the changes you need to do for setting up a package to a minimal.

FYI, in the R.utils package there is a utility function
sourceDirectory() that makes this even easier- this function can keep
track of what files have been changed since last time you sourced a
directory.  Just do

sourceDirectory(R/, modifiedOnly=TRUE);

to load all your *.R scripts.  If you then update one of the files, just do

sourceDirectory(R/, modifiedOnly=TRUE);

again, to reload that file.  All other non-modified files will be
ignored, which can be convenient if you have a lot of files.

Cheers

/Henrik




On Thu, Sep 11, 2008 at 12:14 PM, Adam D. I. Kramer
[EMAIL PROTECTED] wrote:
 Source(file.path) executes the file at file.path in order, just as if you
 had typed it in.

 So, the source file should in fact name each function in turn:

 f1 - function(x) { ... }
 f2 - function(x) { ... }
 ...etc.

 So a good way to debug is to just copy and paste lines from your source file
 into the R command line, and see if they behave as expected.

 --Adam

 On Thu, 11 Sep 2008, Adaikalavan Ramasamy wrote:

 Strange.

 source() should read all the function in that file unless there was a
 syntax error or something else preventing the other function from being
 parsed correctly. Could you send us a simplified example that reproduces
 this problem?

 Thanks.

 Regards, Adai



 [EMAIL PROTECTED] wrote:

  Hello,
 It seems that all methods work. Source() however loads only the last
 function. with save(a,b,file=path) i can save more than 1 function. Thanks
 a lot,

 Mihai
 -Ursprüngliche Nachricht-
 Von: Yihui Xie [mailto:[EMAIL PROTECTED] Gesendet: Donnerstag, 11.
 September 2008 16:48
 An: [EMAIL PROTECTED]
 Cc: Mirauta, Mihai; r-help@r-project.org
 Betreff: Re: [R] How to load functions in R

 We may just read them in the R console instead of an external editor, and
 fix() or edit() them when we need to make any modifications. A trivial
 advantage of saving them as an image file in Windows is that you can
 double-click the file and R will be started with these objects loaded
 automatically. Anyway, to save the functions as ASCII files or even write a
 package are also good solutions :-)

 Regards,
 Yihui

 On Thu, Sep 11, 2008 at 10:34 PM, Adaikalavan Ramasamy
 [EMAIL PROTECTED] wrote:

 I would recommend saving the functions into a separate file and then
 using
 source() as bartjoosen suggested.

 I do not recommend using save() here because the output is non-readable
 (even when using ascii=TRUE option). Which means that you have to load() 
 it,
 then copy-and-paste into an editor before making changes and then running 
 it
 again in R and then save() again.

 Another better option is to consider making your own package. It may
 sound complicated but once you mastered it, it makes your functions more
 portable and encourages you to document it. Further, the function
 package.skeleton() simplifies much of it.

 Regards, Adai



 Yihui Xie wrote:

 Hi, you may save your functions somewhere on your disk using save()
 and load them next time when you want to use them. See ?save and ?load

 Yihui

 On Thu, Sep 11, 2008 at 9:30 PM,  [EMAIL PROTECTED] wrote:

 Hello,

 I am trying to use self created functions in other scripts than the
 one where they are stored.
 For the moment I am using the following structure of commands to do
 that:

 1. Load the text file with the functions in the current script:
 x=parse(path)
 2. transform the tex in a function: f1=eval(x[1]), f2=eval(x[2]) if
 more than one function is stored in the text file 3. use the functions as
 normal

 Is there another possibility to do the same?
 Thank you,

 Mihai Mirauta

  [[alternative HTML version deleted]]






 --
 Yihui Xie [EMAIL PROTECTED]
 Phone: +86-(0)10-82509086 Fax: +86-(0)10-82509086
 Mobile: +86-15810805877
 Homepage: http://www.yihui.name
 School of Statistics, Room 1037, Mingde Main Building, Renmin University
 of China, Beijing, 100872, China



 __
 R-help@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.

 __
 R-help@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.



__
R-help@r-project.org mailing list
https://stat.ethz.ch