Home > FAQ


Q: How can I read
    and write data files?


There are two ways to read and write files:

Firstly, a basic functions to read and write files are implemented: fopen, fclose, fget, fput, fgets, fputs, fread, fwrite, filelength. Those functions can be used to read and write text files as well as data or binary files. See the example program 'files.P' in the examples folder or the transmit.file function in sockets.P.

Secondly, there are two (preliminary) functions in order to read and write matrices called readtable_ and writetable_. The underscore signifies that these functions are likely to be changed in the future.

x = readtable_( 'myfile.dat', 10, 4, ',' )

reads a 10x4 matrix of numbers from the text file myfile.dat. The entries of a row are seperated by ','. The last argument allows you to specify another separator.

To write a matrix mymat to a text file use

val = writetable_( 'myfile.dat', mymat )


call writetable_( 'myfile.dat', mymat )

Here 'val =' is a dummy assignment, writetable_ does not return something meaningful. call eats the return value.

Q: To which extent are strings supported?

Strings are available since the 2011_06 version. You can assign strings to a variable and paste them easily. For example:

a = 'hello';
b = a + ' Peter'

Notice that one may concatenate strings by + instead of using paste(). To ensure that you really get a string, one can use '' + ..., where '' is the empty string.
Strings can be compared using '<', '>', '<=', '>=', '==' and '!='
Please take a look at the example program 'strings.P' in the examples folder.

Q: Which features of C can be 
    used in a cstyle environment?

Currently, the following features of C are available in cstyle
  • C-like function headers
  • C-like for loops, e.g. for ( i = 0; i < n; i += 1 ) { ... }
  • The operator +=
  • vectors are indexed as in C
  • matrices are indexed as in C
  • pointers to identifiers are available (but dereferencing is neither implemented nor necessary)

Q: Why are the graphic functions
    of R not implemented?

R provides tremendous graphics. Since P source code can be easily embedded into R programs, it makes not really sense to reinvent the wheel. You can do computations in P and then use R to generate plots etc. Here is a simple example, which you can modify for you needs:
x = rnorm( 10000, 0, 1 )

P_setvector( "x",  x )

prg = "
  # Compute the n(n+1)/2 Walsh averages (x[i]+x[j])/2 for i <= j
  n = length(x)
  y = (1:(n*(n+1)/2))
  i = 0; j = 0; k = 1
  for ( i in (1:n) )
    for ( j in (1:i) ) {
      y[ k] = (x[i] + x[j])/2
      k = k + 1

P_script( prg )
y = P_getvector( "y" )
plot( density(y) )

Q: Can I use a shared library
    written in C++ in P?

Yes, you can. However, you have to add extern "C" to the function header. Also notice that all arguments have to be pointers to double. Here is an example:
extern "C" void *myfunc( double *x, double *n, double *m ) {

Q: Can I call C code from P as in R?

Yes, you can. P implements some tentative functions dynload_ and C_ for that purpose. Having created a shared library, say, mylib.dll, you can load it using

dynload_( 'mylib.dll' )

Suppose the library contains a function myfunc which manipulates a nxm matrix. To invoke the function use

val = C_( 'myfunc', &mat, nrow(mat), ncol(mat) )

It is important to use the reference operator, since otherwise the matrix mat will not be changed by myfunc.


Q: How to invoke P from R (many time)?

In the folder embed_R you find examples that show how to use the libP.dll (or libP.so) library to compile and run programs from R. Include P.R into your program and use 
to prepare Rturbo/P. With P_setvector and P_setmatrix you can transfer vectors and matrices to P and give them a name there. Prepare your Rturbo/P program as a string, say prg, and invoke
P_script( prg )
The program is then compiled and executed.
NOTE: There is still a stability issue when invoking P_script many times. The workaround is as follows: Simply reload the shared library each time. Use the following template:

# define your program and transfer data...
prg = ...
P_setvector( ... )

# call the program several times
for ( i in (1:10) ) {
  dyn.load("libP.dll")  # dyn.load("libP.so") for Linux
  P_setvector( "i", i )
  P_script( prg )