[Rinside-commits] r195 - in pkg/inst/examples/qt: . qtdensity qtdensitySVG
noreply at r-forge.r-project.org
noreply at r-forge.r-project.org
Fri Mar 25 14:00:51 CET 2011
Author: edd
Date: 2011-03-25 14:00:51 +0100 (Fri, 25 Mar 2011)
New Revision: 195
Added:
pkg/inst/examples/qt/qtdensity/README
pkg/inst/examples/qt/qtdensity/main.cpp
pkg/inst/examples/qt/qtdensity/qtdensity.cpp
pkg/inst/examples/qt/qtdensity/qtdensity.h
pkg/inst/examples/qt/qtdensity/qtdensity.pro
pkg/inst/examples/qt/qtdensitySVG/README
pkg/inst/examples/qt/qtdensitySVG/main.cpp
pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp
pkg/inst/examples/qt/qtdensitySVG/qtdensity.h
pkg/inst/examples/qt/qtdensitySVG/qtdensity.pro
Removed:
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
Log:
split into two directories for standard (png) and SVG
Deleted: pkg/inst/examples/qt/README
===================================================================
--- pkg/inst/examples/qt/README 2011-03-25 12:58:25 UTC (rev 194)
+++ pkg/inst/examples/qt/README 2011-03-25 13:00:51 UTC (rev 195)
@@ -1,19 +0,0 @@
-
-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.
-
-
-
Deleted: pkg/inst/examples/qt/main.cpp
===================================================================
--- pkg/inst/examples/qt/main.cpp 2011-03-25 12:58:25 UTC (rev 194)
+++ pkg/inst/examples/qt/main.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -1,20 +0,0 @@
-// -*- 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/README
===================================================================
--- pkg/inst/examples/qt/qtdensity/README (rev 0)
+++ pkg/inst/examples/qt/qtdensity/README 2011-03-25 13:00:51 UTC (rev 195)
@@ -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/qtdensity/main.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensity/main.cpp (rev 0)
+++ pkg/inst/examples/qt/qtdensity/main.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -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/qtdensity.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensity/qtdensity.cpp (rev 0)
+++ pkg/inst/examples/qt/qtdensity/qtdensity.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -0,0 +1,148 @@
+// -*- 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; // initial bandwidth, will be scaled by 100 so 1.0
+ m_kernel = 0; // initial kernel: gaussian
+ m_cmd = "c(rnorm(100,0,1), rnorm(50,5,1))"; // simple mixture
+
+ m_R["bw"] = m_bw; // pass bandwidth to R, and have R compute a temp.file name
+ m_tempfile = Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()"));
+
+ setupDisplay();
+}
+
+void QtDensity::setupDisplay(void) {
+ QWidget *window = new QWidget;
+ window->setWindowTitle("Qt and RInside 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)));
+
+ QLabel *cmdLabel = new QLabel("R command for random data creation");
+ QLineEdit *cmdEntry = new QLineEdit(QString(m_cmd.c_str()));
+ QObject::connect(cmdEntry, SIGNAL(textEdited(QString)), this, SLOT(getRandomDataCmd(QString)));
+ QObject::connect(cmdEntry, SIGNAL(editingFinished()), this, SLOT(runRandomDataCmd()));
+
+ 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(260,140);
+ kernelRadioBox->setMaximumSize(260,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)));
+
+ imageLabel = new QLabel;
+ imageLabel->setBackgroundRole(QPalette::Base);
+ imageLabel->setSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored);
+ imageLabel->setScaledContents(true);
+
+ image = new QImage(QString(m_tempfile.c_str()));
+ runRandomDataCmd();
+ plot(); // now that we have an image, we can diplay
+
+ QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
+ QHBoxLayout *spinners = new QHBoxLayout;
+ spinners->addWidget(spinBox);
+ spinners->addWidget(slider);
+ QVBoxLayout *topright = new QVBoxLayout;
+ topright->addLayout(spinners);
+ topright->addWidget(cmdLabel);
+ topright->addWidget(cmdEntry);
+ estimationBox->setMinimumSize(360,140);
+ estimationBox->setMaximumSize(360,140);
+ estimationBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+ estimationBox->setLayout(topright);
+ QHBoxLayout *upperlayout = new QHBoxLayout;
+ upperlayout->addWidget(kernelRadioBox);
+ upperlayout->addWidget(estimationBox);
+
+ QHBoxLayout *lowerlayout = new QHBoxLayout;
+ lowerlayout->addWidget(imageLabel);
+
+ QVBoxLayout *outer = new QVBoxLayout;
+ outer->addLayout(upperlayout);
+ 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) {
+ const char *kernelstrings[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
+ m_R["bw"] = m_bw;
+ m_R["kernel"] = 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 + kernelstrings[m_kernel] + cmd2;
+ m_R.parseEvalQ(cmd);
+ image->load(QString(m_tempfile.c_str()));
+ imageLabel->setPixmap(QPixmap::fromImage(*image));
+}
+
+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::getRandomDataCmd(QString txt) {
+ //std::cout << "Cmd: " << txt.toStdString() << std::endl;
+ m_cmd = txt.toStdString();
+}
+
+void QtDensity::runRandomDataCmd(void) {
+ std::string cmd = "y <- " + m_cmd;
+ //std::cout << "Running: " << cmd << std::endl;
+ m_R.parseEvalQ(cmd);
+ plot();
+}
Added: pkg/inst/examples/qt/qtdensity/qtdensity.h
===================================================================
--- pkg/inst/examples/qt/qtdensity/qtdensity.h (rev 0)
+++ pkg/inst/examples/qt/qtdensity/qtdensity.h 2011-03-25 13:00:51 UTC (rev 195)
@@ -0,0 +1,46 @@
+// -*- 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 getRandomDataCmd(QString txt);
+ void runRandomDataCmd(void);
+
+private:
+ void setupDisplay(void); // standard GUI boilderplate of arranging things
+ void plot(void); // run a density plot in R and update the
+
+ 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; // parameters used to estimate the density
+ std::string m_cmd; // random draw command string
+};
+
+#endif
Added: pkg/inst/examples/qt/qtdensity/qtdensity.pro
===================================================================
--- pkg/inst/examples/qt/qtdensity/qtdensity.pro (rev 0)
+++ pkg/inst/examples/qt/qtdensity/qtdensity.pro 2011-03-25 13:00:51 UTC (rev 195)
@@ -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
Deleted: pkg/inst/examples/qt/qtdensity.cpp
===================================================================
--- pkg/inst/examples/qt/qtdensity.cpp 2011-03-25 12:58:25 UTC (rev 194)
+++ pkg/inst/examples/qt/qtdensity.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -1,168 +0,0 @@
-// -*- 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; // initial bandwidth, will be scaled by 100 so 1.0
- m_kernel = 0; // initial kernel: gaussian
- m_cmd = "c(rnorm(100,0,1), rnorm(50,5,1))"; // simple mixture
-
- m_R["bw"] = m_bw; // pass bandwidth to R, and have R compute a temp.file name
- m_tempfile = Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()"));
- m_svgfile = Rcpp::as<std::string>(m_R.parseEval("sfile <- tempfile()"));
- m_R.parseEvalQ("library(cairoDevice)");
-
- setupDisplay();
-}
-
-void QtDensity::setupDisplay(void) {
- QWidget *window = new QWidget;
- window->setWindowTitle("Qt and RInside 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)));
-
- QLabel *cmdLabel = new QLabel("R command for random data creation");
- QLineEdit *cmdEntry = new QLineEdit(QString(m_cmd.c_str()));
- QObject::connect(cmdEntry, SIGNAL(textEdited(QString)), this, SLOT(getRandomDataCmd(QString)));
- QObject::connect(cmdEntry, SIGNAL(editingFinished()), this, SLOT(runRandomDataCmd()));
-
- 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(260,140);
- kernelRadioBox->setMaximumSize(260,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)));
-
- m_svg = new QSvgWidget();
- runRandomDataCmd(); // also calls plot()
-
- QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
- QHBoxLayout *spinners = new QHBoxLayout;
- spinners->addWidget(spinBox);
- spinners->addWidget(slider);
- QVBoxLayout *topright = new QVBoxLayout;
- topright->addLayout(spinners);
- topright->addWidget(cmdLabel);
- topright->addWidget(cmdEntry);
- estimationBox->setMinimumSize(360,140);
- estimationBox->setMaximumSize(360,140);
- estimationBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
- estimationBox->setLayout(topright);
- QHBoxLayout *upperlayout = new QHBoxLayout;
- upperlayout->addWidget(kernelRadioBox);
- upperlayout->addWidget(estimationBox);
-
- QHBoxLayout *svglayout = new QHBoxLayout;
- svglayout->addWidget(m_svg);
-
- QVBoxLayout *outer = new QVBoxLayout;
- outer->addLayout(upperlayout);
- outer->addLayout(svglayout);
- window->setLayout(outer);
- window->show();
- window->resize(625, 725);
-}
-
-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; // never reached ?
-}
-
-void QtDensity::plot(void) {
- const char *kernelstrings[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
- m_R["bw"] = m_bw;
- m_R["kernel"] = 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 cmd1 = "Cairo_svg(tfile,6,6); 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 + kernelstrings[m_kernel] + cmd2;
- m_R.parseEvalQ(cmd);
- filterFile();
- m_svg->load(QString(m_svgfile.c_str()));
-}
-
-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::getRandomDataCmd(QString txt) {
- //std::cout << "Cmd: " << txt.toStdString() << std::endl;
- m_cmd = txt.toStdString();
-}
-
-void QtDensity::runRandomDataCmd(void) {
- std::string cmd = "y <- " + m_cmd;
- //std::cout << "Running: " << cmd << std::endl;
- m_R.parseEvalQ(cmd);
- plot();
-}
-
-void QtDensity::filterFile() {
- // cairoDevice creates richer SVG than Qt can display
- // but per Michaele Lawrence, a simple trick is to s/symbol/g/ which we do here
-
- QFile infile(m_tempfile.c_str());
- infile.open(QFile::ReadOnly);
- QFile outfile(m_svgfile.c_str());
- outfile.open(QFile::WriteOnly | QFile::Truncate);
-
- QTextStream in(&infile);
- QTextStream out(&outfile);
- QRegExp rx1("<symbol");
- QRegExp rx2("</symbol");
- while (!in.atEnd()) {
- QString line = in.readLine();
- line.replace(rx1, "<g");
- line.replace(rx2, "</g");
- out << line << "\n";
- }
- infile.close();
- outfile.close();
-}
Deleted: pkg/inst/examples/qt/qtdensity.h
===================================================================
--- pkg/inst/examples/qt/qtdensity.h 2011-03-25 12:58:25 UTC (rev 194)
+++ pkg/inst/examples/qt/qtdensity.h 2011-03-25 13:00:51 UTC (rev 195)
@@ -1,49 +0,0 @@
-// -*- 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>
-#include <QTemporaryFile>
-#include <QSvgWidget>
-
-class QtDensity : public QMainWindow
-{
- Q_OBJECT
-
-public:
- QtDensity(RInside & R);
- ~QtDensity();
-
-private slots:
- void getBandwidth(int bw);
- void getKernel(int kernel);
- void getRandomDataCmd(QString txt);
- void runRandomDataCmd(void);
-
-private:
- void setupDisplay(void); // standard GUI boilderplate of arranging things
- void plot(void); // run a density plot in R and update the
- void filterFile(void); // modify the richer SVG produced by R
-
- QSvgWidget *m_svg; // the SVG device
-
- RInside & m_R; // reference to the R instance passed to constructor
- std::string m_tempfile; // name of file used by R for plots
- std::string m_svgfile; // another temp file, this time from Qt
- int m_bw, m_kernel; // parameters used to estimate the density
- std::string m_cmd; // random draw command string
-};
-
-#endif
Deleted: pkg/inst/examples/qt/qtdensity.pro
===================================================================
--- pkg/inst/examples/qt/qtdensity.pro 2011-03-25 12:58:25 UTC (rev 194)
+++ pkg/inst/examples/qt/qtdensity.pro 2011-03-25 13:00:51 UTC (rev 195)
@@ -1,45 +0,0 @@
-## -*- 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
-
-QT += svg
-
-## 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
Copied: pkg/inst/examples/qt/qtdensitySVG/README (from rev 192, pkg/inst/examples/qt/README)
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/README (rev 0)
+++ pkg/inst/examples/qt/qtdensitySVG/README 2011-03-25 13:00:51 UTC (rev 195)
@@ -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.
+
+
+
Copied: pkg/inst/examples/qt/qtdensitySVG/main.cpp (from rev 192, pkg/inst/examples/qt/main.cpp)
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/main.cpp (rev 0)
+++ pkg/inst/examples/qt/qtdensitySVG/main.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -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();
+}
Copied: pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp (from rev 192, pkg/inst/examples/qt/qtdensity.cpp)
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp (rev 0)
+++ pkg/inst/examples/qt/qtdensitySVG/qtdensity.cpp 2011-03-25 13:00:51 UTC (rev 195)
@@ -0,0 +1,168 @@
+// -*- 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; // initial bandwidth, will be scaled by 100 so 1.0
+ m_kernel = 0; // initial kernel: gaussian
+ m_cmd = "c(rnorm(100,0,1), rnorm(50,5,1))"; // simple mixture
+
+ m_R["bw"] = m_bw; // pass bandwidth to R, and have R compute a temp.file name
+ m_tempfile = Rcpp::as<std::string>(m_R.parseEval("tfile <- tempfile()"));
+ m_svgfile = Rcpp::as<std::string>(m_R.parseEval("sfile <- tempfile()"));
+ m_R.parseEvalQ("library(cairoDevice)");
+
+ setupDisplay();
+}
+
+void QtDensity::setupDisplay(void) {
+ QWidget *window = new QWidget;
+ window->setWindowTitle("Qt and RInside 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)));
+
+ QLabel *cmdLabel = new QLabel("R command for random data creation");
+ QLineEdit *cmdEntry = new QLineEdit(QString(m_cmd.c_str()));
+ QObject::connect(cmdEntry, SIGNAL(textEdited(QString)), this, SLOT(getRandomDataCmd(QString)));
+ QObject::connect(cmdEntry, SIGNAL(editingFinished()), this, SLOT(runRandomDataCmd()));
+
+ 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(260,140);
+ kernelRadioBox->setMaximumSize(260,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)));
+
+ m_svg = new QSvgWidget();
+ runRandomDataCmd(); // also calls plot()
+
+ QGroupBox *estimationBox = new QGroupBox("Density estimation bandwidth (scaled by 100)");
+ QHBoxLayout *spinners = new QHBoxLayout;
+ spinners->addWidget(spinBox);
+ spinners->addWidget(slider);
+ QVBoxLayout *topright = new QVBoxLayout;
+ topright->addLayout(spinners);
+ topright->addWidget(cmdLabel);
+ topright->addWidget(cmdEntry);
+ estimationBox->setMinimumSize(360,140);
+ estimationBox->setMaximumSize(360,140);
+ estimationBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
+ estimationBox->setLayout(topright);
+ QHBoxLayout *upperlayout = new QHBoxLayout;
+ upperlayout->addWidget(kernelRadioBox);
+ upperlayout->addWidget(estimationBox);
+
+ QHBoxLayout *svglayout = new QHBoxLayout;
+ svglayout->addWidget(m_svg);
+
+ QVBoxLayout *outer = new QVBoxLayout;
+ outer->addLayout(upperlayout);
+ outer->addLayout(svglayout);
+ window->setLayout(outer);
+ window->show();
+ window->resize(625, 725);
+}
+
+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; // never reached ?
+}
+
+void QtDensity::plot(void) {
+ const char *kernelstrings[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
+ m_R["bw"] = m_bw;
+ m_R["kernel"] = 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 cmd1 = "Cairo_svg(tfile,6,6); 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 + kernelstrings[m_kernel] + cmd2;
+ m_R.parseEvalQ(cmd);
+ filterFile();
+ m_svg->load(QString(m_svgfile.c_str()));
+}
+
+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::getRandomDataCmd(QString txt) {
+ //std::cout << "Cmd: " << txt.toStdString() << std::endl;
+ m_cmd = txt.toStdString();
+}
+
+void QtDensity::runRandomDataCmd(void) {
+ std::string cmd = "y <- " + m_cmd;
+ //std::cout << "Running: " << cmd << std::endl;
+ m_R.parseEvalQ(cmd);
+ plot();
+}
+
+void QtDensity::filterFile() {
+ // cairoDevice creates richer SVG than Qt can display
+ // but per Michaele Lawrence, a simple trick is to s/symbol/g/ which we do here
+
+ QFile infile(m_tempfile.c_str());
+ infile.open(QFile::ReadOnly);
+ QFile outfile(m_svgfile.c_str());
+ outfile.open(QFile::WriteOnly | QFile::Truncate);
+
+ QTextStream in(&infile);
+ QTextStream out(&outfile);
+ QRegExp rx1("<symbol");
+ QRegExp rx2("</symbol");
+ while (!in.atEnd()) {
+ QString line = in.readLine();
+ line.replace(rx1, "<g");
+ line.replace(rx2, "</g");
+ out << line << "\n";
+ }
+ infile.close();
+ outfile.close();
+}
Copied: pkg/inst/examples/qt/qtdensitySVG/qtdensity.h (from rev 192, pkg/inst/examples/qt/qtdensity.h)
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/qtdensity.h (rev 0)
+++ pkg/inst/examples/qt/qtdensitySVG/qtdensity.h 2011-03-25 13:00:51 UTC (rev 195)
@@ -0,0 +1,49 @@
+// -*- 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>
+#include <QTemporaryFile>
+#include <QSvgWidget>
+
+class QtDensity : public QMainWindow
+{
+ Q_OBJECT
+
+public:
+ QtDensity(RInside & R);
+ ~QtDensity();
+
+private slots:
+ void getBandwidth(int bw);
+ void getKernel(int kernel);
+ void getRandomDataCmd(QString txt);
+ void runRandomDataCmd(void);
+
+private:
+ void setupDisplay(void); // standard GUI boilderplate of arranging things
+ void plot(void); // run a density plot in R and update the
+ void filterFile(void); // modify the richer SVG produced by R
+
+ QSvgWidget *m_svg; // the SVG device
+
+ RInside & m_R; // reference to the R instance passed to constructor
+ std::string m_tempfile; // name of file used by R for plots
+ std::string m_svgfile; // another temp file, this time from Qt
+ int m_bw, m_kernel; // parameters used to estimate the density
+ std::string m_cmd; // random draw command string
+};
+
+#endif
Copied: pkg/inst/examples/qt/qtdensitySVG/qtdensity.pro (from rev 192, pkg/inst/examples/qt/qtdensity.pro)
===================================================================
--- pkg/inst/examples/qt/qtdensitySVG/qtdensity.pro (rev 0)
+++ pkg/inst/examples/qt/qtdensitySVG/qtdensity.pro 2011-03-25 13:00:51 UTC (rev 195)
@@ -0,0 +1,45 @@
+## -*- 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
+
+QT += svg
+
+## comment this out if you need a different version of R,
[TRUNCATED]
To get the complete diff run:
svnlook diff /svnroot/rinside -r 195
More information about the Rinside-commits
mailing list