[Rinside-commits] r236 - in pkg: . inst inst/examples/wt

noreply at r-forge.r-project.org noreply at r-forge.r-project.org
Fri Mar 30 16:32:11 CEST 2012


Author: edd
Date: 2012-03-30 16:32:10 +0200 (Fri, 30 Mar 2012)
New Revision: 236

Added:
   pkg/inst/examples/wt/wtdensity.cpp
   pkg/inst/examples/wt/wtdensity.css
   pkg/inst/examples/wt/wtdensity.xml
   pkg/inst/examples/wt/wtdensityPlain.cpp
Removed:
   pkg/inst/examples/wt/wtdensity.cpp
Modified:
   pkg/ChangeLog
   pkg/inst/NEWS
   pkg/inst/examples/wt/Makefile
Log:
update Wt example, which is now much prettier thanks to CSS / XML


Modified: pkg/ChangeLog
===================================================================
--- pkg/ChangeLog	2012-02-14 20:56:33 UTC (rev 235)
+++ pkg/ChangeLog	2012-03-30 14:32:10 UTC (rev 236)
@@ -1,6 +1,15 @@
+2012-03-30  Dirk Eddelbuettel  <edd at debian.org>
+
+	* inst/examples/wt/wtdensity.cpp: Extended / prettyfied Wt example to
+	use CSS styling as well as some simple headers and descriptions
+	* inst/examples/wt/wtdensity.css: Added simple CSS markup
+	* inst/examples/wt/wtdensity.xml: Added text and headers
+
+	* inst/examples/wt/wtdensityPlain.cpp: Renamed old version
+
 2012-01-18  Dirk Eddelbuettel  <edd at debian.org>
 
-	* DESCRIPTION: Added BugReports: and MailingList: fields 
+	* DESCRIPTION: Added BugReports: and MailingList: fields
 
 2012-01-15  Dirk Eddelbuettel  <edd at debian.org>
 

Modified: pkg/inst/NEWS
===================================================================
--- pkg/inst/NEWS	2012-02-14 20:56:33 UTC (rev 235)
+++ pkg/inst/NEWS	2012-03-30 14:32:10 UTC (rev 236)
@@ -1,3 +1,8 @@
+0.2.7   2012-xx-yy
+
+   o    Prettified the Wt example 'web application' with CSS use, also added
+        and XML file with simple headers and description text
+
 0.2.6   2012-01-11
 
    o    Correct Windows initialization by not using Rprintf in internal

Modified: pkg/inst/examples/wt/Makefile
===================================================================
--- pkg/inst/examples/wt/Makefile	2012-02-14 20:56:33 UTC (rev 235)
+++ pkg/inst/examples/wt/Makefile	2012-03-30 14:32:10 UTC (rev 236)
@@ -38,18 +38,25 @@
 
 
 
-all:			wtdensity
+all:			wtdensity wtdensityPlain
 
+wtdensityPlain:		wtdensityPlain.cpp
+			$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDLIBS) 
+			strip $@
+
 wtdensity:		wtdensity.cpp
-			$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o wtdensity wtdensity.cpp $(LDLIBS) 
-                        ## optionally strip the binary
-			strip wtdensity
+			$(CXX) $(CPPFLAGS) $(CXXFLAGS) -o $@ $^ $(LDLIBS) 
+			strip $@
 
-run:
+run:			wtdensity
                         ## Wt resources location on Debian/Ubuntu; adjust as needed
 			test -L resources || ln -s /usr/lib/Wt/resources .
-                        ## launch, pointing to resources linked and /tmp dir
-			./wtdensity --docroot ".;/resources,/tmp" --http-addr 127.0.0.1 --http-port 8080
+			./$< --docroot "." --http-addr 127.0.0.1 --http-port 8080
 
+runPlain:		wtdensityPlain
+                        ## Wt resources location on Debian/Ubuntu; adjust as needed
+			test -L resources || ln -s /usr/lib/Wt/resources .
+			./$< --docroot "." --http-addr 127.0.0.1 --http-port 8081
+
 clean:
 			rm -f wtdensity resources

