[Rinside-commits] r187 - in pkg: . inst/examples inst/examples/qt

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Wed Mar 16 01:54:23 CET 2011


Author: edd
Date: 2011-03-16 01:54:23 +0100 (Wed, 16 Mar 2011)
New Revision: 187

Added:
   pkg/inst/examples/qt/
   pkg/inst/examples/qt/README
   pkg/inst/examples/qt/main.cpp
   pkg/inst/examples/qt/qtdensity.cpp
   pkg/inst/examples/qt/qtdensity.h
   pkg/inst/examples/qt/qtdensity.pro
Modified:
   pkg/ChangeLog
   pkg/DESCRIPTION
Log:
new example using Qt


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2011-03-06 14:56:53 UTC (rev 186)
+++ pkg/ChangeLog	2011-03-16 00:54:23 UTC (rev 187)
@@ -1,3 +1,9 @@
+2011-03-15  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/qt/*: Added new example combining RInside with Qt
+	illustrating the standard 'slider to select bandwidth for density
+	estimate' example found in demo(tkdensity) and other places
+
 2011-03-06  Dirk Eddelbuettel  <edd at debian.org>
 
 	* inst/examples/standard/rinside_sample11.cpp: New example in

Modified: pkg/DESCRIPTION
===================================================================
--- pkg/DESCRIPTION	2011-03-06 14:56:53 UTC (rev 186)
+++ pkg/DESCRIPTION	2011-03-16 00:54:23 UTC (rev 187)
@@ -1,6 +1,6 @@
 Package: RInside
 Title: C++ classes to embed R in C++ applications
-Version: 0.2.3
+Version: 0.2.3.1
 Date: $Date$
 Author: Dirk Eddelbuettel and Romain Francois
 Maintainer: Dirk Eddelbuettel and Romain Francois <RomainAndDirk at r-enthusiasts.com>

Added: pkg/inst/examples/qt/README
===================================================================
--- pkg/inst/examples/qt/README	                        (rev 0)
+++ pkg/inst/examples/qt/README	2011-03-16 00:54:23 UTC (rev 187)
@@ -0,0 +1,19 @@
+
+This directory provides a simple example of using RInside with the Qt
+toolkit.  Usage is standard Qt usage, do
+
+    qmake 
+
+to generate a Makefile from the qmake source file ending in .pro, followed
+by
+
+    make
+
+which should generate the qtdensity binary.  Doing
+
+    make clean
+
+tidies things up.
+
+  
+

Added: pkg/inst/examples/qt/main.cpp
===================================================================
--- pkg/inst/examples/qt/main.cpp	                        (rev 0)
+++ pkg/inst/examples/qt/main.cpp	2011-03-16 00:54:23 UTC (rev 187)
@@ -0,0 +1,20 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Qt usage example for RInside, inspired by the standard 'density
+// sliders' example for other GUI toolkits
+//
+// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+
+
+#include <QApplication>
+
+#include "qtdensity.h"
+
+int main(int argc, char *argv[])
+{
+    RInside R(argc, argv);  		// create an embedded R instance
+
+    QApplication app(argc, argv);
+    QtDensity qtdensity(R);
+    return app.exec();
+}

Added: pkg/inst/examples/qt/qtdensity.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensity.cpp	                        (rev 0)
+++ pkg/inst/examples/qt/qtdensity.cpp	2011-03-16 00:54:23 UTC (rev 187)
@@ -0,0 +1,227 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Qt usage example for RInside, inspired by the standard 'density
+// sliders' example for other GUI toolkits
+//
+// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+
+#include <QtGui>
+
+#include "qtdensity.h"
+
+QtDensity::QtDensity(RInside & R) : m_R(R)
+{
+    m_bw = 100;
+    m_kernel = 0;
+    m_kernelstrings.push_back("gaussian");
+    m_kernelstrings.push_back("epanechnikov");
+    m_kernelstrings.push_back("rectangular");
+    m_kernelstrings.push_back("triangular");
+    m_kernelstrings.push_back("cosine");
+
+    m_mixparams["n1"] = "100";
+    m_mixparams["n2"] =  "50";
+    m_mixparams["m1"] =   "0";
+    m_mixparams["m2"] =   "5";
+    m_mixparams["sd1"] =  "1";
+    m_mixparams["sd2"] =  "0.5";
+
+    m_R["bw"] = m_bw;
+    m_tempfile = Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()"));
+    draw();
+    //std::cout << "Used R-assigned tempfile " << m_tempfile << std::endl;
+
+    QWidget *window = new QWidget;
+    window->setWindowTitle("Qt and embedded R demo: density estimation");
+
+    QSpinBox *spinBox = new QSpinBox;
+    QSlider *slider = new QSlider(Qt::Horizontal);
+    spinBox->setRange(5, 200);
+    slider->setRange(5, 200);
+
+    QObject::connect(spinBox, SIGNAL(valueChanged(int)), slider, SLOT(setValue(int)));
+    QObject::connect(slider, SIGNAL(valueChanged(int)), spinBox, SLOT(setValue(int)));
+    spinBox->setValue(m_bw);
+
+    QObject::connect(spinBox, SIGNAL(valueChanged(int)), this, SLOT(getBandwidth(int)));
+
+    QGroupBox *kernelRadioBox = new QGroupBox("Density Estimation kernel");
+    QRadioButton *radio1 = new QRadioButton("&Gaussian");
+    QRadioButton *radio2 = new QRadioButton("&Epanechnikov");
+    QRadioButton *radio3 = new QRadioButton("&Rectangular");
+    QRadioButton *radio4 = new QRadioButton("&Triangular");
+    QRadioButton *radio5 = new QRadioButton("&Cosine");
+    radio1->setChecked(true);
+    QVBoxLayout *vbox = new QVBoxLayout;
+    vbox->addWidget(radio1);
+    vbox->addWidget(radio2);
+    vbox->addWidget(radio3);
+    vbox->addWidget(radio4);
+    vbox->addWidget(radio5);
+    kernelRadioBox->setMinimumSize(300,140);
+    kernelRadioBox->setMaximumSize(300,140);
+    kernelRadioBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+    kernelRadioBox->setLayout(vbox);
+
+    QButtonGroup *kernelGroup = new QButtonGroup;
+    kernelGroup->addButton(radio1, 0);
+    kernelGroup->addButton(radio2, 1);
+    kernelGroup->addButton(radio3, 2);
+    kernelGroup->addButton(radio4, 3);
+    kernelGroup->addButton(radio5, 4);
+    QObject::connect(kernelGroup, SIGNAL(buttonClicked(int)), this, SLOT(getKernel(int)));
+
+    QLabel *labelN1 = new QLabel("Nobs 1");
+    QLabel *labelM1 = new QLabel("Mean 1");
+    QLabel *labelSD1 = new QLabel("StdDev 1");
+    QLineEdit *entryN1 = new QLineEdit(QString(m_mixparams["n1"].c_str()));
+    QLineEdit *entryM1 = new QLineEdit(QString(m_mixparams["m1"].c_str()));
+    QLineEdit *entrySD1 = new QLineEdit(QString(m_mixparams["sd1"].c_str()));
+    QLabel *labelN2 = new QLabel("Nobs 2");
+    QLabel *labelM2 = new QLabel("Mean 2");
+    QLabel *labelSD2 = new QLabel("StdDev 2");
+    QLineEdit *entryN2 = new QLineEdit( QString(m_mixparams["n2"].c_str()));
+    QLineEdit *entryM2 = new QLineEdit(QString(m_mixparams["m2"].c_str()));
+    QLineEdit *entrySD2 = new QLineEdit(QString(m_mixparams["sd2"].c_str()));
+    QWidget *gridbox = new QWidget();
+    QGridLayout *grid = new QGridLayout(gridbox);
+    grid->addWidget(labelN1, 0, 0);
+    grid->addWidget(labelM1, 0, 1);
+    grid->addWidget(labelSD1, 0, 2);
+    grid->addWidget(entryN1, 1, 0);
+    grid->addWidget(entryM1, 1, 1);
+    grid->addWidget(entrySD1, 1, 2);
+    grid->addWidget(labelN2, 2, 0);
+    grid->addWidget(labelM2, 2, 1);
+    grid->addWidget(labelSD2, 2, 2);
+    grid->addWidget(entryN2, 3, 0);
+    grid->addWidget(entryM2, 3, 1);
+    grid->addWidget(entrySD2, 3, 2);
+    gridbox->setMinimumSize(300,140);
+    gridbox->setMaximumSize(300,140);
+    gridbox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+    QObject::connect(entryN1,  SIGNAL(textEdited(QString)), this, SLOT(getN1(QString)));
+    QObject::connect(entryM1,  SIGNAL(textEdited(QString)), this, SLOT(getM1(QString)));
+    QObject::connect(entrySD1, SIGNAL(textEdited(QString)), this, SLOT(getSD1(QString)));
+    QObject::connect(entryN2,  SIGNAL(textEdited(QString)), this, SLOT(getN2(QString)));
+    QObject::connect(entryM2,  SIGNAL(textEdited(QString)), this, SLOT(getM2(QString)));
+    QObject::connect(entrySD2, SIGNAL(textEdited(QString)), this, SLOT(getSD2(QString)));
+
+    imageLabel = new QLabel;
+    imageLabel->setBackgroundRole(QPalette::Base);
+    imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+    imageLabel->setScaledContents(true);
+
+    image = new QImage(QString(m_tempfile.c_str()));
+
+    plot();			// now that we have an image, we can diplay
+
+    QVBoxLayout *outer = new QVBoxLayout;
+    QHBoxLayout *upperlayout = new QHBoxLayout;
+    upperlayout->addWidget(spinBox);
+    upperlayout->addWidget(slider);
+
+    QHBoxLayout *middlelayout = new QHBoxLayout;
+    middlelayout->addWidget(kernelRadioBox);
+    middlelayout->addWidget(gridbox);
+
+    QHBoxLayout *lowerlayout = new QHBoxLayout;
+    lowerlayout->addWidget(imageLabel);
+
+    outer->addLayout(upperlayout);
+    outer->addLayout(middlelayout);
+    outer->addLayout(lowerlayout);
+    window->setLayout(outer);
+    window->show();
+    window->resize(650, 750);
+
+}
+
+QtDensity::~QtDensity() {
+    std::cerr << "Dtor" << std::endl;
+    m_R.parseEvalQ("q('no')");	// we never needed that before -- but maybe the Qt threads get in the way
+    std::cerr << "Dtor R stopped" << std::endl; // not reached !!
+}
+
+void QtDensity::plot(void) {
+    m_R["bw"] = m_bw;
+    m_R["kernel"] = m_kernelstrings[m_kernel]; // that passes the string to R
+    std::string cmd1 = "png(tfile,600,600); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+    std::string cmd2 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
+    std::string cmd = cmd1 + m_kernelstrings[m_kernel] + cmd2;
+    m_R.parseEvalQ(cmd);
+    image->load(QString(m_tempfile.c_str()));
+    imageLabel->setPixmap(QPixmap::fromImage(*image));
+}
+
+void QtDensity::draw(void) {
+    m_R["n1"] = m_mixparams["n1"];
+    m_R["m1"] = m_mixparams["m1"];
+    m_R["sd1"] = m_mixparams["sd1"];
+    m_R["n2"] = m_mixparams["n2"];
+    m_R["m2"] = m_mixparams["m2"];
+    m_R["sd2"] = m_mixparams["sd2"];
+    m_R.parseEvalQ("y <- c(rnorm(as.numeric(n1), as.numeric(m1), as.numeric(sd1)), rnorm(as.numeric(n2), as.numeric(m2), as.numeric(sd2)));");
+}
+
+void QtDensity::getBandwidth(int bw) {
+    if (bw != m_bw) {
+	m_bw = bw;
+	//std::cout << "Bandwidth now " << m_bw << std::endl;
+	plot();
+    }
+}
+
+void QtDensity::getKernel(int kernel) {
+    if (kernel != m_kernel) {
+	m_kernel = kernel;
+	//std::cout << "Kernel now " << kernel << std::endl;
+	plot();
+    }
+}
+
+void QtDensity::getN1(QString txt) {
+    if (txt != "") {
+	m_mixparams["n1"] = txt.toStdString();
+	draw();
+	plot();
+    }
+}
+
+void QtDensity::getM1(QString txt) {
+    if (txt != "") {
+	m_mixparams["m1"] = txt.toStdString();
+	draw();
+	plot();
+    }
+}
+
+void QtDensity::getSD1(QString txt) {
+    if (txt != "") {
+	m_mixparams["sd1"] = txt.toStdString();
+	draw();
+	plot();
+    }
+}
+
+void QtDensity::getN2(QString txt) {
+    if (txt != "") {
+	m_mixparams["n2"] = txt.toStdString();
+	draw();
+	plot();
+    }
+}
+
+void QtDensity::getM2(QString txt) {
+    if (txt != "") {
+	m_mixparams["m2"] = txt.toStdString();
+	draw();
+	plot();
+    }
+}
+
+void QtDensity::getSD2(QString txt) {
+    m_mixparams["sd2"] = txt.toStdString();
+    draw();
+    plot();
+}

Added: pkg/inst/examples/qt/qtdensity.h
===================================================================
--- pkg/inst/examples/qt/qtdensity.h	                        (rev 0)
+++ pkg/inst/examples/qt/qtdensity.h	2011-03-16 00:54:23 UTC (rev 187)
@@ -0,0 +1,51 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Qt usage example for RInside, inspired by the standard 'density
+// sliders' example for other GUI toolkits
+//
+// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+
+#ifndef QTDENSITY_H
+#define QTDENSITY_H
+
+#include <RInside.h>
+
+#include <QMainWindow>
+#include <QHBoxLayout>
+#include <QSlider>
+#include <QSpinBox>
+#include <QLabel>
+
+class QtDensity : public QMainWindow
+{
+    Q_OBJECT
+
+public:
+    QtDensity(RInside & R);
+    ~QtDensity();
+
+private slots:
+    void getBandwidth(int bw);
+    void getKernel(int kernel);
+    void getN1(QString txt);
+    void getM1(QString txt);
+    void getSD1(QString txt);
+    void getN2(QString txt);
+    void getM2(QString txt);
+    void getSD2(QString txt);
+
+private:
+    void plot(void);
+    void draw(void);
+
+    QLabel *imageLabel;
+    QImage *image;
+
+    RInside & m_R;		// reference to the R instance passed to constructor
+    std::string m_tempfile;	// name of file used by R for plots
+    int m_bw, m_kernel;
+    std::vector< std::string > m_kernelstrings;
+    std::map<std::string, std::string> m_mixparams; 	// simple map for n1,n2,m1,m2,sd1,sd2 
+};
+
+#endif

Added: pkg/inst/examples/qt/qtdensity.pro
===================================================================
--- pkg/inst/examples/qt/qtdensity.pro	                        (rev 0)
+++ pkg/inst/examples/qt/qtdensity.pro	2011-03-16 00:54:23 UTC (rev 187)
@@ -0,0 +1,44 @@
+## -*- mode: Makefile; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+##
+## Qt usage example for RInside, inspired by the standard 'density
+## sliders' example for other GUI toolkits
+##
+## Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+
+TEMPLATE = 		app
+HEADERS =		qtdensity.h 
+SOURCES = 		qtdensity.cpp main.cpp
+
+
+## comment this out if you need a different version of R, 
+## and set set R_HOME accordingly as an environment variable
+R_HOME = 		$$system(R RHOME)
+#message("R_HOME is" $$R_HOME)
+
+## include headers and libraries for R 
+RCPPFLAGS = 		$$system($$R_HOME/bin/R CMD config --cppflags)
+RLDFLAGS = 		$$system($$R_HOME/bin/R CMD config --ldflags)
+RBLAS = 		$$system($$R_HOME/bin/R CMD config BLAS_LIBS)
+RLAPACK = 		$$system($$R_HOME/bin/R CMD config LAPACK_LIBS)
+
+## if you need to set an rpath to R itself, also uncomment
+#RRPATH =		-Wl,-rpath,$$R_HOME/lib
+
+## include headers and libraries for Rcpp interface classes
+RCPPINCL = 		$$system($$R_HOME/bin/Rscript -e \'Rcpp:::CxxFlags\(\)\')
+RCPPLIBS = 		$$system($$R_HOME/bin/Rscript -e \'Rcpp:::LdFlags\(\)\')
+
+## for some reason when building with Qt we get this each time
+##   /usr/local/lib/R/site-library/Rcpp/include/Rcpp/module/Module_generated_ctor_signature.h:25: warning: unused parameter ‘classname
+## so we turn unused parameter warnings off
+RCPPWARNING =		-Wno-unused-parameter 
+## include headers and libraries for RInside embedding classes
+RINSIDEINCL = 		$$system($$R_HOME/bin/Rscript -e \'RInside:::CxxFlags\(\)\')
+RINSIDELIBS = 		$$system($$R_HOME/bin/Rscript -e \'RInside:::LdFlags\(\)\')
+
+## compiler etc settings used in default make rules
+QMAKE_CXXFLAGS +=	$$RCPPWARNING $$RCPPFLAGS $$RCPPINCL $$RINSIDEINCL
+QMAKE_LFLAGS +=         $$RLDFLAGS $$RBLAS $$RLAPACK $$RCPPLIBS $$RINSIDELIBS
+
+## addition clean targets
+QMAKE_CLEAN +=		qtdensity Makefile



More information about the Rinside-commits mailing list