Load AND execute R source file

r

Solution

`?source` states:

Description:

     ‘source’ causes R to accept its input from the named file or URL
     or connection.  Input is read and ‘parse’d from that file until
     the end of the file is reached, then the parsed expressions are
     evaluated sequentially in the chosen environment.

Therefore,

- `source` is the function you are looking for,

- I refute your claim - `source` works for me as per the documentation

If you are not seeing the documented behaviour then there must be a different problem that you are not telling us.

How are you deducing that `source` is not executing your `f`?

I can think of one scenario that you may not be aware of, which is documented in `?source`, namely:

Details:

     Note that running code via ‘source’ differs in a few respects from
     entering it at the R command line.  Since expressions are not
     executed at the top level, auto-printing is not done.  So you will
     need to include explicit ‘print’ calls for things you want to be
     printed (and remember that this includes plotting by ‘lattice’,
     FAQ Q7.22).

Not seeing the output from objects called by name or grid graphics-based plots not showing are symptoms of auto-printing not being active as the evaluation is not occurring at the top-level. You need to explicitly `print()` objects, including grid-based graphics like Lattice and ggplot2 figures.

Also note the `print.eval` argument, which will default to

> getOption("verbose")
[1] FALSE

which may also be of use to you.

Problem

Consider a source file of this form: ``` # initialize the function f = function() { # ... } # call the function f() ``` In python, the `import` function would load and executes the file; however in R, the `source` command does initialize functions defined by the source file, but does not call the functions if they are called in the file. Is there any R command (or option) to import and execute the file? Thanks for your help.

Original source

Related problems