[Rd] Does anybody have some starter code in Java to instance a standalone JGRConsole ?

2009-03-13 Thread Daniel Kornhauser
Hi:

Does anybody have some starter code in Java to instance a stand alone
JGRConsole ?

From the docs it looks that it shouldn't be that hard to write some starter
code:

http://www.rosuda.org/r/nightly/javadoc/org/rosuda/JGR/JGRConsole.html

But in practice starter code has always little tricks ...

Searched in rseek, Google and mailing lists but came out empty handed :-(

   Thanks.

 Daniel.

[[alternative HTML version deleted]]

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


[Rd] How to generate a xyplot with multiple panels using an empty data frame ?

2009-01-29 Thread Daniel Kornhauser
Hi:

I am coding some interactive interface using an xyplot from lattice.
When a user clicks on a panel of the xyplot, a simulation is executed and
the resulting data is plotted on the corresponding panel.
The problem is that I start with an empty data frame and only fill it as the
user request data from simulations.
And, I have found it impossible to create a conditional plot that contains
panels using an empty data frame.

For example I want:

library(lattice)
e = data.frame(a, b, c, d)
xyplot(X.a. ~ X.b. | X.c. + X.d., data = e, xlim = c(c(50,60),c(60,70)),
ylim = c(c(10,20),c(20,30)), drop.unused.levels=FALSE,layout = c(2,2))

to create a empty xyplot that would look like:

   50 60   70

   10   | | |
| | |
| | |
   20   
| | |
| | |
   30   | | |
-

but it doesn't create 4 panels it only creates a single panel:

5060  70
---
   10   |  |
   20   |  |
   30   |  |


I am a novice in R so I hope this is not a question that is too easy for the
r-devel list.

  Thanks.

Daniel.

PS: I got around this problem by creating dummy data frames containing
data with the sole purpose of indicating xyplot the data rages.
   It works well, but I consider it a hack.

[[alternative HTML version deleted]]

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


[Rd] Buggy trellis.focus() with xyplot ?

2008-11-19 Thread Daniel Kornhauser
Hi:

(Tried to find a bug report about this issue, but was unable to find it, let
me know if this is a known issue)
I have been working on an interface to highlight xyplot panels on mouse
overs in JavaGD but I have stumbled with what seems to be a bug in
trellis.focus.
I am using R 2.8 with lattice 0.17-15


*** To replicate the bug:
1.- display an xyplot. For example, from the xyplot help page:
library(lattice)
require(stats)
EE - equal.count(ethanol$E, number=9, overlap=1/4)
## Constructing panel functions on the fly; prepanel
xyplot(NOx ~ C | EE, data = ethanol,
   prepanel = function(x, y) prepanel.loess(x, y, span = 1),
   xlab = Compression Ratio, ylab = NOx (micrograms/J),
   panel = function(x, y) {
   panel.grid(h=-1, v= 2)
   panel.xyplot(x, y)
   panel.loess(x,y, span=1)
   },
   aspect = xy)
2.- resize to window be rectangular and have large margins in the left hand
and right hand side.
3.- run trellis.focus() and try to select the first or last panel, you
should observe that it does not select the right one.

sidenotes:
There are other problems with the focus in JavaGD but I just wanted to
include a simple self contained example in this mail.
If you make the window smaller, trellis.focus() works fine, you have to make
it bigger than the initial size.


*** To Fix the bug:
I tried to fix this bug in interraction.R but I was unsuccesful.

The problem should stem from the a bad calculation of the pads in the
follwoing lines :
leftPad -
convertX(sum(glayout$page.layout$widths[1:(colRange[1]-1)]), npc,
valueOnly = TRUE)
rightPad -
convertX(sum(glayout$page.layout$widths[(colRange[2]+1):layCols]), npc,
valueOnly = TRUE)
topPad -
convertY(sum(glayout$page.layout$heights[1:(rowRange[1]-1)]), npc,
valueOnly = TRUE)
botPad -
convertY(sum(glayout$page.layout$heights[(rowRange[2]+1):layRows]), npc,
valueOnly = TRUE)

I was succesful in tweaking the follwing lines adding arbitrary constants to
make it work for a particular instance of a xyplot with a particular size of
a window
   clickXScaled - (as.numeric(clickLoc$x) - leftPad + Danielconstant1)
/ (1 - leftPad - rightPad  + Danielconstant1)

clickYScaled - (as.numeric(clickLoc$y) - botPad + Danielconstant2)
/ (1 - botPad - topPad + Danielconstant3)
This is of course a useless fix, since you want the fix to work for any plot
with any window size, but I might be valuable information.


*** Questions:
- Is this a real bug ?
- Any suggestions for fixing it ?
- If it can't be fixed, is there a way around this bug ?
(For example, setting the margins to be zero and set a fixed size for the
xplot)

  Thanks.

   Daniel.

[[alternative HTML version deleted]]

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


[Rd] Buggy trellis.focus() with xyplot in JavaGD ?

2008-11-19 Thread Daniel Kornhauser
Hi:

(Tried to find a bug report about this issue, but was unable to find it, let
me know if this is a known issue)
I have been working on an interface to highlight xyplot panels on mouse
overs in JavaGD but I have stumbled with what seems to be a bug in
trellis.focus.
I am using JGR R 2.8 with lattice 0.17-15
Note: It's important to you use JGR to use the Java Graphics Device.

*** To Replicate the Bug ***:
1.- display an xyplot. For example, from the xyplot help page:
library(lattice)
require(stats)
EE - equal.count(ethanol$E, number=9, overlap=1/4)
## Constructing panel functions on the fly; prepanel
xyplot(NOx ~ C | EE, data = ethanol,
   prepanel = function(x, y) prepanel.loess(x, y, span = 1),
   xlab = Compression Ratio, ylab = NOx (micrograms/J),
   panel = function(x, y) {
   panel.grid(h=-1, v= 2)
   panel.xyplot(x, y)
   panel.loess(x,y, span=1)
   },
   aspect = xy)
2.- resize to window be rectangular and to have large margins on all sides.
3.- run trellis.focus() and try to select the first or last panel, you
should observe that it does not select the right one.

sidenote:
If you make the window smaller, trellis.focus() works fine, you have to make
it bigger than the initial size.



*** To Fix the Bug ***:
I tried to fix this bug in interraction.R but I was unsuccessful.

The problem should stem from the a bad calculation of the margins.
Notice that if you set the margins to 0 size the bug above does not appear.
You can test this with:
library(lattice)
require(stats)
EE - equal.count(ethanol$E, number=9, overlap=1/4)
## Constructing panel functions on the fly; prepanel
xyplot(NOx ~ C | EE, data = ethanol,
   prepanel = function(x, y) prepanel.loess(x, y, span = 1),
   xlab = Compression Ratio, ylab = NOx (micrograms/J),
   panel = function(x, y) {
   panel.grid(h=-1, v= 2)
   panel.xyplot(x, y)
   panel.loess(x,y, span=1)
   },
   par.settings=list(
layout.widths=list(left.padding=0, right.padding=0),
layout.heights=list(top.padding=0, bottom.padding=0))
)

I traced the a bad calculation of the pads in the following lines :
leftPad - convertX(sum(glayout$page.layout$widths[1:(colRange[1]-1)]),
npc, valueOnly = TRUE)
rightPad -
convertX(sum(glayout$page.layout$widths[(colRange[2]+1):layCols]), npc,
valueOnly = TRUE)
topPad -
convertY(sum(glayout$page.layout$heights[1:(rowRange[1]-1)]), npc,
valueOnly = TRUE)
botPad -
convertY(sum(glayout$page.layout$heights[(rowRange[2]+1):layRows]), npc,
valueOnly = TRUE)

I was succesful in tweaking the follwing lines adding arbitrary constants to
make it work for a particular instance of a xyplot with a particular size of
a window
   clickXScaled - (as.numeric(clickLoc$x) - leftPad + Danielconstant1)
/ (1 - leftPad - rightPad  + Danielconstant1)

clickYScaled - (as.numeric(clickLoc$y) - botPad + Danielconstant2)
/ (1 - botPad - topPad + Danielconstant3)
This is of course a useless fix, since you want the fix to work for any plot
with any window size, but I might be valuable information.



*** Questions ***:
- Is this a real bug ?
- Any suggestions for fixing it ?
- The only way around I have found around this bug is to make the margins 0
is there any other way.

IMPORTANT NOTE:
I have found other problems with the focus in JavaGD but did not mention
them since I just wanted to include a simple self contained example in this
mail.

  Thanks.

[[alternative HTML version deleted]]

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


Re: [Rd] Buggy trellis.focus() with xyplot ?

2008-11-19 Thread Daniel Kornhauser
Thanks a lot for taking this on guys !

Some more background in case you are interested:

I have almost a working version of focus for selecting a panel with mouse
over in a stanalone Java application using an REngine I only have two
issues:

- trellis.unfocus() is terribly slow in a plot of 10 x 10 panels, it takes
1.5 seconds to unfocus, do you know any way to speed it up ?

- the conversion problem that Deepayan referred too in the previous, but I
guess that will be fixed soon. :-)


I am working in a bastardized version of trellis.focus taking an x and y
from an event handler (in Java, C, etc ...) which focuses and highlights the
panel so the REngine instance can perform more operations on the selected
panel.
This would be very useful for easily creating GUI for interactive
visualizations exploration of large multidimensional data sets using lattice
and geared towards non programmers.

I don't really know what I am getting into, so any advice would be welcomed.

Daniel.


2008/11/19 Felix Andrews [EMAIL PROTECTED]

 2008/11/20 Deepayan Sarkar [EMAIL PROTECTED]:
  On Wed, Nov 19, 2008 at 8:55 AM, Daniel Kornhauser
  [EMAIL PROTECTED] wrote:
  Hi:
 
  (Tried to find a bug report about this issue, but was unable to find it,
 let
  me know if this is a known issue)
  I have been working on an interface to highlight xyplot panels on mouse
  overs in JavaGD but I have stumbled with what seems to be a bug in
  trellis.focus.
  I am using R 2.8 with lattice 0.17-15
 
 
  *** To replicate the bug:
  1.- display an xyplot. For example, from the xyplot help page:
  library(lattice)
  require(stats)
  EE - equal.count(ethanol$E, number=9, overlap=1/4)
  ## Constructing panel functions on the fly; prepanel
  xyplot(NOx ~ C | EE, data = ethanol,
prepanel = function(x, y) prepanel.loess(x, y, span = 1),
xlab = Compression Ratio, ylab = NOx (micrograms/J),
panel = function(x, y) {
panel.grid(h=-1, v= 2)
panel.xyplot(x, y)
panel.loess(x,y, span=1)
},
aspect = xy)
  2.- resize to window be rectangular and have large margins in the left
 hand
  and right hand side.
  3.- run trellis.focus() and try to select the first or last panel, you
  should observe that it does not select the right one.
 
  sidenotes:
  There are other problems with the focus in JavaGD but I just wanted to
  include a simple self contained example in this mail.
  If you make the window smaller, trellis.focus() works fine, you have to
 make
  it bigger than the initial size.
 
 
  *** To Fix the bug:
  I tried to fix this bug in interraction.R but I was unsuccesful.
 
  The problem should stem from the a bad calculation of the pads in the
  follwoing lines :
 leftPad -
  convertX(sum(glayout$page.layout$widths[1:(colRange[1]-1)]), npc,
  valueOnly = TRUE)
 rightPad -
  convertX(sum(glayout$page.layout$widths[(colRange[2]+1):layCols]),
 npc,
  valueOnly = TRUE)
 topPad -
  convertY(sum(glayout$page.layout$heights[1:(rowRange[1]-1)]), npc,
  valueOnly = TRUE)
 botPad -
  convertY(sum(glayout$page.layout$heights[(rowRange[2]+1):layRows]),
 npc,
  valueOnly = TRUE)
 
  I was succesful in tweaking the follwing lines adding arbitrary
 constants to
  make it work for a particular instance of a xyplot with a particular
 size of
  a window
clickXScaled - (as.numeric(clickLoc$x) - leftPad +
 Danielconstant1)
  / (1 - leftPad - rightPad  + Danielconstant1)
 
 clickYScaled - (as.numeric(clickLoc$y) - botPad +
 Danielconstant2)
  / (1 - botPad - topPad + Danielconstant3)
  This is of course a useless fix, since you want the fix to work for any
 plot
  with any window size, but I might be valuable information.
 
 
  *** Questions:
  - Is this a real bug ?
  - Any suggestions for fixing it ?
  - If it can't be fixed, is there a way around this bug ?
  (For example, setting the margins to be zero and set a fixed size for
 the
  xplot)
 
  It appears that the conversions used in the current implementation
  (contributed by Felix Andrews) don't work when aspect != fill
  (probably leading back to the use of 'respect = TRUE' in grid.layout).

 Yep, my fault, didn't think it through.

  The right way to do this is to first go down to the subregion
  containing just the panels, and then locate the click location within
  it. But this requires a suitable viewport to be predefined.
 
  I have changed print.trellis to create such a dummy viewport
  (accessible by trellis.focus(figure)), and modified trellis.focus()
  to use it. I will test it a bit more before uploading a new version
  (and also give Felix a chance to see if this breaks anything in

 It won't affect playwith; playwith now uses a different approach: a
 function
 inViewport(x.px, y.px, viewport)
 reports whether a click location in pixels is inside the given
 viewport. I loop through each panel viewport and check whether the
 click is inside.

  playwith etc.). To see

Re: [Rd] Getting the panel location of a xyplot matrix using a mouse click in a GDCanvas

2008-10-24 Thread Daniel Kornhauser
Thanks Deepayan !
It worked great !

You can take a look at my commented sloppy code below.
It's mostly copy and paste from clickFocus.

If you ever get a chance I suggest you divide clickFocus in in two
functions:
  - trellis.clickFocus:
Would behave like the function you have now but would call
xyClickFocus() to do most of the work.
It would mostly call grid.location()
 - trellis.clickFocusXY(x,y)  (I am not refering to the code below, this
function would be different)
   Would do most of the work, and would be great to embed an R device into
any GUI.

I don't know if there is any other way to go around who owns the event loop,
either the REngine or the GUI where the R device is embedded.

Here is my modified sloppy code (I just have been playing with R for a
week):

xyclickFocus -
function(x,
y,
clip.off = FALSE,
highlight = interactive(),
...,
guess = TRUE,
verbose = TRUE)
{
library(lattice) ### New
library(grid) ### New
trellis.unfocus()  ### Modified, from trellis.focus())
layoutMatrix - trellis.currentLayout()
if (guess  sum(layoutMatrix  0) == 1)
{
## there's only one panel, so just select it
w - which(layoutMatrix  0)
focusRow - row(layoutMatrix)[w]
focusCol - col(layoutMatrix)[w]
if (verbose) message(sprintf(Selecting panel at position (%g, %g),
focusRow, focusCol))
}
else if (all(layoutMatrix == 0))
{
warning(No panels available)
return()
}
else
{
x - grconvertX(x, device, npc) ### New
y - grconvertY(y, device, npc) ### New
x - unit(x, native) ### New
y - unit(y, native) ### New
clickLoc = list(x=x,y=y)  ### Modified, changed from -
grid.locator(npc)
glayout -lattice:::lattice.getStatus(layout.details)###
Modified, added lattice:::
rowRange - range(glayout$pos.heights$panel,
glayout$pos.heights$strip)
colRange - range(glayout$pos.widths$panel,
glayout$pos.widths$strip.left)
layCols -  glayout$page.layout$ncol
layRows - glayout$page.layout$nrow
leftPad -
convertX(sum(glayout$page.layout$widths[1:(colRange[1]-1)]), npc,
valueOnly = TRUE)
rightPad -
convertX(sum(glayout$page.layout$widths[(colRange[2]+1):layCols]), npc,
valueOnly = TRUE)
topPad -
convertY(sum(glayout$page.layout$heights[1:(rowRange[1]-1)]), npc,
valueOnly = TRUE)
botPad -
convertY(sum(glayout$page.layout$heights[(rowRange[2]+1):layRows]), npc,
valueOnly = TRUE)
message(Click on panel to focus)
if (is.null(clickLoc)) return()
clickXScaled - (as.numeric(clickLoc$x) - leftPad) / (1 - leftPad -
rightPad)
focusCol - ceiling(clickXScaled * ncol(layoutMatrix))
clickYScaled - (as.numeric(clickLoc$y) - botPad) / (1 - botPad -
topPad)
focusRow - ceiling(clickYScaled * nrow(layoutMatrix))
if (lattice:::lattice.getStatus(as.table)) focusRow -
nrow(layoutMatrix) - focusRow + 1   ### Modified, added lattice:::
}
if ((focusCol = 1)  (focusCol = ncol(layoutMatrix)) 
(focusRow = 1)  (focusRow = nrow(layoutMatrix)) 
layoutMatrix[focusRow, focusCol]  0)
{
trellis.focus(panel, column = focusCol, row = focusRow,
clip.off = clip.off, highlight = highlight,
...)
}
else
{
focusCol - focusRow - 0
}
invisible(list(col=focusCol, row=focusRow))
}


On Thu, Oct 23, 2008 at 2:55 PM, Deepayan Sarkar
[EMAIL PROTECTED]wrote:

 On Thu, Oct 23, 2008 at 12:26 PM, Daniel Kornhauser
 [EMAIL PROTECTED] wrote:
  Hi:
 
  I would like to find out the panel of a xyplot matrix where a mouse
 clicked.
 
  I know this functionality is already bundled in trellis.focus but I can't
  use it because I am coding a stand alone application in Java using with
  GDCanvas as a graphics device.
 
  I tried calling trellis.focus from Java with:
re.eval(t = trellis.focus()));
  However it did not work for an embedded GDCanvas. The above call to the
  trellis.focus() returns null no matter where I click.
 
  (Side note: the functionality must be implemented in the GDCanvas because
  trellis.focus does work correctly in JGR)
 
  I wish to handle all the user GUI events in Java to evaluate different
  commands in an Rengine.
  With a GDCanvas, it's straightforward to get the x y position of a mouse
  click with the standard e.getX() and e.getY()
  So my handler would look like:
 public void mouseClicked(MouseEvent e) {
 System.out.println(Clicked + e.getX() +   + e.getY());
 System.out.println(re.eval(t = trellis.focus()));
 System.out.println(re.eval(t));
// I would update the panel here ...
 }
 
  I am only using Java because:
  - I am proficient in Java GUI programmer
  - It is multiplatform
 
  Another way of posing the question is:
  Given the coordinates

[Rd] Getting the panel location of a xyplot matrix using a mouse click in a GDCanvas

2008-10-23 Thread Daniel Kornhauser
Hi:

I would like to find out the panel of a xyplot matrix where a mouse clicked.

I know this functionality is already bundled in trellis.focus but I can't
use it because I am coding a stand alone application in Java using with
GDCanvas as a graphics device.

I tried calling trellis.focus from Java with:
   re.eval(t = trellis.focus()));
However it did not work for an embedded GDCanvas. The above call to the
trellis.focus() returns null no matter where I click.

(Side note: the functionality must be implemented in the GDCanvas because
trellis.focus does work correctly in JGR)

I wish to handle all the user GUI events in Java to evaluate different
commands in an Rengine.
With a GDCanvas, it's straightforward to get the x y position of a mouse
click with the standard e.getX() and e.getY()
So my handler would look like:
public void mouseClicked(MouseEvent e) {
System.out.println(Clicked + e.getX() +   + e.getY());
System.out.println(re.eval(t = trellis.focus()));
System.out.println(re.eval(t));
   // I would update the panel here ...
}

I am only using Java because:
- I am proficient in Java GUI programmer
- It is multiplatform

Another way of posing the question is:
Given the coordinates from grid.location how can I find out the plane of an
xyplot matrix where the user clicked.

I have tried to figure out how to do it from code using trellis.clickFocus,
but I have have only been playing with R for a week, so I am quite lost
reading R code.
I tried to look up to in the code of JGR and came out emplty handed.

This is my first post so I hope I did not break any rules ...

Thanks !

Daniel

[[alternative HTML version deleted]]

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