2

Here is my GUI "header":

library(gWidgets2RGtk2)
library(cairoDevice)
library(ggplot2)
WINGRAPH0 <- gwindow("")
WINGRAPH <- gvbox(container=WINGRAPH0)

The following code does not work:

gnb <- gnotebook(container=WINGRAPH)
ggraph <- ggraphics(container=gnb)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()

It gives:

Error in UseMethod("depth") : 
  no applicable method for 'depth' applied to an object of class "NULL"

However if I start by displaying an image file in the graphics notebook, this works fine:

gnb <- gnotebook(container=WINGRAPH)
gimage("plot1.png", container=gnb)
ggraph <- ggraphics(container=gnb)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()

In the first code, if I use a classical plot instead of a ggplot (such as plot(0,0)), I get:

Error in plot.new() : figure margins too large

I have tried the answers given to this question but that didn't work.

Community
  • 1
  • 1
Stéphane Laurent
  • 75,186
  • 15
  • 119
  • 225

1 Answers1

2

Set visible to FALSE before plotting:

library(gWidgets2RGtk2)
library(cairoDevice)

w <- gwindow("notebook example")
nb <- gnotebook(cont=w)
gg <- ggraphics(cont=nb, label='1',visible=FALSE)
library(ggplot2)
ggplot(cars, aes(x=speed, y=dist)) + geom_point()
visible(gg) <- TRUE

enter image description here

EIDT

w <- gwindow("notebook example")
nb <- gnotebook(cont=w)
devs <- lapply(1:2, function(i) 
    ggraphics(cont=nb,visible=FALSE, label=as.character(i)))

addHandlerChanged(nb, handler=function(h,...) {
    gg <- h$obj[h$page.no]
    visible(gg) <- TRUE
    if(h$page.no =="1")
        print(ggplot(cars, aes(x=speed, y=dist)) + geom_point())
    else    plot(0)
})
agstudy
  • 119,832
  • 17
  • 199
  • 261
  • Ok ! I had tried with `visible=FALSE` in `gwindow()`, instead of `ggraphics()`. But your solution works for a ggplot graphic, there's still the `figure margins too large` error message for a classical plot graphic. – Stéphane Laurent Dec 09 '13 at 19:34
  • No reason to have an error ! it should also work for a base plot. – agstudy Dec 09 '13 at 19:35
  • The code in your edit works because the ggplot in the first tab works. But `plot(0,0)` in your first code instead of `ggplot(..)` gives `Error in plot.new() : figure margins too large`. Have you tried it ? – Stéphane Laurent Dec 09 '13 at 19:52
  • Good news: It works when I put `visible=FALSE` in `gnotebook()` instead of `ggraphics()` ! – Stéphane Laurent Dec 09 '13 at 19:58
  • I am totally lost. The behavior is unstable ! Now my first code does not generate any error message. And it works if I remove `WINGRAPH` and rename `WINGRAPH0` to `WINGRAPH`, without using `visible=FALSE` anywhere. – Stéphane Laurent Dec 09 '13 at 20:40