Re: [Rd] print.closure at the R level

2009-04-19 Thread Romain Francois

Yesterday's patch did not print the attributes. This one seems fine:

> f <- function(){}
> attr( f, "yada" ) <- function( ) "lobster bisk"
> f
function(){}
attr(,"yada")
function( ) "lobster bisk"

Romain

Romain Francois wrote:

Duncan Murdoch wrote:

On 18/04/2009 10:12 AM, Romain Francois wrote:

Hello,

Could the code that auto prints a function/closure be extracted from 
print.c so that there would be a print.closure function.
I would like to be able to mask a print.closure function so that I 
have a custom auto-print. One reason for that is I plan to have 
syntax highlighting within the R console.


The class of a closure is "function", so you'd want the method to be 
print.function.  Currently that doesn't work for auto printing, so 
your suggestion is still interesting.  (I'm not sure why auto 
printing is special here...)


Duncan Murdoch

The attached patch implements exposing the print.function at the R level.

Romain



__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel



--
Romain Francois
Independent R Consultant
+33(0) 6 28 91 30 30
http://romainfrancois.blog.free.fr


Index: src/include/Internal.h
===
--- src/include/Internal.h	(revision 48350)
+++ src/include/Internal.h	(working copy)
@@ -370,6 +370,7 @@
 SEXP do_printdefault(SEXP, SEXP, SEXP, SEXP);
 SEXP do_printDeferredWarnings(SEXP, SEXP, SEXP, SEXP);
 SEXP do_printdf(SEXP, SEXP, SEXP, SEXP);
+SEXP do_printfunction(SEXP, SEXP, SEXP, SEXP);
 SEXP do_prmatrix(SEXP, SEXP, SEXP, SEXP);
 SEXP do_proctime(SEXP, SEXP, SEXP, SEXP);
 SEXP do_psort(SEXP, SEXP, SEXP, SEXP);
Index: src/library/base/R/print.R
===
--- src/library/base/R/print.R	(revision 48350)
+++ src/library/base/R/print.R	(working copy)
@@ -85,3 +85,8 @@
 print(noquote(cbind("_"=unlist(x))), ...)
 
 `[.simple.list` <- `[.listof`
+
+print.function <- function( x, useSource=TRUE, ...){
+	.Internal( print.function( x, useSource, ... ) )
+}
+
Index: src/main/names.c
===
--- src/main/names.c	(revision 48350)
+++ src/main/names.c	(working copy)
@@ -631,6 +631,7 @@
 {"readline",	do_readln,	0,	11,	1,	{PP_FUNCALL, PREC_FN,	0}},
 {"menu",	do_menu,	0,	11,	1,	{PP_FUNCALL, PREC_FN,	0}},
 {"print.default",do_printdefault,0,	111,	9,	{PP_FUNCALL, PREC_FN,	0}},
+{"print.function",do_printfunction,0,	111,	3,	{PP_FUNCALL, PREC_FN,	0}},
 {"prmatrix",	do_prmatrix,	0,	111,	6,	{PP_FUNCALL, PREC_FN,	0}},
 {"invisible",	do_invisible,	0,	101,	1,	{PP_FUNCALL, PREC_FN,	0}},
 {"gc",		do_gc,		0,	11,	2,	{PP_FUNCALL, PREC_FN,	0}},
Index: src/main/print.c
===
--- src/main/print.c	(revision 48350)
+++ src/main/print.c	(working copy)
@@ -154,6 +154,35 @@
 return x;
 }/* do_prmatrix */
 
+
+/* .Internal( print.function( f,useSource,... ) ) */
+SEXP attribute_hidden do_printfunction( SEXP call, SEXP op, SEXP args, SEXP rho){
+	SEXP s,t;
+	Rboolean useSource = TRUE; 
+	s=CAR(args);
+	args = CDR(args);	useSource=asLogical(CAR(args));
+	
+	int i;
+	if( ! (TYPEOF(s) == CLOSXP || TYPEOF(s) == LANGSXP ) ) return R_NilValue;
+	t = getAttrib(s, R_SourceSymbol);
+	if (!isString(t) || !useSource)
+	t = deparse1(s, 0, useSource | DEFAULTDEPARSE);
+	for (i = 0; i < LENGTH(t); i++)
+	Rprintf("%s\n", CHAR(STRING_ELT(t, i))); /* translated */
+#ifdef BYTECODE
+	if (TYPEOF(s) == CLOSXP && isByteCode(BODY(s)))
+	Rprintf("\n", BODY(s));
+#endif
+	if (TYPEOF(s) == CLOSXP) {
+	t = CLOENV(s);
+	if (t != R_GlobalEnv)
+		Rprintf("%s\n", EncodeEnvironment(t));
+		printAttributes( s, t, FALSE ) ;
+	}
+	return R_NilValue;
+}
+
+
 /* .Internal(print.default(x, digits, quote, na.print, print.gap,
 			   right, max, useS4)) */
 SEXP attribute_hidden do_printdefault(SEXP call, SEXP op, SEXP args, SEXP rho)
