Skip to content
Prev 821 / 12125 Next

[R-pkg-devel] develop package with lots of tcltk in it

Thanks, Dirk.

Sorry for my lack of clarity. I do want to create a package and have
created one and it works, but the structure is awkward. I'm hoping someone
can help me straighten out the organization.

In package/R folder, I have several files that define standalone functions
that crunch data.
In package/data folder, I have several source files that construct
appropriate tktoplevel() windows with the desired widgets
To start the application, user opens a main tktoplevel() window by entering
a simple command from R.
That main window has several buttons that source() files in package/data
folder to build a new tktoplevel(), depending on user choice

For users who don't know anything about R, this solution may work fine. But
for users that do know R, it is unsatisfactory...program changes the
working directory, fills it with hundreds of alien-looking functions and
data, and crashes if working directory is changed or variables are modified
by hand.

I came up with that goofy solution to get around two issues:
1. I wasn't able to get away with defining tktoplevel windows with
associated widgets and storing in .Rda's packaged in /data or as
devtools::use_data(...internal = T), so I figured they needed to be defined
and created as needed;
2. it is easy to define the tktoplevels via code in source() files, but I
was having trouble with scoping and bookkeeping issues when trying to
define them via functions instead. It would be convenient to have all the
interesting data stored as globals and use the tkwidgets to edit and
manipulate them without having to think specifically about how to pass the
interesting data back and forth.

E.g. is there an easy way to organize the following into a package that
does not use 'source', treats tk.x and tk.y as globals, and does not take
over the user's working directory with a bunch of variables like tt1,
x.edit, tk.x, tk.y, etc.?
tt1 <- tktoplevel()
tk.x <- tclVar()
x.edit <- tkentry(tt1, textvariable = tk.x, width = 5)
x.lbl <- tklabel(tt1, text = "Enter x value: ")
xcalc <- tkbutton(tt1, text = "Calculate", command = function()
tkmessageBox(message = tclvalue(tk.x)))
doMoreStuff <- tkbutton(tt1, text = "Do more...", command = function()
 source('doStuff_form.R'))
tkgrid(x.lbl, x.edit)
tkgrid(xcalc,doMoreStuff)

# in doStuff_form.R:
tt2 <- tktoplevel()
tk.y<-tclVar()
y.edit<- tkentry(tt2, textvariable = tk.y, width = 5)
anscalc <- tkbutton(tt2, text = "calculate answer", command = function()
  tkmessageBox(message=as.numeric(tclvalue(tk.y)) *
as.numeric(tclvalue(tk.x)))
)
tkgrid(y.edit, anscalc)

-Dan
On Thu, May 12, 2016 at 8:30 AM, Dirk Eddelbuettel <edd at debian.org> wrote: