実行のためのパラメータ類についてはrJavaを用いてJavaからRを動かしたPostと同様にR_HOME等を設定する.
(ファイルにPNG画像を書き出して読み込んで描画するを繰り返すので注意)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rJava; | |
import java.awt.Graphics; | |
import java.awt.event.WindowAdapter; | |
import java.awt.event.WindowEvent; | |
import java.awt.image.BufferedImage; | |
import java.io.File; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
import javax.swing.JPanel; | |
import org.rosuda.JRI.REXP; | |
import org.rosuda.JRI.Rengine; | |
abstract class RBase extends JPanel implements Runnable { | |
protected Rengine _engine; | |
protected Thread _thread; | |
protected boolean _loopFlag = true; | |
private boolean _lockFlag = false; | |
private File _file; | |
public RBase() { | |
_engine = new Rengine(new String[] { | |
"--no-save" | |
}, false, null); | |
init(); | |
_file = paintIMP(); | |
} | |
abstract void init(); | |
abstract void plot(); | |
abstract void step(); | |
protected REXP eval(String cmd) { | |
return _engine.eval(cmd); | |
} | |
@Override | |
public void paintComponent(Graphics g) { | |
super.paintComponent(g); | |
if (_file != null) { | |
try { | |
BufferedImage img = ImageIO.read(_file); | |
g.drawImage(img, 0, 0, this); | |
} catch (Exception e) { | |
e.printStackTrace(); | |
_file.delete(); | |
_file = null; | |
} | |
} | |
_lockFlag = false; | |
} | |
private File paintIMP() { | |
File file = null; | |
try { | |
file = File.createTempFile("plot", ".png"); | |
file.deleteOnExit(); | |
_engine.eval("png('" + file.getAbsolutePath() + "')"); | |
plot(); | |
_engine.eval("dev.off()"); | |
return file; | |
} catch (Exception e) { | |
if (file != null) { | |
file.delete(); | |
file = null; | |
} | |
return null; | |
} | |
} | |
protected void toggle() { | |
if (!(_thread != null && _thread.isAlive())) | |
start(); | |
else | |
_loopFlag = false; | |
} | |
protected void start() { | |
if (_thread == null || !_thread.isAlive()) { | |
_thread = new Thread(this); | |
_loopFlag = true; | |
_thread.start(); | |
} | |
} | |
protected void stop() { | |
_loopFlag = true; | |
} | |
protected void reset() { | |
_loopFlag = true; | |
init(); | |
_file = paintIMP(); | |
repaint(); | |
} | |
protected void quit() { | |
stop(); | |
if (_engine != null && _engine.isAlive()) | |
_engine.end(); | |
} | |
@Override | |
public void run() { | |
try { | |
while (_loopFlag) { | |
_lockFlag = true; | |
repaint(); | |
do { | |
Thread.sleep(10); | |
} while (_lockFlag); | |
step(); | |
_file = paintIMP(); | |
} | |
} catch (Exception e) { | |
e.printStackTrace(); | |
_engine.end(); | |
} | |
} | |
public static void show(RBase p) { | |
System.out.println("R_HOME:" + System.getenv("R_HOME")); | |
System.out.println("java.library.path:" | |
+ System.getProperty("java.library.path")); | |
JFrame f = new JFrame(); | |
f.getRootPane().setContentPane(p); | |
f.addWindowListener(new WindowAdapter() { | |
public void windowClosing(WindowEvent e) { | |
p.quit(); | |
System.exit(0); | |
} | |
}); | |
f.setSize(480, 500); | |
f.setVisible(true); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package rJava; | |
import java.awt.event.KeyEvent; | |
import java.awt.event.KeyListener; | |
import org.rosuda.JRI.REXP; | |
public class RSample extends RBase implements KeyListener { | |
static int L = 30; | |
static int L2 = L * L; | |
static int BW = 5; | |
private int[] _boxes; | |
public static void main(String[] args) { | |
show(new RSample()); | |
} | |
void init() { | |
_boxes = new int[L2]; | |
int n = 80 * 30 * 30; | |
for (int i = 0; i < L2; i++) { | |
_boxes[i] = 0; | |
} | |
for (int i = 0; i < n; i++) { | |
_boxes[random(L2)]++; | |
} | |
_engine.assign("d", _boxes); | |
eval("d0<-d"); | |
} | |
void plot() { | |
_engine.assign("d", _boxes); | |
eval("dmax<-max(d)"); | |
eval("dmin<-min(d)"); | |
eval(String.format("breaks<-seq(0,dmax+%d,by=%d)", BW, BW)); | |
String args = ""; | |
args += String.format(", xlim=c(0,dmax+%d)", BW); | |
args += ", breaks=breaks"; | |
args += ", freq=TRUE"; | |
args += String.format(", xlab='コマ内の点の数(階級[巾%d])'", BW); | |
args += ", ylab='コマの数(階級に属するコマの数)'"; | |
args += ", family='Osaka-Mono'"; | |
args += ", main=''"; | |
eval(String.format("hist(d, col='#22222240', %s)", args)); | |
eval("hist(d0, add=TRUE, ann=F, col='#CCCCCC40', breaks=breaks)"); | |
eval("grid()"); | |
eval("rug(d)"); | |
eval(String.format("x<-breaks[1:length(breaks)-1]+%d/2", BW)); | |
eval("y<-table(cut(d, breaks=breaks))"); | |
curve_fitting(); | |
} | |
void step() { | |
int n = 100000; | |
for (int i = 0; i < n; i++) { | |
if (!stepIMP()) { | |
i--; | |
continue; | |
} | |
} | |
} | |
// ------------------------------------------------ | |
protected boolean stepIMP() { | |
int p = random(L2); | |
int q = random(L2); | |
if (_boxes[p] == 0 || p == q) { | |
return false; | |
} | |
_boxes[p]--; | |
_boxes[q]++; | |
return true; | |
} | |
private void curve_fitting() { | |
String f = "a*b^x"; | |
String c = "solid"; | |
REXP r = eval("result<-nls(y~(" + f + "), start=c(a=1, b=1))"); | |
if (r == null) | |
return; | |
eval("p<-result$m$getPars()"); | |
eval("a= p[1]"); | |
eval("b= p[2]"); | |
eval("func= function(x) " + f); | |
eval("plot(func, 0, dmax, add=TRUE, lty='" + c + "')"); | |
String legend = "legend('topright'"; | |
legend += String.format(", c(expression(%s))", f); | |
legend += String.format(", lty=c('%s')", c); | |
// legend += ", pch=c(-1)"; | |
legend += ", bty='n')"; | |
eval(legend); | |
} | |
// ------------------------------------------------ | |
public RSample() { | |
super(); | |
setFocusable(true); // KeyListnerに必要な設定 | |
addKeyListener(this); | |
} | |
@Override | |
public void keyTyped(KeyEvent e) { | |
} | |
@Override | |
public void keyPressed(KeyEvent e) { | |
} | |
@Override | |
public void keyReleased(KeyEvent e) { | |
if (e.getKeyCode() == KeyEvent.VK_SPACE) { | |
toggle(); | |
} else if (e.getKeyCode() == KeyEvent.VK_R) { | |
reset(); | |
} else if (e.getKeyCode() == KeyEvent.VK_Q) { | |
quit(); | |
} | |
} | |
// ------------------------------------------------ | |
private static int random(final int n) { | |
return (int) Math.floor(Math.random() * n); | |
} | |
} |
0 件のコメント:
コメントを投稿