user interface - Output device for R that allows to manipulate variables with sliders (or other GUI elements)? -
i want plot data , manipulate variables used in plot (here: rangemin
, rangemax
). consequences of variable change should directly visible in plot.
i'd have gui elements (e.g. slider) can use changing variable values of rangemin
, rangemax
, call rangeplot()
function.
is there output device in r, provides gui-elements?
# generate example data n <- 100 x <- 1:n y <- x + (runif(n) * 25) # rangemin: value i'd manipulate using sliders # rangemax: value i'd manipulate using sliders rangeplot <- function(x, y, rangemin, rangemax) { plot(x, y) # linear regression using datapoints a range plotrange <- c(rangemin:rangemax) # linear model (y mapped on x) linreg = lm(formula = y[plotrange] ~ x[plotrange]) abline(linreg, col=2) # highlight points used liniar regression points(x[plotrange], y[plotrange], col=2, pch=3) # show slope , intercept in plot text( x=min(x), y=max(y), paste0( "intercept: ", linreg$coefficients[1], "\nslope: ", linreg$coefficients[2] ), adj=c(0, 1) ) } # manual call rangeplot(x=x, y=y, rangemin=1, rangemax=n) rangeplot(x=x, y=y, rangemin=0.2*n, rangemax=0.8*n) rangeplot(x=x, y=y, rangemin=50, rangemax=60) #
to elaborate on comments. manipulate package rstudio makes such things easy. package works within rstudio. if want use same syntax without rstudio, there example in gwidgets2 (no need gwidgetsmanipulate):
require(tcltk); require(tkrplot) require(gwidgets2) # require(devtools); install_github(c("gwidgets2", "gwidgets2tcltk"), "jverzani") options(guitoolkit="tcltk") source(system.file("examples", "manipulate.r", package="gwidgets2")) w <- gwindow("manipulate example", visible=false) manipulate({ y <- get(distribution)(size) plot(density(y, bw=bandwidth/100, kernel=kernel)) points(y, rep(0, size)) }, ## distribution=picker("normal"="rnorm", "exponential"="rexp"), kernel=picker("gaussian", "epanechnikov", "rectangular", "triangular", "cosine"), size=picker(5, 50, 100, 200, 300), bandwidth=slider(0.05 * 100, 2.00 * 100, step=0.05 * 100, initial=1* 100), # integers needed button=button("refresh"), container=w ) visible(w) <- true
the guis better either rgtk2 or qt backends, bit harder install.
Comments
Post a Comment