[Rd] inconsistency in ?factor

2009-05-25 Thread Petr Savicky
In the almost current development version (2009-05-22 r48594) and also in 
  https://svn.r-project.org/R/trunk/src/library/base/man/factor.Rd
?factor contains (compare the formulations marked by ^^)

\section{Warning}{
  The interpretation of a factor depends on both the codes and the
  \code{"levels"} attribute.  Be careful only to compare factors with
  the same set of levels (in the same order).  
  ^

\section{Comparison operators and group generic methods}{
  ...

  Only \code{==} and \code{!=} can be used for factors: a factor can
  only be compared to another factor with an identical set of levels
  (not necessarily in the same ordering) or to a character vector.
   

In the development version 2009-05-22 r48594, the latter formulation
"not necessarily in the same ordering" is correct.

  f1 <- factor(c("a", "b", "c", "c", "b", "a", "a"), levels=c("a", "b", "c"))
  f2 <- factor(c("a", "b", "c", "c", "b", "a", "c"), levels=c("c", "b", "a"))
  f1 == f2 # [1]  TRUE  TRUE  TRUE  TRUE  TRUE  TRUE FALSE

The first formulation "Be careful to compare ... levels in the same order" may
be just a warning against a potential problem if the levels have different 
order,
however this is not clear.

Petr.

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


Re: [Rd] inconsistency in ?factor

2009-05-25 Thread Berwin A Turlach
G'day Petr,

On Mon, 25 May 2009 09:04:14 +0200
Petr Savicky  wrote:

> The first formulation "Be careful to compare ... levels in the same
> order" may be just a warning against a potential problem if the
> levels have different order, however this is not clear.

Well, the first statement is a remark on comparison in general while
the second statement is specific to "comparison operators and generic
methods".  There are other ways of comparing objects; note:

R> f1 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("a", "b", "c"))
R> f2 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("c", "b", "a"))
R> f1==f2
[1] TRUE TRUE TRUE TRUE TRUE TRUE
R> identical(f1,f2)
[1] FALSE
R> all.equal(f1,f2)
[1] "Attributes: < Component 2: 2 string mismatches >"

Just my 2c.

Cheers,

Berwin

=== Full address =
Berwin A TurlachTel.: +65 6516 4416 (secr)
Dept of Statistics and Applied Probability+65 6516 6650 (self)
Faculty of Science  FAX : +65 6872 3919   
National University of Singapore 
6 Science Drive 2, Blk S16, Level 7  e-mail: sta...@nus.edu.sg
Singapore 117546http://www.stat.nus.edu.sg/~statba

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


Re: [Rd] [R] step by step debugger in R?

2009-05-25 Thread Simon Urbanek


On May 24, 2009, at 10:18 AM, Romain Francois wrote:


Robert Gentleman wrote:

Hi,
 I stripped the cc's as I believe that all read this list.

Romain Francois wrote:


[moving this to r-devel]

Robert Gentleman wrote:


Hi,

Romain Francois wrote:


Duncan Murdoch wrote:


On 5/22/2009 10:59 AM, Michael wrote:

Really I think if there is a Visual Studio strength debugger,  
our
collective time spent in developing R code will be greatly  
reduced.


If someone who knows how to write a debugger plugin for Eclipse  
wants
to help, we could have that fairly easily.  All the  
infrastructure is

there; it's the UI part that's missing.

Duncan Murdoch

[I've copied Mark Bravington and Robert Gentleman to the list as  
they
are likely to have views here, and I am not sure they monitor R- 
help]


Hello,

Making a front-end to debugging was one of the proposed google  
summer of
code for this year [1], it was not retained eventually, but I am  
still

motivated.

Pretty much all infrastructure is there, and some work has been  
done
__very recently__ in R's debugging internals (ability to step  
up). As I
see it, the ability to call some sort of hook each time the  
debugger

waits for input would make it much easier for someone to write

I have still not come to an understanding of what this is  
supposed to

do? When
you have the browser prompt you can call any function or code you  
want

to. There
is no need for something special to allow you to do that.


