Next: , Previous: Conversion system, Up: Top


5 Input/Output functions

RPy provides three functions for customizing the input and output from the R interpreter.

In versions 0.1 and 0.2, the input/output from/to R was connected to the C stdin/stdout, which don't necessarily coincides with the Python sys.stdin/sys.stdout. These was noticeable if you run those versions over IDLE or other IDE (probably, you don't see the output of r.print_(5)). Now, the R input/output is connected, by default, to the Python streams. But you can insert your own functions for reading, writing and displaying files.

get_rpy_input()
set_rpy_input(f)
Get/set the function used by the R interpreter to require input.

The parameter for set_rpy_input must be a function with signature f(prompt, size). The parameter prompt is a string to be displayed and size is an integer which denotes the maximum length of the input buffer.

get_rpy_output()
set_rpy_output(f)
Get/set the function used by the R interpreter to output data.

The parameter for set_rpy_output must be a function with signature f(s), where s is the string to be displayed.

get_rpy_showfiles()
set_rpy_showfiles(f)
[Not available on Windows] Get/set the function used by the R interpreter to display files, including the output from the help command.

The parameter for set_rpy_showfiles must be a function with signature f(files, headers, title, delete). Parameters files and headers are lists of filenames and strings, respectively, to be displayed sequentially. Parameter title is the overall title and parameter delete signals whether the files should be deleted after displaying.

The default values for the input/output/showfiles functions are in the io module. That is, when RPy is imported, the following instructions are executed:

     import io
     set_rpy_input(io.rpy_input)
     set_rpy_output(io.rpy_output)
     set_rpy_showfiles(io.rpy_showfiles)

For input and output, the functions io.rpy_input and io.rpy_output just use the sys.stdin and sys.stdout streams of Python. For displaying files, the io module provides two functions: io.showfiles_common and io.showfiles_tty, and the default io.rpy_showfiles is an alias for the former. Function io.showfiles_common displays the files using the io.rpy_output function, while function io.showfiles_tty displays the files using a pager (namely less, you may need to customize it).

Notes