14 m_cmd =
"c(rnorm(100,0,1), rnorm(50,5,1))";
22 QWidget *window =
new QWidget;
23 window->setWindowTitle(
"Qt and RInside demo: density estimation");
25 QSpinBox *spinBox =
new QSpinBox;
26 QSlider *slider =
new QSlider(Qt::Horizontal);
27 spinBox->setRange(5, 200);
28 slider->setRange(5, 200);
29 QObject::connect(spinBox, SIGNAL(valueChanged(
int)), slider, SLOT(setValue(
int)));
30 QObject::connect(slider, SIGNAL(valueChanged(
int)), spinBox, SLOT(setValue(
int)));
31 spinBox->setValue(
m_bw);
32 QObject::connect(spinBox, SIGNAL(valueChanged(
int)),
this, SLOT(
getBandwidth(
int)));
34 QLabel *cmdLabel =
new QLabel(
"R command for random data creation");
35 QLineEdit *cmdEntry =
new QLineEdit(
m_cmd);
36 QObject::connect(cmdEntry, SIGNAL(textEdited(QString)),
this, SLOT(
getRandomDataCmd(QString)));
37 QObject::connect(cmdEntry, SIGNAL(editingFinished()),
this, SLOT(
runRandomDataCmd()));
39 QGroupBox *kernelRadioBox =
new QGroupBox(
"Density Estimation kernel");
40 QRadioButton *radio1 =
new QRadioButton(
"&Gaussian");
41 QRadioButton *radio2 =
new QRadioButton(
"&Epanechnikov");
42 QRadioButton *radio3 =
new QRadioButton(
"&Rectangular");
43 QRadioButton *radio4 =
new QRadioButton(
"&Triangular");
44 QRadioButton *radio5 =
new QRadioButton(
"&Cosine");
45 radio1->setChecked(
true);
46 QVBoxLayout *vbox =
new QVBoxLayout;
47 vbox->addWidget(radio1);
48 vbox->addWidget(radio2);
49 vbox->addWidget(radio3);
50 vbox->addWidget(radio4);
51 vbox->addWidget(radio5);
52 kernelRadioBox->setMinimumSize(260,140);
53 kernelRadioBox->setMaximumSize(260,140);
54 kernelRadioBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
55 kernelRadioBox->setLayout(vbox);
57 QButtonGroup *kernelGroup =
new QButtonGroup;
58 kernelGroup->addButton(radio1, 0);
59 kernelGroup->addButton(radio2, 1);
60 kernelGroup->addButton(radio3, 2);
61 kernelGroup->addButton(radio4, 3);
62 kernelGroup->addButton(radio5, 4);
63 QObject::connect(kernelGroup, SIGNAL(buttonClicked(
int)),
this, SLOT(
getKernel(
int)));
65 m_svg =
new QSvgWidget();
68 QGroupBox *estimationBox =
new QGroupBox(
"Density estimation bandwidth (scaled by 100)");
69 QHBoxLayout *spinners =
new QHBoxLayout;
70 spinners->addWidget(spinBox);
71 spinners->addWidget(slider);
72 QVBoxLayout *topright =
new QVBoxLayout;
73 topright->addLayout(spinners);
74 topright->addWidget(cmdLabel);
75 topright->addWidget(cmdEntry);
76 estimationBox->setMinimumSize(360,140);
77 estimationBox->setMaximumSize(360,140);
78 estimationBox->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
79 estimationBox->setLayout(topright);
80 QHBoxLayout *upperlayout =
new QHBoxLayout;
81 upperlayout->addWidget(kernelRadioBox);
82 upperlayout->addWidget(estimationBox);
84 QHBoxLayout *lowerlayout =
new QHBoxLayout;
85 lowerlayout->addWidget(
m_svg);
87 QVBoxLayout *outer =
new QVBoxLayout;
88 outer->addLayout(upperlayout);
89 outer->addLayout(lowerlayout);
90 window->setLayout(outer);
95 const char *kernelstrings[] = {
"gaussian",
"epanechnikov",
"rectangular",
"triangular",
"cosine" };
98 std::string cmd0 =
"svg(width=6,height=6,pointsize=10,filename=tfile); ";
99 std::string cmd1 =
"plot(density(y, bw=bw/100, kernel=kernel), xlim=range(y)+c(-2,2), main=\"Kernel: ";
100 std::string cmd2 =
"\"); points(y, rep(0, length(y)), pch=16, col=rgb(0,0,0,1/4)); dev.off()";
101 std::string cmd = cmd0 + cmd1 + kernelstrings[
m_kernel] + cmd2;
126 std::string cmd =
"y2 <- " +
m_cmd.toStdString() +
"; y <- y2";
135 infile.open(QFile::ReadOnly);
137 outfile.open(QFile::WriteOnly | QFile::Truncate);
139 QTextStream in(&infile);
140 QTextStream out(&outfile);
141 QRegExp rx1(
"<symbol");
142 QRegExp rx2(
"</symbol");
143 while (!in.atEnd()) {
144 QString line = in.readLine();
145 line.replace(rx1,
"<g");
146 line.replace(rx2,
"</g");
void getKernel(int kernel)
void getRandomDataCmd(QString txt)
void parseEvalQ(const std::string &line)
void runRandomDataCmd(void)
int parseEval(const std::string &line, SEXP &ans)
void parseEvalQNT(const std::string &line)
void getBandwidth(int bw)