Sure. What I have in mind is something that gets __automatically__
called, similar to the task callback but happening right before  
the user

is given the browser prompt.



I am trying to understand the scenario you have in mind. Is it that  
the user is
running R directly and your debugger is essentially a helper  
function that gets

updated etc as R runs?

yes. there are now several ui toolkits that could be use to give  
some sort of graphical representation of what is being debugged.


I agree with you that and IDE should command R and not the other way  
around, but I am not (yet) here talking about a fully featured IDE,  
but simply a debugger.


I need to read more about embedding R (as in section 8 of WRE). I  
know you can supply your own implementation of the REPL, but I am  
not sure this includes the one that goes on once trapped into the  
browser.


Yes - it would be quite useless otherwise ;)  there are many examples  
of GUIs that use it (including the built-in ones [Windows, MAc, ..] or  
external ones e.g JGR).


Cheers,
S


For example littler ships its own REPL, but this does not seem to  
fool/deal with browser:


$ r -e "print(1:10); browser(); print(1:5) "
[1]  1  2  3  4  5  6  7  8  9 10
Called from: NULL
c
[1]  1  2  3  4  5

Not sure this is an omission of Jeffrey and Dirk or something else.

If so, then I don't think that works very well and given the  
constraints we
have with R I don't think it will be able to solve many of the  
problems that an
IDE should.  The hook you want will give you some functionality,  
but no where

near enough.



In the long run, I do agree. In the short run, I'd prefer taking the  
bus to the airport rather than walk.


Let me suggest instead that the IDE should be running the show. It  
should
initialize an instance of R, but it controls all communication and  
hence
controls what is rendered on the client side.  If that is what you  
mean by
embedding R, then yes that is what is needed. There is no way that  
I can see to
support most of the things that IDE type debuggers support without  
the IDE

controlling the communication with R.

And if I am wrong about what your debugger will look like please  
let me know.


best wishes
  Robert




front-ends. A recent post of mine (patch included) [2] on R-devel
suggested a custom prompt for browser which would do the trick,  
but I

now think that a hook would be more appropriate. Without something
similar to that, there is no way that I know of for making a  
front-end,
unless maybe if you embed R ... (please let me know how I am  
wrong)



I think you are wrong. I can't see why it is needed. The external
debugger has
lots of options for handling debugging. It can rewrite code (see
examples in
trace for how John Chambers has done this to support tracing at a
location),
which is AFAIK a pretty standard approach to writing debuggers.  
It can

figure
out where the break point is (made a bit easier by allowing it to  
put

in pieces
of text in the call to browser).  These are things the internal
debugger can't do.



Thanks. I'll have another look into that.


There is also the debug package [3,4] which does __not__ work  
with R
internals but rather works with instrumenting tricks at the R  
level.
debug provides a tcl/tk front-end. It is my understanding that  
it does
not work using R internals (do_browser, ...) because it was not  
possible
at the time, and I believe this is still not possible today, but  
I might

be wrong. I'd prefer to be w

Re: [Rd] inconsistency in ?factor

2009-05-25 Thread Petr Savicky
On Mon, May 25, 2009 at 03:58:06PM +0800, Berwin A Turlach wrote:
> Well, the first statement is a remark on comparison in general while
> the second statement is specific to "comparison operators and generic
> methods".  There are other ways of comparing objects; note:
> 
> R> f1 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("a", "b", "c"))
> R> f2 <- factor(c("a", "b", "c", "c", "b", "a"), levels=c("c", "b", "a"))
> R> f1==f2
> [1] TRUE TRUE TRUE TRUE TRUE TRUE
> R> identical(f1,f2)
> [1] FALSE
> R> all.equal(f1,f2)
> [1] "Attributes: < Component 2: 2 string mismatches >"

I see. We have to distinguish comparison of the objects and their components.

Let me propose the following formulation
  Two factors may be identical only if they have the same set of
  levels (in the same order).
instead of
  Be careful only to compare factors with the same set of
  levels (in the same order).

Petr.

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


