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
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
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