R brush for SyntaxHighlighter

(Update, 2013-05-02 14:15 JST: This article is deprecated because I changed the template of this blog.)

I'm working on a custom brush to extend SyntaxHighlighter to support the syntax of R.
The project is on GitHub.
The brush file is SyntaxHighlighter/scripts/shBrushR.js.
Here's an example.

resultframe <- sessionframe <- temp <- bpdata[1,]
transform(resultframe, morningbedtime=c(0))
resultframe <- resultframe[0,]
for (i in 2:length(bpdata$ID)) {
  tempnext <- bpdata[i,]
  if ((abs(difftime(temp$datetime, tempnext$datetime, units="mins")) > 10) || (i == length(bpdata$ID))) {
    if (i == length(bpdata$ID)) {
      sessionframe <- merge(sessionframe, tempnext, all=T)
      sessiondatetime <- strptime(tempnext$datetime, "%Y-%m-%d %H:%M:%S")
    } else {
      sessiondatetime <- strptime(temp$datetime, "%Y-%m-%d %H:%M:%S")
    sessionmax <- mean(sessionframe$max)
    sessionmin <- mean(sessionframe$min)
    sessionbpm <- mean(sessionframe$bpm)
    if ((morningbegin <= sessiondatetime$hour) && (sessiondatetime$hour < morningend)) {
      sessionmorningbedtime <- c(0)
    } else {
      sessionmorningbedtime <- c(1)
    resultframe <- merge(resultframe, data.frame(datetime=sessiondatetime, max=sessionmax, min=sessionmin, bpm=sessionbpm, morningbedtime=sessionmorningbedtime), all=T)
    sessionframe <- temp <- tempnext
  } else {
    sessionframe <- merge(sessionframe, tempnext, all=T)
    temp <- tempnext


Default settings for bar/column chart axes in major spreadsheet applications may lead to chart junk.

Depending on data, Excel, Calc, Google Docs spreadsheet, which are the most popular spreadsheet applications, scale chart axes differently by default.

Below are examples. (

Note that the vertical axes start from 0, 75, 80 depending on the values in the charts in Excel and Calc. Google Docs' chart has its vertical axis from 80, not zero.

The problem is that the mysterious feature may lead to chart junk because the axes don't start from zero and are misleading.

I couldn't find how to fix this by changing default settings with these applications.

So I'm working on Excel macro which scans all the bar/column charts in the current book and make their axes start from zero.

You can download Excel macro book file (FixChartAxes.xlsm) and Excel addin file (FixChartAxes.xlam). (both from Google Docs)

You can see the source code of the macro at Gist.

I think the best solution is that Microsoft fixes this problem, because it is the most influential company in the spreadsheet software industry.

To Microsoft: Please make the default settings of bar/column chart axes start from zero by default.


R memo: elements' positions in vector under conditions

First, make a vector.
> x <- c(10,20,30)
> x
[1] 10 20 30
If you want to get positions of the elements in vector x which meet a condition, for example, x > 10, you can do like this:
> (1:length(x))[x > 10]
[1] 2 3
The second and third elements meet the condition.

The first part,
> (1:length(x))
[1] 1 2 3
gives you a vector which counts from 1 up to the number of the elements in x.

The last part,
> x > 10
gives you a vector which shows if each element meets the condition (greater than 10).

Then, you can get only the elements which correspond to TRUE by
> (1:length(x))[x > 10]

c.f. 13. ベクトル要素へのアクセス


MathML test

This post is for testing MathJax on Blogger.



The above examples are generated by Microsoft Mathematics.

The following is from MathML Samples.








The following is generated by formulator-mathml.

y = ax 2 + bx + c