Skip to content

Instantly share code, notes, and snippets.

@kohiro37
Last active February 11, 2019 12:33
Show Gist options
  • Save kohiro37/fb030f0046d41e42018a561cb61dff71 to your computer and use it in GitHub Desktop.
Save kohiro37/fb030f0046d41e42018a561cb61dff71 to your computer and use it in GitHub Desktop.
Example of run a R script in Python3
import os
import sys
import subprocess
import textwrap
import random
def create_rscript(rscript_path):
"""Rスクリプトファイルの作成
"""
r_script = textwrap.dedent("""
# コマンドラインの引数取得
args1 <- commandArgs(trailingOnly = TRUE)[0:10]
args2 <- commandArgs(trailingOnly = TRUE)[11:20]
g1 <- as.numeric(args1)
g2 <- as.numeric(args2)
cat('g1=\n')
g1
summary(g1)
cat('g2=\n')
g2
summary(g2)
# t検定の結果出力
t.test(g1, g2, var.equal=TRUE)
""").strip()
with open(rscript_path,'w') as f:
f.write(r_script)
def main():
# Rスクリプトファイル名
rscript_path = 'r_script.R'
if not os.path.exists(rscript_path):
create_rscript(rscript_path)
else:
print('{}と同名ファイルが存在します。'.format(rscript_path))
sys.exit()
# 平均が異なる正規分布に従う値を10ずつ生成
# 値は文字列に変換しておく
g1 = [str(random.normalvariate(0, 1)) for i in range(10)]
g2 = [str(random.normalvariate(2, 1)) for i in range(10)]
# --vanilla はRscript実行時に余計なオブジェクト等を読み込まないためのオプション
cmd = ['Rscript', '--vanilla', rscript_path] + g1 + g2
try:
# check_outputでRスクリプトの実行結果を取得
# 結果はバイト列だが、universal_newlines=Trueとするとエンコードされる
res = subprocess.check_output(cmd, stderr=subprocess.STDOUT, universal_newlines=True)
except subprocess.CalledProcessError as e:
print(e.output)
else:
print(res)
os.remove(rscript_path)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment