The following example shows how to plot a graph in a Tk window, using Luke Tierney's
tkrplot package.
require(tcltk)
library(tkrplot)
Myhscale <- 1.5 # Horizontal scaling
Myvscale <- 1.5 # Vertical scaling
plotFunction <- function()
{
params <- par(bg="white")
x <- -100:100
y <- x^2
plot(x,y,main="A parabola")
par(params)
}
tt <- tktoplevel()
tkwm.title(tt,"A parabola")
img <- tkrplot(tt,fun=plotFunction,hscale=Myhscale,vscale=Myvscale)
tkgrid(img)
The code listed above gives the following graph window:

It is worth noting that tkrplot places the graph on the clipboard (in Windows or X11) before it plots it on the window. This means that once a graph has been plotted, it can easily be pasted into another program. However, if a second graph is plotted, the first graph will be lost from the clipboard, so the software developer may wish to include a "Copy to Clipboard" button or menu item on the tkrplot graph window, so that the user can come back to it later and copy it to the clipboard. This can be done using the tkrreplot function as follows:
require(tcltk)
library(tkrplot)
Myhscale <- 1.5 # Horizontal scaling
Myvscale <- 1.5 # Vertical scaling
plotFunction <- function()
{
params <- par(bg="white")
x <- -100:100
y <- x^2
plot(x,y,main="A parabola")
par(params)
}
tt <- tktoplevel()
tkwm.title(tt,"A parabola")
img <- tkrplot(tt,fun=plotFunction,hscale=Myhscale,vscale=Myvscale)
CopyToClip <- function()
{
tkrreplot(img)
}
copy.but <- tkbutton(tt,text="Copy to Clipboard",command=CopyToClip)
tkgrid(img)
tkgrid(copy.but)
The code listed above gives the following graph window:
