RApacheにおけるマルチバイト文字の利用について

RApacheでのマルチバイト文字(例えばUTF-8)を扱うには

Sys.setlocale(category='LC_ALL','ja_JP.UTF-8')

を実行しておく必要がある。これをやっておかないと、マルチバイト文字を含むファイルの読み込みや、iconvによる文字コード変換が正しく動作しない。

※ 参考 "UTF-8 characters in RApache"
https://groups.google.com/forum/?fromgroups=#!topic/rapache/V3IAIRhAybw

ある要素の子要素全てにアクセスする方法(SVG)

SVGでいくつかの要素をgタグでグループ化しているとき、getElementById()でgタグの要素を取得して、その子要素全てにアクセスする例。

childNodesで子ノードが取得できる。ノードにはテキストノード(text要素ではない)が含まれるので、nodeNameでノードの種類をチェックした上で処理を行う。

<?xml version="1.0" encoding="utf-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
	width="800" height="800"
     version="1.0" onload="init()">
<script type="text/javascript">
<![CDATA[
function init(){
	var group1 = document.getElementById("group1");
	var children = group1.childNodes;
	for(i=0;i<children.length;i++){
		if(children[i].nodeName=='circle'){
			children[i].style.setProperty('fill', '#00ff00');
		}
	}
}
]]>
</script>
<g id="group1">
	<circle cx="100" cy="100" r="50" style="fill: #ff0000" />
	<circle cx="200" cy="100" r="50" style="fill: #ff0000" />
	<circle cx="300" cy="100" r="50" style="fill: #ff0000" />
	<circle cx="400" cy="100" r="50" style="fill: #ff0000" />
</g>
<g id="group2">
	<circle cx="100" cy="200" r="50" style="fill: #ff0000" />
	<circle cx="200" cy="200" r="50" style="fill: #ff0000" />
	<circle cx="300" cy="200" r="50" style="fill: #ff0000" />
	<circle cx="400" cy="200" r="50" style="fill: #ff0000" />
</g>
</svg>

SVGの要素をHTMLのフォームパーツで操作する

objectタグで適当なidをつけてSVGを埋め込んで、

document.getElementById('hogehoge').contentDocument;

で、SVGDocumentを取得。あとは、SVGに対するJavaScriptを記述すればよい。

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>traffic light</title>
<script type="text/javascript">
var now = 'blue';
var svg;
function init(){
	svg = document.getElementById("testsvg").contentDocument;
	switch_on(now);
}

function switch_on(col){
	var circle = svg.getElementById(now);
	circle.style.setProperty('fill', '#999999');
	circle = svg.getElementById(col);
	circle.style.setProperty('fill', col);
	now = col;
	
}
</script>
</head>
<body onload="init()">
<h1>traffic light</h1>
<object id="testsvg" data="test.svg" type="image/svg+xml" width="600px" height="200px"> </object>
<p>
<input type="radio" name="switch" value="blue" onclick="switch_on(this.value);" checked>Blue
<input type="radio" name="switch" value="yellow" onclick="switch_on(this.value);">Yellow
<input type="radio" name="switch" value="red" onclick="switch_on(this.value);">Red
</p>
</body>
</html>

SVGのコードは以下のとおり。

<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg"
     xmlns:xlink="http://www.w3.org/1999/xlink"
     width="600px"
     height="200px"
     version="1.0">
<circle id="blue" cx="100" cy="100" r="100" style="fill:#999999" />
<circle id="yellow" cx="300" cy="100" r="100" style="fill:#999999" />
<circle id="red" cx="500" cy="100" r="100" style="fill:#999999" />
</svg>

MacOSXでiplotsを使う

iplotsはJGR(Java GUI for R)の中で動作するので、JGRのインストールも必要。

install.packages("iplots")
install.packages("JGR")

さらに、CarbonELパッケージも必要。これはinstall.packagesでインストールできないので、CarbonELのダウンロードページからCarbonEL_0.1-4.tgzをダウンロードして解凍、できたフォルダをライブラリのフォルダ(/Library/Frameworks/R.framework/Versions/2.15/Resources/library など)にコピーする。次に、

Sys.setenv(NOAWT=1)
library(JGR)
JGR()

これでJGRのコンソールが起動して、iplotsが利用できるようになる。Sys.setenvを実行しないとエラーとなる。