Re: [Rd] qbinom (PR#13711)

2009-05-25 Thread maechler
> "PS" == Petr Savicky 
> on Sat, 23 May 2009 18:22:26 +0200 writes:

PS> On Wed, May 20, 2009 at 11:10:11PM +0200, wolfgang.re...@gmail.com 
wrote:
PS> ...
>> Strange behavior of qbinom:
>> 
>> > qbinom(0.01, 5016279, 1e-07)
>> [1] 0
>> > qbinom(0.01, 5016279, 2e-07)
>> [1] 16
>> > qbinom(0.01, 5016279, 3e-07)
>> [1] 16
>> > qbinom(0.01, 5016279, 4e-07)
>> [1] 16
>> > qbinom(0.01, 5016279, 5e-07)
>> [1] 0
>> 

PS> There is a bug in function do_search() in file src/nmath/qbinom.c. This
PS> function contains a cycle
PS> for(;;) {
PS>   if(y == 0 ||
PS>   (*z = pbinom(y - incr, n, pr, /*l._t.*/TRUE, /*log_p*/FALSE)) < p)
PS>   return y;
PS>   y = fmax2(0, y - incr);
PS> }
PS> When this cycle stops, *z contains pbinom(y - incr, ...), but is used
PS> as if it is pbinom(y, ...) for the resulting y.

PS> In the example qbinom(p=0.01, size=5016279, prob=4e-07), we get at 
PS> some step y=15 as a result of a search left with incr=50, so we have
PS> *z=pbinom(-35, ...)=0. Hence, y=15 is treated as too low and is 
increased
PS> to 16. Since 16 is detected to be sufficient, the search stops with 
y=16,
PS> which is wrong.

[]

Thanks to Wolfgang and Petr,

Petr's analysis of the problem seems right on spot to me,
and I'm currently testing the patch (and will also add some
regression tests along Petr's example)
which will make it into R-patched (to be R 2.9.1 in a while) and
R-devel.

Gratefully,
Martin Maechler, ETH Zurich

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


Re: [Rd] (PR#13705) [wishlist, patch] make row() and col() preserve dimnames

2009-05-25 Thread Prof Brian Ripley

On Sun, 17 May 2009, goodr...@fas.harvard.edu wrote:


Full_Name: Ben Goodrich
Version: 2.9.0
OS: Linux (Debian unstable)
Submission from: (NULL) (128.103.220.16)


row(x), col(x), and functions that call them like lower.tri(x) and 
upper.tri(x) do not retain the rownames or colnames of x in the 
matrix that is returned. Example from R version 2.9.0 :


x <- matrix(1:9, nrow = 3, ncol = 3)
rownames(x) <- LETTERS[1:3]
colnames(x) <- letters[1:3]

dimnames(row(x)) # NULL
dimnames(col(x)) # NULL

Is there anyone for whom the expected behavior is to drop the 
dimnames of x ? It is not consistent with other functions of 
matrices.


I suspect everyone who reads the help page carefully.

This is not a 'function of a matrix' x but of dim(x) for a 
'matrix-like' object.  The help page makes this clear to me (I am not 
the author), so I think you have misread it.  There is no question of 
'drop the dimames' nor 'retaining the rownames': it is a new object, 
an integer matrix whatever x was.


These functions are mainly for use in programming, and need to be 
efficient -- so adding dimnames that will never be needed by all the 
existing code is a non-trivial overhead.



By default, row(x) already
returns the row numbers of x (and similarly for col()), so how would seeing
integers in the rownames be helpful?

Without patch:

row(x)

[,1] [,2] [,3]
[1,]111
[2,]222
[3,]333

With patch:

row(x)

 a b c
A 1 1 1
B 2 2 2
C 3 3 3

Patch:
Index: src/library/base/R/matrix.R
===
--- src/library/base/R/matrix.R (revision 48553)
+++ src/library/base/R/matrix.R (working copy)
@@ -104,8 +104,9 @@
labs <- rownames(x, do.NULL=FALSE, prefix="")
res <- factor(.Internal(row(dim(x))), labels=labs)
dim(res) <- dim(x)
-res
-} else .Internal(row(dim(x)))
+} else res <- .Internal(row(dim(x)))
+dimnames(res) <- dimnames(x)
+res
}


Lots of issues here: dimnames() is generic and may not be compatible 
with the object produced, so checks are needed.  And for efficiency 
this should be done in the C-level code that creates the object if 
done at all.



col <- function(x, as.factor=FALSE)
@@ -114,8 +115,9 @@
labs <- colnames(x, do.NULL=FALSE, prefix="")
res <- factor(.Internal(col(dim(x))), labels=labs)
dim(res) <- dim(x)
-res
-} else .Internal(col(dim(x)))
+} else res <- .Internal(col(dim(x)))
+dimnames(res) <- dimnames(x)
+res
}

crossprod <- function(x, y=NULL) .Internal(crossprod(x,y))

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



--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595

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


Re: [Rd] [R] step by step debugger in R?

2009-05-25 Thread Romain Francois

Simon Urbanek wrote:


[snip]


I need to read more about embedding R (as in section 8 of WRE). I 
know you can supply your own implementation of the REPL, but I am not 
sure this includes the one that goes on once trapped into the browser.


Yes - it would be quite useless otherwise ;)  there are many examples 
of GUIs that use it (including the built-in ones [Windows, MAc, ..] or 
external ones e.g JGR).


Cheers,
S


Hi Simon,

Do you mean the rReadConsole callback ? I managed to make some minor 
modifications to the rtest.java example that comes with JRI to somewhat 
emulate automatically call some code (ls.str()) in this example at the 
browser prompt, before giving the prompt to the user.


   static boolean browse_first = true ;  
 public String rReadConsole(Rengine re, String prompt, int 
addToHistory) {

   System.out.print(prompt);
   if( prompt.startsWith( "Browse[") ){
   if( browse_first ){
   System.out.println( "\n re.eval( \" 
print( ls.str() )\" ); " ) ;

   re.eval( "print( ls.str() )" ) ;
   browse_first = false ;
   System.out.println( "\n return 
\"ls.str()\"" ) ;

   return "ls.str()\n" ;
   } else{
   browse_first = true ;
   }
  
   }

...
}

It seems to work and could get me somewhere, although it has a "it 
works, but it does not feel right" taste. Basically the code pretends 
the user typed "ls.str\n" at the browse prompt, so that the R evaluator 
evaluates it, and then comes back to the browse prompt.


There is also the re.eval( "print( ls.str() )" ) part which was my first 
attempt, but apparently this gets evaluated in the global environment, 
which is no good. I can get around that by returning some sort of 
"record the sys.frames and sys.calls somewhere and do something with 
them" function, but I was wondering if you meant something else.


Romain

Here is the transcript of a simple session of ./run rtest (with the 
small adjustement above)


> f <- function( x= 5) browser()
rBusy(1)
rBusy(0)
> f()
rBusy(1)
Called from: f()
rBusy(0)
Browse[1]>
 re.eval( " print( ls.str() )" );
a :  chr "hello"
b : 'data.frame':3 obs. of  2 variables:
$ a: num  1.2 2.3 4.5
$ b: num  1.4 2.6 4.2
bool :  logi [1:3] TRUE FALSE FALSE
f : function (x = 5) 
iris : 'data.frame':150 obs. of  5 variables:

$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 
1 1 1 1 1 ...


 return "ls.str()"
rBusy(1)
x :  num 5
rBusy(0)
Browse[1]>
rBusy(0)
>



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

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


[Rd] "interpolator constructor"; was "Scope problem?"

2009-05-25 Thread Martin Maechler
Hi Barry,

this is just a side-remark, probably not at all something you
were interested in, but to be put along this thread in the list
archives, in case some readers are side-tracked there ...

> "BaRow" == Barry Rowlingson 
> on Fri, 22 May 2009 17:28:42 +0100 writes:

BaRow> I've just spent today trying to fix a Heisenbug...
BaRow> this function returns a linear interpolator function:

BaRow> interpOne <- function(xl,yl){
BaRow> f = function(data){
BaRow> t = (data-min(xl))/(max(xl)-min(xl))
BaRow> return(min(yl)+t*(max(yl)-min(yl)))
BaRow> }
BaRow> return(f)
BaRow> }

