require() but for source()d files

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

require() but for source()d files

Ivan Calandra-3
Dear useRs,

I'm looking for something like require() but which will work on
source()d files.

I have a .R file with lots of functions and I'm writing a new function
(say, 'foo') that depends on the functions from this file.

Until now, I have always source()d the .R file before running 'foo'.
But I would like to include some code in 'foo' to source() the file only
if it hasn't been done yet, and do nothing if the file has already been
source()d in the session.
So, the same behavior as require() with packages, but with a .R file.

Does it make sense? I could provide an example if you think it would help.

Thank you in advance,
Ivan

--
Ivan Calandra
University of Franche-Comté
Laboratoire Chrono-Environnement
Bureau ATER -107L
16, Route de Gray
25030 Besançon Cedex, France
[hidden email]
+33 (0) 381 66 20 60
http://chrono-environnement.univ-fcomte.fr/spip.php?article1830

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: require() but for source()d files

Henrik Bengtsson-3
On Aug 21, 2014 7:40 AM, "Ivan Calandra" <[hidden email]>
wrote:
>
> Dear useRs,
>
> I'm looking for something like require() but which will work on source()d
files.
>
> I have a .R file with lots of functions and I'm writing a new function
(say, 'foo') that depends on the functions from this file.
>
> Until now, I have always source()d the .R file before running 'foo'.
> But I would like to include some code in 'foo' to source() the file only
if it hasn't been done yet, and do nothing if the file has already been
source()d in the session.

R.utils::sourceTo(file, modifiedOnly=TRUE)

should do it. It will always source the file once. Also, contrary to
source() it default is to use local=TRUE.

Henrik

> So, the same behavior as require() with packages, but with a .R file.
>
> Does it make sense? I could provide an example if you think it would help.
>
> Thank you in advance,
> Ivan
>
> --
> Ivan Calandra
> University of Franche-Comté
> Laboratoire Chrono-Environnement
> Bureau ATER -107L
> 16, Route de Gray
> 25030 Besançon Cedex, France
> [hidden email]
> +33 (0) 381 66 20 60
> http://chrono-environnement.univ-fcomte.fr/spip.php?article1830
>
> ______________________________________________
> [hidden email] 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.

        [[alternative HTML version deleted]]

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: require() but for source()d files

Ivan Calandra-3
Thank you Henrik, that's exactly what I'm looking for!
Ivan

--
Ivan Calandra
University of Franche-Comté
Laboratoire Chrono-Environnement
Bureau ATER -107L
16, Route de Gray
25030 Besançon Cedex, France
[hidden email]
+33 (0) 381 66 20 60
http://chrono-environnement.univ-fcomte.fr/spip.php?article1830

Le 21/08/14 18:15, Henrik Bengtsson a écrit :

>
> On Aug 21, 2014 7:40 AM, "Ivan Calandra" <[hidden email]
> <mailto:[hidden email]>> wrote:
> >
> > Dear useRs,
> >
> > I'm looking for something like require() but which will work on
> source()d files.
> >
> > I have a .R file with lots of functions and I'm writing a new
> function (say, 'foo') that depends on the functions from this file.
> >
> > Until now, I have always source()d the .R file before running 'foo'.
> > But I would like to include some code in 'foo' to source() the file
> only if it hasn't been done yet, and do nothing if the file has
> already been source()d in the session.
>
> R.utils::sourceTo(file, modifiedOnly=TRUE)
>
> should do it. It will always source the file once. Also, contrary to
> source() it default is to use local=TRUE.
>
> Henrik
>
> > So, the same behavior as require() with packages, but with a .R file.
> >
> > Does it make sense? I could provide an example if you think it would
> help.
> >
> > Thank you in advance,
> > Ivan
> >
> > --
> > Ivan Calandra
> > University of Franche-Comté
> > Laboratoire Chrono-Environnement
> > Bureau ATER -107L
> > 16, Route de Gray
> > 25030 Besançon Cedex, France
> > [hidden email] <mailto:[hidden email]>
> > +33 (0) 381 66 20 60
> > http://chrono-environnement.univ-fcomte.fr/spip.php?article1830
> >
> > ______________________________________________
> > [hidden email] <mailto:[hidden email]> 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.
>

______________________________________________
[hidden email] 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.