I'm using the R package gWidgets. I want to add a point on a picture (.jpg).
My code is:
require(jpeg)
require(gWidgets)
options(guiToolkit="RGtk2")
w <- gwindow("test")
gimage("yourpath.jpg",dirname="", container = w,toolkit=guiToolkit("RGtk2"))
da <- w@widget@widget
callbackID <- gSignalConnect(da,"button-release-event", function
(w,e,...) {
# allocation <- w$GetAllocation()
addHandlerClicked(da, handler = function(h,...) {
})
xclick <- e$GetX()
yclick <- e$GetY()
print(xclick)
print(yclick)
points(xclick, yclick)
pressed <<- FALSE
return(TRUE)
})
warning:
plot.new has not been called yet
Could someone help me? Thanks
warning:
plot.new has not been called yet
Well, that's true isn't it ? In the code above, nowhere is a plot created (using plot() or similar).
Also in your code I cannot see where you create "yourpath.jpg" -- presumably, it is the plot you're trying to draw.
In fact you are trying, it seems, to mix an image and a plot. If you want to include an R plot, what you need is a ggraphics.
Something like that should do what you seem to try and achieve:
library(gWidgets2)
options(guiToolkit="RGtk2")
# Generate some data
xdata<-rnorm(n=5)
ydata<-rnorm(n=5)
gTest<-function(){
#Plotting function
plotf<-function(...){
plot(xdata,ydata)
}
# Function to add points
.addPoint<-function(h,...){
points(h$x,h$y,col="red")
}
win <- gwindow("Test")
theplot<-ggraphics(cont=win)
addHandlerClicked(theplot,handler=.addPoint)
Sys.sleep(0.1) # Prevents error with "figure margins too large"
plotf()
}
gTest()
Note that as written, new points are plotted (with points()
) but not actually saved. You would need to do, for instance
.addPoint<-function(h,...){
points(h$x,h$y,col="red")
xdata<<-c(xdata,h$x)
ydata<<-c(ydata,h$y)
}
Here with a global assignment, that may or may not be what you need; mostly <<-
and global variables are regarded as bad practice, but sometimes it's good enough!