>> k=interpOne(c(0,1),c(4,5))
>> k(0.5)
BaRow> [1] 4.5

Note that "base R" has already two such functions,
namely
  splinefun() 
and   approxfun(),
both returning a *function* as in your examples.

Best,
Martin Maechler, ETH Zurich

BaRow> and this function uses the above to return a function that returns a
BaRow> piece-wise linear interpolator function:

BaRow> mr <- function(){
BaRow> parts = list()
BaRow> ranges =  rbind(c(0,1),c(1,2),c(2,3))
BaRow> domains = rbind(c(3,4),c(5,6),c(2,8))
BaRow> for(i in 1:length(ranges[,1])){
BaRow> parts[[i]] = interpOne(ranges[i,],domains[i,])
BaRow> }

BaRow> f = function(d){
BaRow> pos = sum(d>ranges[,1])
BaRow> cat("using pos = ",pos,"\n")
BaRow> return(parts[[pos]](d))
BaRow> }
BaRow> return(f)
BaRow> }

BaRow> m = mr()

BaRow> The 'ranges' and 'domains' vectors describe the pieces. But this 
doesn't work:
>> m(0.5)
BaRow> using pos =  1
BaRow> [1] -7

BaRow> - but it should be 3.5 (since 0.5 is in the first piece, and that
BaRow> then interpolates between 3 and 4). What about the other pieces:

>> m(1.5)
BaRow> using pos =  2
BaRow> [1] -1
>> m(2.5)
BaRow> using pos =  3
BaRow> [1] 5

BaRow> - which looks like it's using the last set of range/domain pairs each
BaRow> time. Curious, I thought.

BaRow> So I thought I'd evaluate the functions as they are created in the
BaRow> list to see what's going on. Change the loop to print out:

BaRow> for(i in 1:length(ranges[,1])){
BaRow> parts[[i]] = interpOne(ranges[i,],domains[i,])
BaRow> cat("part ",i," at zero = ",parts[[i]](0),"\n")
BaRow> }

BaRow> and try:

>> m=mr()
BaRow> part  1  at zero =  3
BaRow> part  2  at zero =  4
BaRow> part  3  at zero =  -10

BaRow> looks good, those are the intercepts of my pieces... but now:

>> m(0.5)
BaRow> using pos =  1
BaRow> [1] 3.5
>> m(1.5)
BaRow> using pos =  2
BaRow> [1] 5.5
>> m(2.5)
BaRow> using pos =  3
BaRow> [1] 5

BaRow> Woah! It's now working!  Trying to observe the thing changes it? A 
Heisenbug!

BaRow> I can only think it's my misunderstanding of some aspect of R's
BaRow> scoping and evaluation rules. Does evaluating the functions within
BaRow> that loop cause a copy of some environment to be made, or a 'lazy
BaRow> evaluation' to be evaluated? Or a 'promise' to be fulfilled? I don't
BaRow> really understand those terms, I'd just hoped functions ran in the
BaRow> environment they were created in. Seems sometimes they do, sometimes
BaRow> they dont... What's going on?

BaRow> R 2.9.0 on Ubuntu.

BaRow> Barry

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

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


Re: [Rd] [R] step by step debugger in R?

2009-05-25 Thread Simon Urbanek


On May 25, 2009, at 4:54 PM, Romain Francois wrote:


Simon Urbanek wrote:


[snip]


I need to read more about embedding R (as in section 8 of WRE). I  
know you can supply your own implementation of the REPL, but I am  
not sure this includes the one that goes on once trapped into the  
browser.


Yes - it would be quite useless otherwise ;)  there are many  
examples of GUIs that use it (including the built-in ones [Windows,  
MAc, ..] or external ones e.g JGR).


Cheers,
S


Hi Simon,

Do you mean the rReadConsole callback ? I managed to make some minor  
modifications to the rtest.java example that comes with JRI to  
somewhat emulate automatically call some code (ls.str()) in this  
example at the browser prompt, before giving the prompt to the user.


  static boolean browse_first = true ;   public String  
