diff -urNp gretl/gretl/cli/Makefile.in gretl3/cli/Makefile.in
--- gretl/gretl/cli/Makefile.in	2008-03-22 16:38:37.000000000 +0000
+++ gretl3/cli/Makefile.in	2009-07-05 14:43:25.000000000 +0100
@@ -51,7 +51,8 @@ LIBTOOL = ../libtool
 	$(CC) $(CFLAGS) -MM $< > .deps/$*.d 
 
 $(CLI): .deps $(OBJS)
-	$(LIBTOOL) --mode=link $(CC) -o $@ $(OBJS) $(GRETLLIB) $(READLINE_LIBS) 
+	$(LIBTOOL) --mode=link $(CC) -o $@ $(OBJS) $(GRETLLIB) $(READLINE_LIBS)  /usr/lib/R/lib/libR.so
+
 
 .deps:
 	mkdir $@
diff -urNp gretl/gretl/lib/Makefile.in gretl3/lib/Makefile.in
--- gretl/gretl/lib/Makefile.in	2009-06-20 16:59:43.000000000 +0100
+++ gretl3/lib/Makefile.in	2009-07-05 14:37:31.000000000 +0100
@@ -5,7 +5,7 @@ prefix = @prefix@
 exec_prefix = @exec_prefix@
 bindir = @bindir@
 CC = @CC@
-CFLAGS = @CFLAGS@
+CFLAGS = -I/usr/share/R/include/ @CFLAGS@
 MAKE = @MAKE@
 INSTALL = @INSTALL@
 LN = @LN_S@
@@ -54,7 +54,8 @@ else
 endif
 
 LIBS = $(LAPACK_LIBS) -lm -ldl -lz $(XML_LIBS) \
-	$(GLIB_LIBS) $(GMP_LIBS) $(FFTW_LIBS)
+	$(GLIB_LIBS) $(GMP_LIBS) $(FFTW_LIBS) \
+	/usr/lib/R/lib/libR.so
 
 #### End of system configuration section. ####
 
@@ -77,6 +78,7 @@ PUBLIC_HDRS = \
 	dbread.h \
 	describe.h \
 	discrete.h \
+	embeddedRCall.h \
 	estimate.h \
 	flow_control.h \
 	forecast.h \
@@ -150,6 +152,7 @@ SRCS = adf_kpss.c \
 	dbwrite.c \
 	describe.c \
 	discrete.c \
+	embeddedRCall.c \
 	estimate.c \
 	flow_control.c \
 	forecast.c \
diff -urNp gretl/gretl/lib/src/embeddedRCall.c gretl3/lib/src/embeddedRCall.c
--- gretl/gretl/lib/src/embeddedRCall.c	1970-01-01 01:00:00.000000000 +0100
+++ gretl3/lib/src/embeddedRCall.c	2009-07-04 22:09:20.000000000 +0100
@@ -0,0 +1,46 @@
+#include <Rembedded.h>
+#include "embeddedRCall.h"
+
+int
+eval_R_command(const char *funcName, int argc, char *argv[])
+{
+    SEXP e;
+    SEXP arg;
+
+    int i;
+    int errorOccurred;
+    init_R(argc, argv);
+
+    PROTECT(arg = allocVector(INTSXP, 10));
+    for(i = 0; i < LENGTH(arg); i++) INTEGER(arg)[i]  = i + 1;
+
+    PROTECT(e = lang2(install(funcName), arg));
+
+    /* Evaluate the call to the R function.
+       Ignore the return value.
+    */
+    R_tryEval(e, R_GlobalEnv, &errorOccurred);
+
+    Rf_endEmbeddedR(0);
+    UNPROTECT(2);   
+    return(0);
+}
+
+void
+init_R(int argc, char **argv)
+{
+    int defaultArgc = 1;
+    char *defaultArgv[] = {"Rtest"};
+
+    if(argc == 0 || argv == NULL) {
+	argc = defaultArgc;
+	argv = defaultArgv;
+    }
+    Rf_initEmbeddedR(argc, argv);
+}
+
+void
+end_R()
+{
+    Rf_endEmbeddedR(0);
+}
diff -urNp gretl/gretl/lib/src/embeddedRCall.h gretl3/lib/src/embeddedRCall.h
--- gretl/gretl/lib/src/embeddedRCall.h	1970-01-01 01:00:00.000000000 +0100
+++ gretl3/lib/src/embeddedRCall.h	2009-07-04 21:49:48.000000000 +0100
@@ -0,0 +1,11 @@
+#ifndef EMBEDDED_R_CALL_H
+#define EMBEDDED_R_CALL_H
+
+#include <R.h>
+#include <Rinternals.h>
+
+int eval_R_command(const char *funcName, int argc, char *argv[]);
+void init_R(int argc, char **argv);
+void end_R();
+
+#endif
diff -urNp gretl/gretl/lib/src/geneval.c gretl3/lib/src/geneval.c
--- gretl/gretl/lib/src/geneval.c	2009-07-02 21:32:57.000000000 +0100
+++ gretl3/lib/src/geneval.c	2009-07-05 14:51:13.000000000 +0100
@@ -29,6 +29,7 @@
 #include "kalman.h"
 #include "libset.h"
 #include "version.h"
