2

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.

user
  • 7,123
  • 7
  • 48
  • 90
  • The function is executed on my system. – juba Apr 03 '13 at 20:35
  • 1
    mine as well. If you do not assign the output of a sourced function call, you will not see it, but the assigned variable will be available in your working environment. – Justin Apr 03 '13 at 20:35
  • Strange-- when I put a print statement in the function, it **is** called, but the qplot function **isn't called**. But when I copy and paste the file into the R interpreter, the gplot function is called. Is there some way that these functions are selectively called or could be silenced? Note: eval(parse('filename.R')) **does** call gplot. – user Apr 03 '13 at 20:39
  • 6
    Ah, the infamous ggplot/lattice issue: See http://stackoverflow.com/q/6675066/210673 – Aaron left Stack Overflow Apr 03 '13 at 20:40
  • 3
    @Oliver I have addressed this FAQ in my Answer below. It is due to 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. – Gavin Simpson Apr 03 '13 at 20:41

1 Answers1

7

?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,

  1. source is the function you are looking for,
  2. I refute your claim - source works for me as per the documentation
  3. 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.

Gavin Simpson
  • 170,508
  • 25
  • 396
  • 453