rReadConsole(Rengine re, String prompt, int addToHistory) {

  System.out.print(prompt);
  if( prompt.startsWith( "Browse[") ){
  if( browse_first ){
  System.out.println( "\n re.eval( \"  
print( ls.str() )\" ); " ) ;

  re.eval( "print( ls.str() )" ) ;
  browse_first = false ;
  System.out.println( "\n return  
\"ls.str()\"" ) ;

  return "ls.str()\n" ;
  } else{
  browse_first = true ;
  }
}
...
}

It seems to work and could get me somewhere, although it has a "it  
works, but it does not feel right" taste. Basically the code  
pretends the user typed "ls.str\n" at the browse prompt, so that the  
R evaluator evaluates it, and then comes back to the browse prompt.


There is also the re.eval( "print( ls.str() )" ) part which was my  
first attempt, but apparently this gets evaluated in the global  
environment, which is no good. I can get around that by returning  
some sort of "record the sys.frames and sys.calls somewhere and do  
something with them" function, but I was wondering if you meant  
something else.




Well, it's entirely up to you - the REPL is working. I wasn't  
suggesting you have to use JRI for the debugger, I was just pointing  
out that browsing is treated as a regular prompt on the REPL, so any  
embedding has access to it.
The JRI eval() command has nothing to do with this directly - you can  
evaluate in any environment, just not specifying anything will throw  
you in the global environment - it's really up to you (it just  
abstracts out the direct access to parse and eval part of R - you can  
(ab)use it any way you see fit).


Cheers,
Simon



Romain

Here is the transcript of a simple session of ./run rtest (with the  
small adjustement above)


> f <- function( x= 5) browser()
rBusy(1)
rBusy(0)
> f()
rBusy(1)
Called from: f()
rBusy(0)
Browse[1]>
 re.eval( " print( ls.str() )" );
a :  chr "hello"
b : 'data.frame':3 obs. of  2 variables:
$ a: num  1.2 2.3 4.5
$ b: num  1.4 2.6 4.2
bool :  logi [1:3] TRUE FALSE FALSE
f : function (x = 5) iris : 'data.frame':150 obs. of  5 variables:
$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1  
1 1 1 1 1 1 ...


 return "ls.str()"
rBusy(1)
x :  num 5
rBusy(0)
Browse[1]>
rBusy(0)
>



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





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


Re: [Rd] [R] step by step debugger in R?

2009-05-25 Thread Romain Francois

Simon Urbanek wrote:



On May 25, 2009, at 4:54 PM, Romain Francois wrote:


Simon Urbanek wrote:


[snip]


I need to read more about embedding R (as in section 8 of WRE). I 
know you can supply your own implementation of the REPL, but I am 
not sure this includes the one that goes on once trapped into the 
browser.


Yes - it would be quite useless otherwise ;)  there are many 
examples of GUIs that use it (including the built-in ones [Windows, 
MAc, ..] or external ones e.g JGR).


Cheers,
S


Hi Simon,

Do you mean the rReadConsole callback ? I managed to make some minor 
modifications to the rtest.java example that comes with JRI to 
somewhat emulate automatically call some code (ls.str()) in this 
example at the browser prompt, before giving the prompt to the user.


  static boolean browse_first = true ;   public String 
rReadConsole(Rengine re, String prompt, int addToHistory) {

  System.out.print(prompt);
  if( prompt.startsWith( "Browse[") ){
  if( browse_first ){
  System.out.println( "\n re.eval( \" 
print( ls.str() )\" ); " ) ;

  re.eval( "print( ls.str() )" ) ;
  browse_first = false ;
  System.out.println( "\n return 
\"ls.str()\"" ) ;

  return "ls.str()\n" ;
  } else{
  browse_first = true ;
  }
}
...
}

It seems to work and could get me somewhere, although it has a "it 
works, but it does not feel right" taste. Basically the code pretends 
the user typed "ls.str\n" at the browse prompt, so that the R 
evaluator evaluates it, and then comes back to the browse prompt.