Deleted: pkg/inst/examples/wt/wtdensity.cpp
===================================================================
--- pkg/inst/examples/wt/wtdensity.cpp	2012-02-14 20:56:33 UTC (rev 235)
+++ pkg/inst/examples/wt/wtdensity.cpp	2012-03-30 14:32:10 UTC (rev 236)
@@ -1,195 +0,0 @@
-// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
-//
-// Wt usage example for RInside, inspired by the standard 'density sliders' example
-//
-// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
-//
-// This file is licensed under GPL 2 or later, as are the rest of RInside and Rcpp
-//
-// Derived from hello.C in the Wt sources
-// Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
-
-#include <cstdio>
-
-#include <Wt/WApplication>
-#include <Wt/WBreak>
-#include <Wt/WContainerWidget>
-#include <Wt/WLineEdit>
-#include <Wt/WPushButton>
-#include <Wt/WText>
-#include <Wt/WImage>
-#include <Wt/WSpinBox>
-#include <Wt/WGroupBox>
-#include <Wt/WButtonGroup>
-#include <Wt/WRadioButton>
-#include <Wt/WHBoxLayout>
-#include <Wt/WEnvironment>
-#include <Wt/WFileResource>
-
-#include <RInside.h>
-
-using namespace Wt;
-
-class DensityApp : public WApplication {
-public:
-    DensityApp(const WEnvironment& env, RInside & R);
-
-private:
-    WLineEdit *codeEdit_;	// to edit the RNG draw expression
-    WButtonGroup *group_;	// holds the radiobuttons
-    WSpinBox *spin_;		// selects the density bandwidth
-    WImage *img_;		// displays the image
-    WFileResource *imgfile_;	// controls the file resources
-    WText *greeting_;		// text label for status message
-
-    void reportButton();	// called when new button selected
-    void reportEdit();		// called when RNG expression edited
-    void reportSpinner();	// called when bandwidth changed
-    void plot();		// to call R for new plot
-    
-    enum Kernel { Gaussian     = 0, Epanechnikov = 1, Rectangular  = 2,
-		  Triangular   = 3, Cosine       = 4 };
-  
-    RInside & R_;		// reference to embedded R instance
-    std::string tempfile_;	// name of file used by R for plots
-    int bw_, kernel_;		// parameters used to estimate the density
-    std::string cmd_;		// random draw command string
-    Rcpp::NumericVector Yvec_;	// the random draw
-};
-
-// The env argument contains information about the new session, and the initial request. 
-// It must be passed to the WApplication // constructor so it is typically also an argument
-// for your custom application constructor.
-DensityApp::DensityApp(const WEnvironment& env, RInside & R) : WApplication(env), R_(R) {
-
-    setTitle("Witty WebApp With RInside");			// application title
-
-    std::string tfcmd = "tfile <- tempfile(pattern=\"img\", tmpdir=\"/tmp\", fileext=\".png\")";	
-    tempfile_ = Rcpp::as<std::string>(R_.parseEval(tfcmd));  	// assign to 'tfile' in R, and report back
-    bw_ = 100; 
-    kernel_ = 0;						// parameters used to estimate the density
-    cmd_ = "c(rnorm(100,0,1), rnorm(50,5,1))";			// random draw command string
-   
-    Wt::WGroupBox *wc = new Wt::WGroupBox("Density Estimation", root());
-
-    Wt::WHBoxLayout *layout = new Wt::WHBoxLayout();
-    Wt::WContainerWidget *midbox = new Wt::WContainerWidget(root());
-    layout->addWidget(midbox);
-    Wt::WContainerWidget *container = new Wt::WContainerWidget(root());
-    layout->addWidget(container);
-
-    wc->setLayout(layout, AlignTop | AlignJustify);
-
-    midbox->addWidget(new WText("Density estimation scale factor (div. by 100)"));
-    midbox->addWidget(new WBreak());                       	// insert a line break
-    spin_ = new WSpinBox(midbox);
-    spin_->setRange(5, 200);
-    spin_->setValue(bw_);
-    spin_->valueChanged().connect(this, &DensityApp::reportSpinner);
-
-    midbox->addWidget(new WBreak());                       	// insert a line break
-    midbox->addWidget(new WText("R Command for data generation"));  // show some text
-    midbox->addWidget(new WBreak());                       	// insert a line break
-    codeEdit_ = new WLineEdit(midbox);                     	// allow text input
-    codeEdit_->setTextSize(30);
-    codeEdit_->setText(cmd_); 
-    codeEdit_->setFocus();                                 	// give focus
-    codeEdit_->enterPressed().connect(this, &DensityApp::reportEdit);
-
-    group_ = new Wt::WButtonGroup(container);		    	// use button group to arrange radio buttons
-
-    Wt::WRadioButton *button;
-    button = new Wt::WRadioButton("Gaussian", container);
-    new Wt::WBreak(container);
-    group_->addButton(button, Gaussian);
-
-    button = new Wt::WRadioButton("Epanechnikov", container);
-    new Wt::WBreak(container);
-    group_->addButton(button, Epanechnikov);
-
-    button = new Wt::WRadioButton("Rectangular", container);
-    new Wt::WBreak(container);
-    group_->addButton(button, Rectangular);
-
-    button = new Wt::WRadioButton("Triangular", container);
-    new Wt::WBreak(container);
-    group_->addButton(button, Triangular);
-
-    button = new Wt::WRadioButton("Cosine", container);
-    new Wt::WBreak(container);
-    group_->addButton(button, Cosine);
-
-    group_->setCheckedButton(group_->button(kernel_));
-    group_->checkedChanged().connect(this, &DensityApp::reportButton);
-
-    Wt::WGroupBox *botbox = new Wt::WGroupBox("Resulting chart", root());
-    imgfile_ = new Wt::WFileResource("image/png", tempfile_);
-    imgfile_->suggestFileName("density.png");  // name the clients sees of datafile
-    img_ = new Wt::WImage(imgfile_, "PNG version", botbox);
-
-    Wt::WGroupBox *stbox = new Wt::WGroupBox("Status", root());
-    greeting_ = new WText(stbox);                         	// empty text
-    greeting_->setText("Setting up...");
-  
-    reportEdit();						// create a new RNG draw in Yvec_
-    plot();							// and draw a new density plot
-}
-
-void DensityApp::reportButton() {
-    kernel_ = group_->checkedId(); 				// get id of selected kernel 
-    plot();
-}
-
-void DensityApp::reportEdit() {
-    cmd_ = codeEdit_->text().toUTF8();	// get text written in box, as UTF-8, assigned to string
-    std::string rng = "y <- " + cmd_ + ";";
-    R_.parseEvalQ(rng);			// evaluates expression, assigns to 'y'
-    Yvec_ = R_["y"];			// cache the y vector
-    plot();
-}
-
-void DensityApp::reportSpinner() {
-    bw_ = spin_->value();		// get the value of the spin selector
-    plot();
-}
-
-void DensityApp::plot() {
-    const char *kernelstr[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
-    greeting_->setText("Starting R call");
-    R_["tfile"]  = tempfile_;
-    R_["bw"]     = bw_;
-    R_["kernel"] = kernelstr[kernel_]; 			// passes the string to R
-    R_["y"]      = Yvec_;
-    std::string cmd0 = "png(filename=tfile,width=600,height=400); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
-    std::string cmd1 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
-    std::string cmd = cmd0 + kernelstr[kernel_] + cmd1; // stick the selected kernel in the middle
-    R_.parseEvalQ(cmd);				     	// evaluate command -- generates new density plot
-    imgfile_->setChanged();				// important: tells consumer that image has changed, forces refresh
-    greeting_->setText("Finished request from " + this->environment().clientAddress() + " using " + this->environment().userAgent()) ;
-}
-
-WApplication *createApplication(const WEnvironment& env) {
-    // You could read information from the environment to decide whether
-    // the user has permission to start a new application
-    //
-    // We grab an instance of the embedded R. Note we can start only one,
-    // so resource conflicts have to be managed (eg add mutexes etc)
-    //
-    return new DensityApp(env, RInside::instance());
-}
-
-int main(int argc, char **argv) {
-
-    RInside R(argc, argv);              // create the one embedded R instance 
-
-    // Your main method may set up some shared resources, but should then
-    // start the server application (FastCGI or httpd) that starts listening
-    // for requests, and handles all of the application life cycles.
-    //
-    // The last argument to WRun specifies the function that will instantiate
-    // new application objects. That function is executed when a new user surfs
-    // to the Wt application, and after the library has negotiated browser
-    // support. The function should return a newly instantiated application
-    // object.
-    return WRun(argc, argv, createApplication);
-}

Added: pkg/inst/examples/wt/wtdensity.cpp
===================================================================
--- pkg/inst/examples/wt/wtdensity.cpp	                        (rev 0)
+++ pkg/inst/examples/wt/wtdensity.cpp	2012-03-30 14:32:10 UTC (rev 236)
@@ -0,0 +1,208 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Wt usage example for RInside, inspired by the standard 'density sliders' example
+//
+// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+//
+// This file is licensed under GPL 2 or later, as are the rest of RInside and Rcpp
+//
+// Derived from hello.C in the Wt sources
+// Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
+
+#include <cstdio>
+
+#include <Wt/WApplication>
+#include <Wt/WBreak>
+#include <Wt/WContainerWidget>
+#include <Wt/WLineEdit>
+#include <Wt/WPushButton>
+#include <Wt/WText>
+#include <Wt/WImage>
+#include <Wt/WSpinBox>
+#include <Wt/WGroupBox>
+#include <Wt/WButtonGroup>
+#include <Wt/WRadioButton>
+#include <Wt/WHBoxLayout>
+#include <Wt/WEnvironment>
+#include <Wt/WFileResource>
+
+#include <RInside.h>
+
+using namespace Wt;
+
+class DensityApp : public WApplication {
+public:
+    DensityApp(const WEnvironment& env, RInside & R);
+
+private:
+    WLineEdit *codeEdit_;	// to edit the RNG draw expression
+    WButtonGroup *group_;	// holds the radiobuttons
+    WSpinBox *spin_;		// selects the density bandwidth
+    WImage *img_;		// displays the image
+    WFileResource *imgfile_;	// controls the file resources
+    WText *greeting_;		// text label for status message
+
+    void reportButton();	// called when new button selected
+    void reportEdit();		// called when RNG expression edited
+    void reportSpinner();	// called when bandwidth changed
+    void plot();		// to call R for new plot
+    
+    enum Kernel { Gaussian     = 0, Epanechnikov = 1, Rectangular  = 2,
+		  Triangular   = 3, Cosine       = 4 };
+  
+    RInside & R_;		// reference to embedded R instance
+    std::string tempfile_;	// name of file used by R for plots
+    int bw_, kernel_;		// parameters used to estimate the density
+    std::string cmd_;		// random draw command string
+    Rcpp::NumericVector Yvec_;	// the random draw
+};
+
+// The env argument contains information about the new session, and the initial request. 
+// It must be passed to the WApplication // constructor so it is typically also an argument
+// for your custom application constructor.
+DensityApp::DensityApp(const WEnvironment& env, RInside & R) : WApplication(env), R_(R) {
+
+    setTitle("Witty WebApp With RInside");			// application title
+
+    setCssTheme("polished");
+    messageResourceBundle().use(appRoot() + "wtdensity");
+
+    new WText(WString::tr("overview"), root());
+
+    std::string tfcmd = "tfile <- tempfile(pattern=\"img\", tmpdir=\"/tmp\", fileext=\".png\")";	
+    tempfile_ = Rcpp::as<std::string>(R_.parseEval(tfcmd));  	// assign to 'tfile' in R, and report back
+    bw_ = 100; 
+    kernel_ = 0;						// parameters used to estimate the density
+    cmd_ = "c(rnorm(100,0,1), rnorm(50,5,1))";			// random draw command string
+   
+    new WText(WString::tr("user input"), root());
+    Wt::WContainerWidget *wc = new Wt::WContainerWidget(root());
+    wc->setStyleClass("box");
+
+    Wt::WHBoxLayout *layout = new Wt::WHBoxLayout();
+    Wt::WContainerWidget *midbox = new Wt::WContainerWidget(root());
+    layout->addWidget(midbox);
+    Wt::WContainerWidget *container = new Wt::WContainerWidget(root());
+    layout->addWidget(container);
+
+    wc->setLayout(layout, AlignTop | AlignJustify);
+
+    midbox->addWidget(new WText("Density estimation scale factor (div. by 100)"));
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    spin_ = new WSpinBox(midbox);
+    spin_->setRange(5, 200);
+    spin_->setValue(bw_);
+    spin_->valueChanged().connect(this, &DensityApp::reportSpinner);
+
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    midbox->addWidget(new WText("R Command for data generation"));  // show some text
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    codeEdit_ = new WLineEdit(midbox);                     	// allow text input
+    codeEdit_->setTextSize(30);
+    codeEdit_->setText(cmd_); 
+    codeEdit_->setFocus();                                 	// give focus
+    codeEdit_->enterPressed().connect(this, &DensityApp::reportEdit);
+
+    group_ = new Wt::WButtonGroup(container);		    	// use button group to arrange radio buttons
+
+    Wt::WRadioButton *button;
+    button = new Wt::WRadioButton("Gaussian", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Gaussian);
+
+    button = new Wt::WRadioButton("Epanechnikov", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Epanechnikov);
+
+    button = new Wt::WRadioButton("Rectangular", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Rectangular);
+
+    button = new Wt::WRadioButton("Triangular", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Triangular);
+
+    button = new Wt::WRadioButton("Cosine", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Cosine);
+
+    group_->setCheckedButton(group_->button(kernel_));
+    group_->checkedChanged().connect(this, &DensityApp::reportButton);
+
+    new WText(WString::tr("r result"), root());
+    Wt::WContainerWidget *botbox = new Wt::WContainerWidget(root());
+    botbox->setStyleClass("box");
+    imgfile_ = new Wt::WFileResource("image/png", tempfile_);
+    imgfile_->suggestFileName("density.png");  // name the clients sees of datafile
+    img_ = new Wt::WImage(imgfile_, "PNG version", botbox);
+
+    new WText(WString::tr("browser info"), root());
+    Wt::WContainerWidget *stbox = new Wt::WContainerWidget(root());
+    stbox->setStyleClass("box");
+    greeting_ = new WText(stbox);                         	// empty text
+    greeting_->setText("Setting up...");
+
+    useStyleSheet("wtdensity.css");    				// set our style sheet last
+  
+    reportEdit();						// create a new RNG draw in Yvec_
+    plot();							// and draw a new density plot
+}
+
+void DensityApp::reportButton() {
+    kernel_ = group_->checkedId(); 				// get id of selected kernel 
+    plot();
+}
+
+void DensityApp::reportEdit() {
+    cmd_ = codeEdit_->text().toUTF8();	// get text written in box, as UTF-8, assigned to string
+    std::string rng = "y <- " + cmd_ + ";";
+    R_.parseEvalQ(rng);			// evaluates expression, assigns to 'y'
+    Yvec_ = R_["y"];			// cache the y vector
+    plot();
+}
+
+void DensityApp::reportSpinner() {
+    bw_ = spin_->value();		// get the value of the spin selector
+    plot();
+}
+
+void DensityApp::plot() {
+    const char *kernelstr[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
+    greeting_->setText("Starting R call");
+    R_["tfile"]  = tempfile_;
+    R_["bw"]     = bw_;
+    R_["kernel"] = kernelstr[kernel_]; 			// passes the string to R
+    R_["y"]      = Yvec_;
+    std::string cmd0 = "png(filename=tfile,width=600,height=400); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+    std::string cmd1 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
+    std::string cmd = cmd0 + kernelstr[kernel_] + cmd1; // stick the selected kernel in the middle
+    R_.parseEvalQ(cmd);				     	// evaluate command -- generates new density plot
+    imgfile_->setChanged();				// important: tells consumer that image has changed, forces refresh
+    greeting_->setText("Finished request from " + this->environment().clientAddress() + " using " + this->environment().userAgent()) ;
+}
+
+WApplication *createApplication(const WEnvironment& env) {
+    // You could read information from the environment to decide whether
+    // the user has permission to start a new application
+    //
+    // We grab an instance of the embedded R. Note we can start only one,
+    // so resource conflicts have to be managed (eg add mutexes etc)
+    //
+    return new DensityApp(env, RInside::instance());
+}
+
+int main(int argc, char **argv) {
+
+    RInside R(argc, argv);              // create the one embedded R instance 
+
+    // Your main method may set up some shared resources, but should then
+    // start the server application (FastCGI or httpd) that starts listening
+    // for requests, and handles all of the application life cycles.
+    //
+    // The last argument to WRun specifies the function that will instantiate
+    // new application objects. That function is executed when a new user surfs
+    // to the Wt application, and after the library has negotiated browser
+    // support. The function should return a newly instantiated application
+    // object.
+    return WRun(argc, argv, createApplication);
+}

Added: pkg/inst/examples/wt/wtdensity.css
===================================================================
--- pkg/inst/examples/wt/wtdensity.css	                        (rev 0)
+++ pkg/inst/examples/wt/wtdensity.css	2012-03-30 14:32:10 UTC (rev 236)
@@ -0,0 +1,25 @@
+body, html {
+  font-family: verdana,helvetica,tahoma,sans-serif;
+}
+
+p.p {
+  margin: 16px 0px;
+  font-size: smaller;
+}
+
+.box {
+  margin: 5px 20px;
+  font-size: smaller;
+  font-family: sans-serif;
+}
+
+h2 {
+  padding-left: 30px;
+  border-bottom: 1px solid #000;
+  font-size: larger;
+}
+
+h2 span {
+  border-bottom: 0.2em solid #000;
+}
+

Added: pkg/inst/examples/wt/wtdensity.xml
===================================================================
--- pkg/inst/examples/wt/wtdensity.xml	                        (rev 0)
+++ pkg/inst/examples/wt/wtdensity.xml	2012-03-30 14:32:10 UTC (rev 236)
@@ -0,0 +1,70 @@
+<?xml version="1.0" encoding="ISO-8859-1" ?>
+<messages>
+  <message id="error-missing-data">
+    <div>The data file "{1}" could not be found. Check your deployment.</div>
+  </message>
+  <message id="overview">
+
+    <h2><span>Overview</span></h2>
+
+    <p class="p">
+      This example demonstrates some of the capabilities of the
+      the <a href="http://www.webtoolkit.eu/wt/">Wt library</a>, in combination with the
+      <a href="http://dirk.eddelbuettel.com/code/rinside.html">RInside</a> classes for 
+      embedding the <a href="http://www.r-project.org">R</a> statistical language and environment.
+    </p>
+
+    <p class="p">
+      It reimplements a standard GUI / application setting:
+      drawing from a random distribution, and estimation a
+      non-parametric density for which the user selects the kernel and bandwidth.
+      <a href="http://dirk.eddelbuettel.com/code/rinside.html">RInside</a> already contains
+      an example of this using <a href="http://www.qt-project.org">Qt</a> to provide 
+      a standard <em>application</em>.
+    </p>
+
+    <p class="p">
+      Here we show how to do the same in a <em>web application</em>
+      which, thanks to the abstractions provided by
+      the <a href="http://www.webtoolkit.eu/wt/">Wt</a>, is rather straightforward.
+    </p>
+  </message>
+
+  <message id="user input">
+    <h2><span>User Input for Density Estimation</span></h2>
+  </message>
+
+  <message id="user input orig">
+    <h2><span>User Input for Density Estimation</span></h2>
+    <p class="p">
+      The user can select a bandwidth factor (to be divided by 100),
+      an R expression to generate data (with a default for a mixture
+      distributions) and a kernel function.
+    </p>
+  </message>
+
+  <message id="r result">
+    <h2><span>Resulting R Chart</span></h2>
+  </message>
+
+  <message id="r result orig">
+
+    <h2><span>Resulting R Chart</span></h2>
+
+    <p class="p">
+      The chart below is created by R given the selected user input.
+    </p>
+  </message>
+
+  <message id="browser info">
+    <h2><span>Browser Information</span></h2>
+  </message>
+
+  <message id="browser info orig">
+    <h2><span>Browser Information</span></h2>
+    <p class="p">
+      The string below is provided by the client's browser and often
+      reveals operating system and IP address.
+    </p>
+  </message>
+</messages>
\ No newline at end of file

Copied: pkg/inst/examples/wt/wtdensityPlain.cpp (from rev 228, pkg/inst/examples/wt/wtdensity.cpp)
===================================================================
--- pkg/inst/examples/wt/wtdensityPlain.cpp	                        (rev 0)
+++ pkg/inst/examples/wt/wtdensityPlain.cpp	2012-03-30 14:32:10 UTC (rev 236)
@@ -0,0 +1,195 @@
+// -*- mode: C++; c-indent-level: 4; c-basic-offset: 4;  tab-width: 8; -*-
+//
+// Wt usage example for RInside, inspired by the standard 'density sliders' example
+//
+// Copyright (C) 2011  Dirk Eddelbuettel and Romain Francois
+//
+// This file is licensed under GPL 2 or later, as are the rest of RInside and Rcpp
+//
+// Derived from hello.C in the Wt sources
+// Copyright (C) 2008 Emweb bvba, Heverlee, Belgium.
+
+#include <cstdio>
+
+#include <Wt/WApplication>
+#include <Wt/WBreak>
+#include <Wt/WContainerWidget>
+#include <Wt/WLineEdit>
+#include <Wt/WPushButton>
+#include <Wt/WText>
+#include <Wt/WImage>
+#include <Wt/WSpinBox>
+#include <Wt/WGroupBox>
+#include <Wt/WButtonGroup>
+#include <Wt/WRadioButton>
+#include <Wt/WHBoxLayout>
+#include <Wt/WEnvironment>
+#include <Wt/WFileResource>
+
+#include <RInside.h>
+
+using namespace Wt;
+
+class DensityApp : public WApplication {
+public:
+    DensityApp(const WEnvironment& env, RInside & R);
+
+private:
+    WLineEdit *codeEdit_;	// to edit the RNG draw expression
+    WButtonGroup *group_;	// holds the radiobuttons
+    WSpinBox *spin_;		// selects the density bandwidth
+    WImage *img_;		// displays the image
+    WFileResource *imgfile_;	// controls the file resources
+    WText *greeting_;		// text label for status message
+
+    void reportButton();	// called when new button selected
+    void reportEdit();		// called when RNG expression edited
+    void reportSpinner();	// called when bandwidth changed
+    void plot();		// to call R for new plot
+    
+    enum Kernel { Gaussian     = 0, Epanechnikov = 1, Rectangular  = 2,
+		  Triangular   = 3, Cosine       = 4 };
+  
+    RInside & R_;		// reference to embedded R instance
+    std::string tempfile_;	// name of file used by R for plots
+    int bw_, kernel_;		// parameters used to estimate the density
+    std::string cmd_;		// random draw command string
+    Rcpp::NumericVector Yvec_;	// the random draw
+};
+
+// The env argument contains information about the new session, and the initial request. 
+// It must be passed to the WApplication // constructor so it is typically also an argument
+// for your custom application constructor.
+DensityApp::DensityApp(const WEnvironment& env, RInside & R) : WApplication(env), R_(R) {
+
+    setTitle("Witty WebApp With RInside");			// application title
+
+    std::string tfcmd = "tfile <- tempfile(pattern=\"img\", tmpdir=\"/tmp\", fileext=\".png\")";	
+    tempfile_ = Rcpp::as<std::string>(R_.parseEval(tfcmd));  	// assign to 'tfile' in R, and report back
+    bw_ = 100; 
+    kernel_ = 0;						// parameters used to estimate the density
+    cmd_ = "c(rnorm(100,0,1), rnorm(50,5,1))";			// random draw command string
+   
+    Wt::WGroupBox *wc = new Wt::WGroupBox("Density Estimation", root());
+
+    Wt::WHBoxLayout *layout = new Wt::WHBoxLayout();
+    Wt::WContainerWidget *midbox = new Wt::WContainerWidget(root());
+    layout->addWidget(midbox);
+    Wt::WContainerWidget *container = new Wt::WContainerWidget(root());
+    layout->addWidget(container);
+
+    wc->setLayout(layout, AlignTop | AlignJustify);
+
+    midbox->addWidget(new WText("Density estimation scale factor (div. by 100)"));
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    spin_ = new WSpinBox(midbox);
+    spin_->setRange(5, 200);
+    spin_->setValue(bw_);
+    spin_->valueChanged().connect(this, &DensityApp::reportSpinner);
+
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    midbox->addWidget(new WText("R Command for data generation"));  // show some text
+    midbox->addWidget(new WBreak());                       	// insert a line break
+    codeEdit_ = new WLineEdit(midbox);                     	// allow text input
+    codeEdit_->setTextSize(30);
+    codeEdit_->setText(cmd_); 
+    codeEdit_->setFocus();                                 	// give focus
+    codeEdit_->enterPressed().connect(this, &DensityApp::reportEdit);
+
+    group_ = new Wt::WButtonGroup(container);		    	// use button group to arrange radio buttons
+
+    Wt::WRadioButton *button;
+    button = new Wt::WRadioButton("Gaussian", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Gaussian);
+
+    button = new Wt::WRadioButton("Epanechnikov", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Epanechnikov);
+
+    button = new Wt::WRadioButton("Rectangular", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Rectangular);
+
+    button = new Wt::WRadioButton("Triangular", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Triangular);
+
+    button = new Wt::WRadioButton("Cosine", container);
+    new Wt::WBreak(container);
+    group_->addButton(button, Cosine);
+
+    group_->setCheckedButton(group_->button(kernel_));
+    group_->checkedChanged().connect(this, &DensityApp::reportButton);
+
+    Wt::WGroupBox *botbox = new Wt::WGroupBox("Resulting chart", root());
+    imgfile_ = new Wt::WFileResource("image/png", tempfile_);
+    imgfile_->suggestFileName("density.png");  // name the clients sees of datafile
+    img_ = new Wt::WImage(imgfile_, "PNG version", botbox);
+
+    Wt::WGroupBox *stbox = new Wt::WGroupBox("Status", root());
+    greeting_ = new WText(stbox);                         	// empty text
+    greeting_->setText("Setting up...");
+  
+    reportEdit();						// create a new RNG draw in Yvec_
+    plot();							// and draw a new density plot
+}
+
+void DensityApp::reportButton() {
+    kernel_ = group_->checkedId(); 				// get id of selected kernel 
+    plot();
+}
+
+void DensityApp::reportEdit() {
+    cmd_ = codeEdit_->text().toUTF8();	// get text written in box, as UTF-8, assigned to string
+    std::string rng = "y <- " + cmd_ + ";";
+    R_.parseEvalQ(rng);			// evaluates expression, assigns to 'y'
+    Yvec_ = R_["y"];			// cache the y vector
+    plot();
+}
+
+void DensityApp::reportSpinner() {
+    bw_ = spin_->value();		// get the value of the spin selector
+    plot();
+}
+
+void DensityApp::plot() {
+    const char *kernelstr[] = { "gaussian", "epanechnikov", "rectangular", "triangular", "cosine" };
+    greeting_->setText("Starting R call");
+    R_["tfile"]  = tempfile_;
+    R_["bw"]     = bw_;
+    R_["kernel"] = kernelstr[kernel_]; 			// passes the string to R
+    R_["y"]      = Yvec_;
+    std::string cmd0 = "png(filename=tfile,width=600,height=400); plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
+    std::string cmd1 = "\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4));  dev.off()";
+    std::string cmd = cmd0 + kernelstr[kernel_] + cmd1; // stick the selected kernel in the middle
+    R_.parseEvalQ(cmd);				     	// evaluate command -- generates new density plot
+    imgfile_->setChanged();				// important: tells consumer that image has changed, forces refresh
+    greeting_->setText("Finished request from " + this->environment().clientAddress() + " using " + this->environment().userAgent()) ;
+}
+
+WApplication *createApplication(const WEnvironment& env) {
+    // You could read information from the environment to decide whether
+    // the user has permission to start a new application
+    //
+    // We grab an instance of the embedded R. Note we can start only one,
+    // so resource conflicts have to be managed (eg add mutexes etc)
+    //
+    return new DensityApp(env, RInside::instance());
+}
+
+int main(int argc, char **argv) {
+
+    RInside R(argc, argv);              // create the one embedded R instance 
+
+    // Your main method may set up some shared resources, but should then
+    // start the server application (FastCGI or httpd) that starts listening
+    // for requests, and handles all of the application life cycles.
+    //
+    // The last argument to WRun specifies the function that will instantiate
+    // new application objects. That function is executed when a new user surfs
+    // to the Wt application, and after the library has negotiated browser
+    // support. The function should return a newly instantiated application
+    // object.
+    return WRun(argc, argv, createApplication);
+}



More information about the Rinside-commits mailing list