Sliders in R TclTk

Sliders in R TclTk

James Wettenhall & Philippe Grosjean bio photo By James Wettenhall & Philippe Grosjean

The following example illustrates how to use a slider in a Tk window.

library(tcltk2)

win1 <- tktoplevel()
tktitle(win1) <- "Slider"

# Use a linked Tcl variable to catch the value
sliderValue <- tclVar("50")

# Add a label with the current value of the slider
win1$env$label <- tk2label(win1,
  text = "Slider value: 50%")
tkgrid(win1$env$label, padx = 10, pady = c(15, 5))

# A function that changes the label
onChange <- function(...) {
  value <- as.integer(tclvalue(sliderValue))
  label <- sprintf("Slider value: %s%%", value)
  tkconfigure(win1$env$label, text = label)
}

# Add the slider
win1$env$slider <- tk2scale(win1, from = 0, to = 100,
  variable = sliderValue, orient = "horizontal", length = 200,
  command = onChange)
tkgrid(win1$env$slider, padx = 10, pady = c(5, 15))

The code above produces the following window:

slider

Dragging the slider up to 95% gives the following:

slider dragged