There is also the re.eval( "print( ls.str() )" ) part which was my 
first attempt, but apparently this gets evaluated in the global 
environment, which is no good. I can get around that by returning 
some sort of "record the sys.frames and sys.calls somewhere and do 
something with them" function, but I was wondering if you meant 
something else.


Thank you for these comments. It confirms what I was thinking.



Well, it's entirely up to you - the REPL is working. I wasn't 
suggesting you have to use JRI for the debugger, I was just pointing 
out that browsing is treated as a regular prompt on the REPL, so any 
embedding has access to it.


I understand that. It was the quickest way for me to get an example 
going. java/JRI is one option, but there are others (Qt, ...)


The JRI eval() command has nothing to do with this directly - you can 
evaluate in any environment, just not specifying anything will throw 
you in the global environment - it's really up to you (it just 
abstracts out the direct access to parse and eval part of R - you can 
(ab)use it any way you see fit).


... and I surely will.

Romain



Cheers,
Simon



Romain

Here is the transcript of a simple session of ./run rtest (with the 
small adjustement above)


> f <- function( x= 5) browser()
rBusy(1)
rBusy(0)
> f()
rBusy(1)
Called from: f()
rBusy(0)
Browse[1]>
 re.eval( " print( ls.str() )" );
a :  chr "hello"
b : 'data.frame':3 obs. of  2 variables:
$ a: num  1.2 2.3 4.5
$ b: num  1.4 2.6 4.2
bool :  logi [1:3] TRUE FALSE FALSE
f : function (x = 5) iris : 'data.frame':150 obs. of  5 variables:
$ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
$ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
$ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
$ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
$ Species : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 
1 1 1 1 1 1 ...


 return "ls.str()"
rBusy(1)
x :  num 5
rBusy(0)
Browse[1]>
rBusy(0)
>



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










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

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


[Rd] Fwd: [R] size of point symbols

2009-05-25 Thread baptiste auguie

Dear all,


Having received no answer in r-help I'm trying r-devel (hoping this is  
not a stupid question).


I don't understand the rationale behind the absolute sizes of the point
symbols, and I couldn't find it documented (I got lost in the C code  
graphics.c and gave up). The example below uses

Grid to check the size of the symbols against a square of 10mm x 10mm.


checkOneSymbol <- function(pch=0){
   gTree(children=gList(
   rectGrob(0.5, 0.5, width=unit(10, "mm"), height=unit(10,
"mm"),
   gp=gpar(lty=2, fill=NA, col=alpha("black", 0.5))),
   pointsGrob(0.5, 0.5, size=unit(10, "mm"),pch=pch,
   gp=gpar(col=alpha("red", 0.5)))
   ))

}
all.symbols <- lapply(0:23, checkOneSymbol)

pdf("symbols.pdf", height=1.2/2.54, width=24.2/2.54)

vp <- viewport(width=0.5, height=0.5, name="main")
pushViewport(vp)

pushViewport(viewport(layout=grid.layout(1, 24,
   widths=unit(10, "mm"),
   heights=unit(10, "mm"),
   just="center")))

for(ii in 0:23){
pushViewport(viewport(layout.pos.col=ii+1, layout.pos.row=1))
grid.draw(all.symbols[[ii+1]])
upViewport(1)
}
dev.off()



What dictates the size of each symbol? (in other words, why is pch=21
a circle of radius given in inches, while pch=2 is a triangle of base
length specified  in mm and offset vertically?, etc.)

I'm trying to develop a new symbol for the ggplot2 package where the  
size is to be accurately mapped onto the data either in linear size or  
area. I was expecting a similar idea behind the choice of base  
symbols. Is this documented?


Best regards,

baptiste

_

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

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


[Rd] Vista authorization issues (PR#13718)

2009-05-25 Thread dpkesling
Full_Name: David Kesling
Version: 2.9.0
OS: Windows Vista & XP sp2
Submission from: (NULL) (65.166.169.237)


I upgraded to 2.9.0 on my desktop (XP) and my laptop (Vista Home Basic). The XP
machine (with both Tinn-R and JGR) is running just fine... very happy. The
laptop is NOT doing too well. I've had to activate the XP-compatability mode AND
run Rgui as administrator... and there still seem to be issues. Tinn-R, even
after reinstallation, will not properly communicate with Rgui. JGR can't even
FIND R 2.9.

Again, 2.9 seems to run fine under XP and, with much struggling, under Vista.
But registry issues on the Vista platform have broken Tinn-R and JGR IDEs.

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


Re: [Rd] Fwd: [R] size of point symbols

2009-05-25 Thread Prof Brian Ripley
I don't know where you get your claims from.  R graphics is handled 
internally in inches, with a device-specific mapping to pixels/points 
etc (which is documented for each device on its help page).  This has 
to be done carefully, as pixels may not be square.


What the meaning of pch=1:23 is in terms of coordinates is not 
documented except via the sources.  The source is function GESymbol in 
file src/main/engine.c, so for example pch = 2 is


case 2: /* S triangle - point up */
xc = RADIUS * GSTR_0;
r = toDeviceHeight(TRC0 * xc, GE_INCHES, dd);
yc = toDeviceHeight(TRC2 * xc, GE_INCHES, dd);
xc = toDeviceWidth(TRC1 * xc, GE_INCHES, dd);
xx[0] = x; yy[0] = y+r;
xx[1] = x+xc; yy[1] = y-yc;
xx[2] = x-xc; yy[2] = y-yc;
gc->fill = R_TRANWHITE;
GEPolygon(3, xx, yy, gc, dd);
break;

which as you see is in inches, not mm as you asserted.  The first line 
sets xc to 0.375 inches for cex=1, for example.


You need to take the stroke width (as set by lty) into account when 
assessing the visual size of symbols


On Mon, 25 May 2009, baptiste auguie wrote:


Dear all,


Having received no answer in r-help I'm trying r-devel (hoping this is not a 
stupid question).


I don't understand the rationale behind the absolute sizes of the point
symbols, and I couldn't find it documented (I got lost in the C code 
graphics.c and gave up).


You are expected to study the sources for yourself.  That's part of 
the price of R.


There is a manual, 'R Internals', that would have explained to you 
that graphics.c is part of base graphics and hence not of grid 
graphics.



The example below uses
Grid to check the size of the symbols against a square of 10mm x 10mm.


checkOneSymbol <- function(pch=0){
  gTree(children=gList(
  rectGrob(0.5, 0.5, width=unit(10, "mm"), height=unit(10,
"mm"),
  gp=gpar(lty=2, fill=NA, col=alpha("black", 0.5))),
  pointsGrob(0.5, 0.5, size=unit(10, "mm"),pch=pch,
  gp=gpar(col=alpha("red", 0.5)))
  ))

}
all.symbols <- lapply(0:23, checkOneSymbol)

pdf("symbols.pdf", height=1.2/2.54, width=24.2/2.54)

vp <- viewport(width=0.5, height=0.5, name="main")
pushViewport(vp)

pushViewport(viewport(layout=grid.layout(1, 24,
  widths=unit(10, "mm"),
  heights=unit(10, "mm"),
  just="center")))

for(ii in 0:23){
pushViewport(viewport(layout.pos.col=ii+1, layout.pos.row=1))
grid.draw(all.symbols[[ii+1]])
upViewport(1)
}
dev.off()



What dictates the size of each symbol? (in other words, why is pch=21
a circle of radius given in inches, while pch=2 is a triangle of base
length specified  in mm and offset vertically?, etc.)

I'm trying to develop a new symbol for the ggplot2 package where the size is 
to be accurately mapped onto the data either in linear size or area. I was 
expecting a similar idea behind the choice of base symbols. Is this 
documented?


Best regards,

baptiste

_

Baptiste AuguiƩ

School of Physics
University of Exeter
Stocker Road,
Exeter, Devon,
EX4 4QL, UK

Phone: +44 1392 264187

http://newton.ex.ac.uk/research/emag

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


--
Brian D. Ripley,  rip...@stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel:  +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UKFax:  +44 1865 272595__
R-devel@r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-devel