Top / AppleScript

R Scripts

●ドロップしたプログラムを実行

on open droplist
    set theFile to first item of droplist
    -- ドロップしたファイルがRのプログラムかどうかとかチェックはしていない
    -- 複数のファイルをドロップされても、その内の一つだけを選択する
    set rcommand to "source(\"" & (theFile as string) & "\")"
    -- Mac OS Xの場合は上の文を以下のようにする (「POSIX path of」を付け加える)
    -- set rcommand to "source(\"" & (POSIX path of theFile as string) & "\")"
    -- AppleScriptでは"をダブルクォート間に入れたい場合は\"と記述する
    tell application "R"
        activate
        cmd rcommand
    end tell
end open

●ドロップしたデータファイルを処理

以下のスクリプトは下のようなデータファイルがドロップされたことを想定しています。Rのコマンドの部分を日常的に処理するデータファイルに合わせて調整すれば、データファイルをドロップするだけなので楽になるでしょう。

mass
1.6
1.8
1.7
2.0
1.9

on run -- スクリプトをダブルクリックされたら、エラーメッセージを表示
    display dialog "これはドロップレットです。" buttons "OK" default button 1 -- 単なるエラー処理なので
end run -- 上のスクリプトのように、この部分は省略してもいい
on open droplist
    set theFile to first item of droplist
    tell application "Finder"
        set name_file to name of theFile
        set path_folder to (folder of theFile) as string
    end tell
    -- 以下でデータをxという名前で自動的にセットしているが
    -- display dialog "このデータの名前を入力してください" default answer name_file buttons {"OK"} default button 1
    -- set name_data to text returned of result
    -- としてRのコマンド部分のxを「" & name_data & "」で置き換えれば、どういう名前にするか尋ねるようにできるね
    tell application "R"
        activate
        cmd "setwd(\"" & path_folder & "\")"
        cmd "x <- scan(file=\"" & name_file & "\", skip=1)"
        cmd "cat(sprintf(\"Mean: %f\\n\", mean(x))); cat(sprintf(\"Unbiased Variance: %f\\n\", var(x))); cat(sprintf(\"Variance: %f\\n\", (length(x)-1)*var(x)/length(x))); cat(sprintf(\"Unbiased Standard Deviation: %f\\n\", sd(x))); cat(sprintf(\"Standard Deviation: %f\\n\", sqrt((length(x)-1)/length(x))*sd(x))); cat(sprintf(\"Minimum: %f\\n\", min(x))); cat(sprintf(\"Maximum: %f\\n\", max(x)))"
        -- 複数のコマンドを「;」で区切って書いてもいいけど、途中で改行して書くのはだめみたい
        -- だけど、以下のようにもちろん複数の「cmd 何々」に分けて書くのは大丈夫
        -- cmd "cat(sprintf(\"Mean: %f\\n\", mean(x)));"
        -- cmd "cat(sprintf(\"Unbiased Variance: %f\\n\", var(x))); cat(sprintf(\"Variance: %f\\n\", (length(x)-1)*var(x)/length(x)));"
        -- cmd "cat(sprintf(\"Unbiased Standard Deviation: %f\\n\", sd(x))); cat(sprintf(\"Standard Deviation: %f\\n\", sqrt((length(x)-1)/length(x))*sd(x)));"
        -- cmd "cat(sprintf(\"Minimum: %f\\n\", min(x))); cat(sprintf(\"Maximum: %f\\n\", max(x)))"
    end tell
end open

●Excelでの選択部分のデータを処理

以下のスクリプトはExcelで矩形選択したセルの値 (数値のみ) を、Rに行列データ (matrix) として渡すことを想定しています。数値のみと言ったのは、単に下で例として示したRのコマンドのcorが数値のみの行列データを引数とするからです。ExcelのAppleScriptの部分は選択部分が数値でも文字列でも関係なく動作するので、文字列を含むデータを引数とするRのコマンドなどに対しても使えます。後はそれに合わせて加工したデータを渡すだけだと思います。ところでFormulaR1C1の代わりにValueなどとしても同様に動作します。どう書くのが文法的にふさわしいのかよく分かりません。RjpWikiの要望掲示板にぱるすきという名前で書き込みをしてみました (こういうWebページの共同作成もいいですね,参加に気が引けたりしますが、思い切って質問・回答等されるといいかと思います,人に指摘できる立場でないですが^^;)。
tell application "Microsoft Excel" to set selected_cells to FormulaR1C1 of Selection
set matrixdata to "matrix(c("
set num_col to number of first item of selected_cells
set num_row to number of every item of selected_cells
repeat with i from 1 to num_row
    repeat with j from 1 to num_col
        if ((i = num_row) and (j = num_col)) then
            set matrixdata to matrixdata & (item num_col of (item num_row of selected_cells))
        else
            set matrixdata to matrixdata & (item j of (item i of selected_cells)) & ","
        end if
    end repeat
end repeat
set matrixdata to matrixdata & "), ncol=" & num_col & ", byrow=TRUE)"
--display dialog matrixdata
tell application "R"
    activate
    cmd "cor(" & matrixdata & ")"
end tell

Top / AppleScript