+#include "gretl_foreign.h"
 
 #include <errno.h>
 
@@ -4499,6 +4500,110 @@ static NODE *eval_ufunc (NODE *t, parser
     return ret;
 }
 
+
+/* evaluate a R function */
+
+static NODE *eval_Rfunc (NODE *t, parser *p)
+{
+    fnargs *args = NULL;
+    int argc = 0;
+    NODE *l = t->v.b2.l;
+    NODE *r = t->v.b2.r;
+    int i, m = r->v.bn.n_nodes;
+    const char *funname = l->v.str;
+    int rtype = GRETL_TYPE_NONE;
+    NODE *n, *ret = NULL;
+
+
+    /* evaluate the function arguments */
+    /* find the function */
+    int status=gretl_R_get_call(funname,m);
+	if (status) {
+		p->err=1;
+		return NULL;
+	}
+
+    for (i=0; i<m && !p->err; i++) {
+	if (useries_node(r->v.bn.n[i])) {
+	    /* let dataset variables through "as is" */
+	    n = r->v.bn.n[i];
+	} else if (uscalar_node(r->v.bn.n[i])) {
+	    /* let predefined scalars through "as is" */
+	    n = r->v.bn.n[i];
+	} else {
+	    n = eval(r->v.bn.n[i], p);
+	    if (n == NULL) {
+		fprintf(stderr, "%s: failed to evaluate arg %d\n", funname, i); 
+	    } else if (!ok_ufunc_sym(n->t)) {
+		fprintf(stderr, "%s: node type %d: not OK\n", funname, n->t);
+		p->err = E_TYPES;
+	    }
+	}
+
+	if (p->err) {
+	    break;
+	}
+
+#if EDEBUG
+	fprintf(stderr, "%s: arg[%d] is of type %d\n", funname, i, n->t);
+#endif
+
+	if  (uscalar_node(n)) {
+	    gretl_R_function_add_Scalar(n->v.xval);
+	} else if (n->t == NUM) {
+	    gretl_R_function_add_Scalar(n->v.xval);
+	} else if (n->t == MAT) {
+	    gretl_R_function_add_Matrix(n->v.m);
+	} else {
+	    fprintf(stderr, "argument not supported");
+	    p->err = 1;
+	    return NULL;
+	}
+	if (p->err) {
+	    fprintf(stderr, "eval_Rfunc: error evaluating arg %d\n", i);
+	}
+    }
+
+    /* try sending args to function */
+
+    if (!p->err) {
+	void *retp = NULL;
+	double xret = NADBL;
+    	retp = &xret;
+
+
+	p->err = gretl_R_function_exec(funname, &rtype, &retp);
+
+	if (!p->err) {
+	    if (rtype == GRETL_TYPE_DOUBLE) {
+		ret = aux_scalar_node(p);
+		if (ret != NULL) {
+		    ret->v.xval = xret;
+		}
+	    } else if (rtype == GRETL_TYPE_MATRIX) {
+		gretl_matrix *mret = NULL;
+		ret = aux_matrix_node(p);
+		if (ret != NULL) {
+		    if (is_tmp_node(ret)) {
+			gretl_matrix_free(ret->v.m);
+		    }
+		    mret= (gretl_matrix *) retp;
+		    ret->v.m = mret;
+		}
+	    } 
+	}
+
+    }
+
+#if EDEBUG
+    fprintf(stderr, "eval_Rfunc: p->err = %d, ret = %p\n", 
+	    p->err, (void *) ret);
+#endif
+
+    return ret;
+}
+
+
 static gretl_matrix *get_corrgm_matrix (NODE *l,
 					NODE *m,
 					NODE *r,
@@ -6752,6 +6857,9 @@ static NODE *eval (NODE *t, parser *p)
     case UFUN:
 	ret = eval_ufunc(t, p);
 	break;
+    case RFUN:
+	ret = eval_Rfunc(t, p);
+	break;
     case QUERY:
 	ret = eval_query(t, p);
 	break;
diff -urNp gretl/gretl/lib/src/geneval.c~ gretl3/lib/src/geneval.c~
--- gretl/gretl/lib/src/geneval.c~	2009-06-18 08:28:25.000000000 +0100
+++ gretl3/lib/src/geneval.c~	2009-07-05 14:49:26.000000000 +0100
@@ -29,6 +29,7 @@
 #include "kalman.h"
 #include "libset.h"
 #include "version.h"
+#include "gretl_foreign.h"
 
 #include <errno.h>
 
@@ -4019,9 +4020,12 @@ static NODE *series_series_func (NODE *l
 	    p->err = bkbp_filter(x, y, p->dinfo);
 	    break;
 	case F_FRACDIFF:
-	    p->err = fracdiff_series(x, y, r->v.xval, p->dinfo);
+	    p->err = fracdiff_series(x, y, r->v.xval, 1, (autoreg(p))? p->obs : -1, p->dinfo);
 	    break;
-        case F_BOXCOX:
+	case F_FRACLAG:
+	    p->err = fracdiff_series(x, y, r->v.xval, 0, (autoreg(p))? p->obs : -1, p->dinfo);
+	    break;
+	case F_BOXCOX:
 	    p->err = boxcox_series(x, y, r->v.xval, p->dinfo);
 	    break;
 	case F_DIFF:
@@ -4496,6 +4500,120 @@ static NODE *eval_ufunc (NODE *t, parser
     return ret;
 }
 
+
+/* evaluate a R function */
+
+static NODE *eval_Rfunc (NODE *t, parser *p)
+{
+    fnargs *args = NULL;
+    int argc = 0;
+    NODE *l = t->v.b2.l;
+    NODE *r = t->v.b2.r;
+    int i, m = r->v.bn.n_nodes;
+    const char *funname = l->v.str;
+    int rtype = GRETL_TYPE_NONE;
+    NODE *n, *ret = NULL;
+
+
+    /* evaluate the function arguments */
+    /* find the function */
+    int status=gretl_R_get_call(funname,m);
+	if (status) {
+		p->err=1;
+		return NULL;
+	}
+
+    for (i=0; i<m && !p->err; i++) {
+	if (useries_node(r->v.bn.n[i])) {
+	    /* let dataset variables through "as is" */
+	    n = r->v.bn.n[i];
+	} else if (uscalar_node(r->v.bn.n[i])) {
+	    /* let predefined scalars through "as is" */
+	    n = r->v.bn.n[i];
+	} else {
+	    n = eval(r->v.bn.n[i], p);
+	    if (n == NULL) {
+		fprintf(stderr, "%s: failed to evaluate arg %d\n", funname, i); 
+	    } else if (!ok_ufunc_sym(n->t)) {
+		fprintf(stderr, "%s: node type %d: not OK\n", funname, n->t);
+		p->err = E_TYPES;
+	    }
+	}
+
+	if (p->err) {
+	    break;
+	}
+
+#if EDEBUG
+	fprintf(stderr, "%s: arg[%d] is of type %d\n", funname, i, n->t);
+#endif
+
+	if  (uscalar_node(n)) {
+	    gretl_R_function_add_Scalar(n->v.xval);
+	} else if (n->t == NUM) {
+	    gretl_R_function_add_Scalar(n->v.xval);
+	} else if (n->t == MAT) {
+	    gretl_R_function_add_Matrix(n->v.m);
+	} else {
+	    fprintf(stderr, "argument not supported");
+	    p->err = 1;
+	    return NULL;
+	}
+	if (p->err) {
+	    fprintf(stderr, "eval_Rfunc: error evaluating arg %d\n", i);
+	}
+    }
+
+    /* try sending args to function */
+
+    if (!p->err) {
+	void *retp = NULL;
+	double xret = NADBL;
+    	retp = &xret;
+
+
+	if ((p->flags & P_UFRET) && 
+	    (rtype == GRETL_TYPE_DOUBLE || rtype == GRETL_TYPE_SERIES)) {
+	    /* pick up description of generated var, if any */
+	    pdescrip = &descrip;
+	}
+
+	p->err = gretl_R_function_exec(funname, &rtype, &retp);
+
+	if (!p->err) {
+	    if (rtype == GRETL_TYPE_DOUBLE) {
+		ret = aux_scalar_node(p);
+		if (ret != NULL) {
+		    ret->v.xval = xret;
+		}
+	    } else if (rtype == GRETL_TYPE_MATRIX) {
+		gretl_matrix *mret = NULL;
+		ret = aux_matrix_node(p);
+		if (ret != NULL) {
+		    if (is_tmp_node(ret)) {
+			gretl_matrix_free(ret->v.m);
+		    }
+		    mret= (gretl_matrix *) retp;
+		    ret->v.m = mret;
+		}
+	    } 
+	}
+
+	if (descrip != NULL) {
+	    strcpy(p->lh.label, descrip);
+	    free(descrip);
+	}
+    }
+
+#if EDEBUG
+    fprintf(stderr, "eval_Rfunc: p->err = %d, ret = %p\n", 
+	    p->err, (void *) ret);
+#endif
+
+    return ret;
+}
+
+
 static gretl_matrix *get_corrgm_matrix (NODE *l,
 					NODE *m,
 					NODE *r,
@@ -4700,7 +4818,8 @@ static NODE *eval_3args_func (NODE *l, N
 }
 
 /* Bessel function handler: the 'r' node can be of scalar, series or
-   matrix type.  Right now, this only supports scalar order.
+   matrix type.  Right now, this only supports scalar order ('m'
+   node).
 */
 
 static NODE *eval_bessel_func (NODE *l, NODE *m, NODE *r, parser *p) 
@@ -4709,7 +4828,7 @@ static NODE *eval_bessel_func (NODE *l, 
     double v;
     NODE *ret = NULL;
 
-    if (!starting(p)) {
+    if (!starting(p) && r->t != VEC) {
 	return aux_any_node(p);
     }
 
@@ -4742,8 +4861,8 @@ static NODE *eval_bessel_func (NODE *l, 
 	ret = aux_vec_node(p, p->dinfo->n);
 	if (ret != NULL) {
 	    const double *x = r->v.xvec;
-	    int t1 = p->dinfo->t1;
-	    int t2 = p->dinfo->t2;
+	    int t1 = (autoreg(p))? p->obs : p->dinfo->t1;
+	    int t2 = (autoreg(p))? p->obs : p->dinfo->t2;
 	    int t;
 
 	    for (t=t1; t<=t2 && !p->err; t++) {
@@ -6374,6 +6493,7 @@ static NODE *eval (NODE *t, parser *p)
     case F_HPFILT:
     case F_BKFILT:
     case F_FRACDIFF:
+    case F_FRACLAG:
     case F_BOXCOX:
     case F_PNOBS:
     case F_PMIN:
@@ -6747,6 +6867,9 @@ static NODE *eval (NODE *t, parser *p)
     case UFUN:
 	ret = eval_ufunc(t, p);
 	break;
+    case RFUN:
+	ret = eval_Rfunc(t, p);
+	break;
     case QUERY:
 	ret = eval_query(t, p);
 	break;
@@ -8026,9 +8149,10 @@ static void assign_to_matrix_mod (parser
 	gretl_matrix_free(b);
     }
 
+    p->lh.m1 = m;
+
     if (!p->err) {
 	p->err = user_matrix_replace_matrix_by_name(p->lh.name, m);
-	p->lh.m1 = m;
     }
 }
 
diff -urNp gretl/gretl/lib/src/genlex.c gretl3/lib/src/genlex.c
--- gretl/gretl/lib/src/genlex.c	2009-07-01 18:48:43.000000000 +0100
+++ gretl3/lib/src/genlex.c	2009-07-05 14:30:29.000000000 +0100
@@ -819,6 +819,9 @@ static void look_up_word (const char *s,
 		} else if (get_user_function_by_name(s)) {
 		    p->sym = UFUN;
 		    p->idstr = gretl_strdup(s);
+		} else if (get_R_function_by_name(s)) {
+		    p->sym = RFUN;
+		    p->idstr = gretl_strdup(s);
 		} else if (string_is_defined(s)) {
 		    p->sym = VSTR;
 		    p->idstr = gretl_strdup(s);
@@ -869,7 +872,8 @@ static void word_check_next_char (const 
 		   !func2_symb(p->sym) &&
 		   !func3_symb(p->sym) &&
 		   !funcn_symb(p->sym) && 
-		   p->sym != UFUN) {
+		   p->sym != UFUN &&
+		   p->sym != RFUN) {
 	    p->err = 1;
 	} 
     } else if (p->ch == '[') {
@@ -1386,6 +1390,8 @@ const char *getsymb (int t, const parser
 	    return mvarname(p->idnum);
 	} else if (t == UFUN) {
 	    return p->idstr;
+	} else if (t == RFUN) {
+	    return p->idstr;
 	} else if (t == STR) {
 	    return p->idstr;
 	}
diff -urNp gretl/gretl/lib/src/genparse.h gretl3/lib/src/genparse.h
--- gretl/gretl/lib/src/genparse.h	2009-06-19 10:06:45.000000000 +0100
+++ gretl3/lib/src/genparse.h	2009-07-05 14:30:47.000000000 +0100
@@ -111,6 +111,7 @@ enum {
   /* 80 */    INC,   
 	      DEC,
 	      QUERY,
+	      RFUN,	  /* R function call */
 	      EOT,	  /* end of transmission */
 	      UNK 
 };
@@ -356,7 +357,8 @@ enum {
                    !func2_symb(s) && s != EROOT)
 
 #define b2sym(s) (evalb2(s) || s == DMSTR || s == DMSL || \
-                  s == OVAR || s == UFUN || s == LISTVAR)
+                  s == OVAR || s == UFUN || s == LISTVAR || \
+				  s == RFUN)
 
 #define b3sym(s) (s == QUERY || func3_symb(s))
 
diff -urNp gretl/gretl/lib/src/gensyntax.c gretl3/lib/src/gensyntax.c
--- gretl/gretl/lib/src/gensyntax.c	2009-06-19 10:06:45.000000000 +0100
+++ gretl3/lib/src/gensyntax.c	2009-07-05 14:30:58.000000000 +0100
@@ -978,6 +978,16 @@ static NODE *powterm (parser *p)
 		get_args(t->v.b2.r, p, -1, opt, &next);
 	    }
 	}
+    } else if (sym == RFUN) {
+	t = newb2(sym, NULL, NULL);
+	if (t != NULL) {
+	    t->v.b2.l = newstr(p, STR);
+	    lex(p);
+	    t->v.b2.r = newbn(FARGS);
+	    if (t != NULL) {
+		get_args(t->v.b2.r, p, -1, opt, &next);
+	    }
+	}
     } else if (funcn_symb(sym)) {
 	t = newb1(sym, NULL);
 	if (t != NULL) {
diff -urNp gretl/gretl/lib/src/gretl_foreign.c gretl3/lib/src/gretl_foreign.c
--- gretl/gretl/lib/src/gretl_foreign.c	2009-01-26 20:34:31.000000000 +0000
+++ gretl3/lib/src/gretl_foreign.c	2009-07-05 14:32:11.000000000 +0100
@@ -19,6 +19,7 @@
 
 #include "libgretl.h"
 #include <glib.h>
+#include "embeddedRCall.h"
 
 #ifdef G_OS_WIN32
 # include <windows.h>
@@ -34,13 +35,16 @@ static int foreign_n_lines; 
 static int foreign_lang;
 static gretlopt foreign_opt;
 
+static int Rinit=0;
+
 /* foreign_opt may include OPT_D to send data, OPT_Q to operate
    quietly (don't display output from foreign program)
 */
 
 enum {
     LANG_R = 1,
-    LANG_OX
+    LANG_OX,
+	LANG_RLib
 };
 
 static void destroy_foreign (void)
@@ -60,6 +64,8 @@ static int set_foreign_lang (const char 
 	foreign_lang = LANG_R;
     } else if (!strcmp(lang, "ox")) {
 	foreign_lang = LANG_OX;
+    } else if (!strcmp(lang, "RLib")) {
+	foreign_lang = LANG_RLib;
     } else {
 	pprintf(prn, "%s: unknown language\n");
 	err = E_DATA;
@@ -181,6 +187,152 @@ static int lib_run_R_sync (gretlopt opt,
 
 #endif
 
+static SEXP current_arg;
+static SEXP current_call;
+/*
+ This arranges for the command source("foo.R")
+ to be called and this defines the function we will
+ call in bar1.
+ */
+void
+source(const char *name)
+{
+    SEXP e;
+
+    PROTECT(e = lang2(install("source"), mkString(name)));
+    R_tryEval(e, R_GlobalEnv, NULL);
+    UNPROTECT(1);
+}
+
+
+int lib_run_RLib_sync(gretlopt opt, PRN *prn) {
+	if (! Rinit) {
+		char *localArgs[] = {"R", "--silent"};
+		#ifdef G_OS_WIN32
+    	err = !SetEnvironmentVariable("R_PROFILE", Rprofile);
+		#else
+    	setenv("R_HOME", "/usr/lib/R", 1);
+		#endif
+    	init_R(sizeof(localArgs)/sizeof(localArgs[0]), localArgs);
+		Rinit=1;
+	}
+	gchar *Rsrc = g_strdup_printf("%sRsrc", gretl_dot_dir());
+	source(Rsrc);
+}
+
+int get_R_function_by_name(char *name) {
+    SEXP fun;
+    SEXP e;
+
+    fun = findFun(install(name), R_GlobalEnv);
+    if(fun == R_NilValue) {
+	UNPROTECT(1);
+	return 0;
+    }
+    return 1;
+}
+
+void gretl_R_function_add_Scalar(double value) {
+	current_arg=CDR(current_arg);
+    SETCAR( current_arg, ScalarReal(value));
+    return;	
+}
+
+void gretl_R_function_add_Vector(double *values, int t1, int t2) {
+
+    SEXP res = allocVector(REALSXP, t2-t1+1);
+
+	current_arg=CDR(current_arg);
+
+	int i;
+    for (i=t1; i<=t2; i++) {
+    	REAL(res)[i-t1] = values[i];
+    }
+    
+    SETCAR(current_arg, res);
+    return;	
+}
+
+void gretl_R_function_add_Matrix(gretl_matrix *values) {
+
+	int nr = gretl_matrix_rows(values);
+	int nc = gretl_matrix_cols(values);
+
+	current_arg=CDR(current_arg);
+
+    SEXP res = allocMatrix(REALSXP, nr , nc);
+
+	int i,j;
+    for (i=0; i<nr; i++) {
+	    for (j=0; j<nc; j++) {
+    		REAL(res)[i + j * nr] = gretl_matrix_get(values, i, j);
+    	}
+    }
+    
+    SETCAR(current_arg, res);
+    return;	
+}
+
+int gretl_R_get_call(char *name, int numArg) {
+
+    SEXP call;
+    SEXP e;
+    call = findFun(install(name), R_GlobalEnv);
+    if(call == R_NilValue) {
+	fprintf(stderr, "No definition for function %s\n", name);
+	UNPROTECT(1);
+	return -1;
+    }
+    PROTECT(e = allocList(numArg+1));
+	SET_TYPEOF(e, LANGSXP);
+	SETCAR(e, install(name));
+    current_call=e;
+	current_arg=e;
+	return 0;
+}
+
+int
+gretl_R_function_exec(char *name, int *rtype, void **ret) 
+{
+
+    //PrintValue(current_call);
+    //eval(current_call, R_GlobalEnv);
+
+    //SETCAR(current_call, install(name));
+    PrintValue(current_call);
+    SEXP res=R_tryEval(current_call, R_GlobalEnv, NULL);
+
+	if (isMatrix(res)) {
+		gretl_matrix *pm;
+		int nc=ncols(res);
+		int nr=nrows(res);
+		pm = gretl_matrix_alloc(nr, nc);
+		int i,j;
+		for (i=0; i<nr; i++) {
+			for (j=0; j<nc; j++) {
+				gretl_matrix_set(pm, i, j,REAL(res)[i + j * nr]);
+			}
+		}
+		UNPROTECT(1);
+		*ret= (void *) pm;
+		*rtype = GRETL_TYPE_MATRIX;
+		return 0;
+	} else if (isVectorAtomic(res)) {
+		double *resD1, *resD2;
+		resD1=REAL(res);
+
+		resD2=*ret;
+	
+		*resD2=*resD1;
+	
+
+    	UNPROTECT(1);
+		*rtype = GRETL_TYPE_DOUBLE;
+		return 0;
+	}
+	return -1;
+}
+
 /* write out current dataset in R format, and, if this succeeds,
    write appropriate R commands to @fp to source the data
 */
@@ -485,8 +637,14 @@ int foreign_execute (const double **Z, c
     } else if (foreign_lang == LANG_OX) {
 	pputs(prn, "language=ox: not supported yet\n");
 	err = E_NOTIMP;
-    }
-    
+    } else if (foreign_lang == LANG_RLib ) {
+	err = write_gretl_R_files(NULL, Z, pdinfo, foreign_opt);
+	if (err) {
+	    delete_gretl_R_files();
+	} else {
+	    lib_run_RLib_sync(foreign_opt, prn);
+	}
+    }    
     destroy_foreign();
 
     return err;
diff -urNp gretl/gretl/lib/src/gretl_foreign.h gretl3/lib/src/gretl_foreign.h
--- gretl/gretl/lib/src/gretl_foreign.h	2008-05-25 16:07:15.000000000 +0100
+++ gretl3/lib/src/gretl_foreign.h	2009-07-05 14:32:25.000000000 +0100
@@ -30,4 +30,11 @@ int write_gretl_R_files (const char *buf
 
 void delete_gretl_R_files (void);
 
+gretl_R_function_exec(char *name, int *rtype, void *ret);
+int get_R_function_by_name(char *name);
+int gretl_R_get_call(char *name, int numargs);
+void gretl_R_function_add_Scalar(double value);
+void gretl_R_function_add_Vector(double *values, int t1, int t2);
+void gretl_R_function_add_Matrix(gretl_matrix *values);
+
 #endif /* GRETL_FOREIGN_H */