@@ -646,23 +675,15 @@
 case EXPRSXP:
 	PrintExpression(s);
 	break;
+case LANGSXP:
 case CLOSXP:
-case LANGSXP:
-	t = getAttrib(s, R_SourceSymbol);
-	if (!isString(t) || !R_print.useSource)
-	t = deparse1(s, 0, R_print.useSource | DEFAULTDEPARSE);
-	for (i = 0; i < LENGTH(t); i++)
-	Rprintf("%s\n", CHAR(STRING_ELT(t, i))); /* translated */
-#ifdef BYTECODE
-	if (TYPEOF(s) == CLOSXP && isByteCode(BODY(s)))
-	Rprintf("\n", BODY(s));
-#endif
-	if (TYPEOF(s) == CLOSXP) {
-	t = CLOENV(s);
-	if (t != R_GlobalEnv)
-		Rprintf("%s\n", EncodeEnvironment(t));
+	{
+		SEXP call;
+		PROTECT( call = lang2(install("print.function"), s));
+		eval(call,env);
+		UNPROTECT(1);
+		break;
 	}
-	break;
 case ENVSXP:
 	Rprintf("%s\n", EncodeEnvironment(s));
 	break;
@@ -905,7 +926,13 @@
 	PROTECT(call = lang2(install("print"), s));
 	eval(call, env);
 	UNPROTECT(1);
-} else PrintValueRec(s, e

Re: [Rd] wish list: automatic package installation

2009-04-19 Thread Hiroyuki Kawakatsu
Hi,

Apologies for using this thread for something that is only
tangentially related.

Is there a mechanism in R to ensure that the configure.args used to
build R is also used when installing packages? If not, I would like to
add this as wish (perhaps we can grab this info from config.log?) At
the moment, I add the line

options(configure.args="...")

in my .Rprofile and install.packages() would use them. However, if I
change these when building R, I need to remember to change my
.Rprofile as well.

h.

On Sun, Apr 19, 2009, John Fox wrote:
>
> Dear list members,
>
> The release of R 2.9.0, reminds me of a long-standing nit that I have to
> pick. I prefer not to transfer and update all of the packages in my old
> library, because I see a new release of R as an opportunity to start with a
> clean slate. I'd rather install packages as I need them. My personal
> solution is to use the following function in place of library():
>
> package <- function(package, dependencies=TRUE, ...){
> package <- as.character(substitute(package))
> if (!(package %in% .packages(all.available=TRUE)))
>install.packages(package, dependencies=dependencies)
>library(package, character.only=TRUE, ...)
>}
>
> I'm sure that this function could be improved, and possibly I've missed a
> facility that's already available. If not, it would be nice if the library()
> command automatically tried to download and install missing packages
> (perhaps if an option is set).
>
> Regards,
>  John
>
> --
> John Fox, Professor
> Department of Sociology
> McMaster University
> Hamilton, Ontario, Canada
> web: socserv.mcmaster.ca/jfox

-- 
+---
| Hiroyuki Kawakatsu
| Business School, Dublin City University
| Dublin 9, Ireland. Tel +353 (0)1 700 7496

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel


Re: [Rd] export C++ array to R

2009-04-19 Thread Simon Urbanek


On Apr 18, 2009, at 4:12 AM, whizvast wrote:



Hi, I am a newbie on C++

Right now I have an array of doubles in C++.

Is there a way to "export" that array into R? Of course, I can  
allocate
the memory block first using "allocVector" and copying the array  
contents

one by one.

But, what if that array is fairly large? Copying doesn't look that
efficient.
I was thinking of setting the data pointer(DATAPTR) point to that  
array,

and adjust the LENGTH of SEXP. But I don't know how to do that.



The short answer is no, you can't, because the memory has to be  
allocated by R. If you are allocating the memory yourself, you can  
simply use allocVector instead of malloc/new to allocate the array in  
the first place - that saves you the copying and is the more usual  
approach for such R packages.


Cheers,
Simon

__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel