Stéphane Laurent
Stéphane Laurent

Reputation: 84559

Displaying a graphic with gWidgets2

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.

Upvotes: 2

Views: 1123

Answers (1)

agstudy
agstudy

Reputation: 121578

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)
})

Upvotes: 2

Related Questions