[英]Running a Python script in Jupyter Notebook, with arguments passing
[英]Passing arguments to a R script from a JuPyter notebook on a Mac and vice versa?
我有一個 JuPyter 筆記本(在 Python 中),並希望將 arguments 列表傳遞給 R 程序(單獨存儲在同一目錄中)。 R 程序執行后,會將結果傳回 JuPyter notebook。
我在 macOS 上。
所以我的問題如下:
command = "/Library/Frameworks/R.framework/Resources/bin/R"
script = "/Users/me/Desktop/rProgram.R"
args = [1,2,3] # some arguments to pass to R
cmd = [command, script] + args
p = subprocess.Popen(cmd, stdout = subprocess.PIPE, stderr = subprocess.PIPE)
results = p.communicate()
程序似乎沒有運行。 如果我打印results
,我會得到:
ARGUMENT '/Users/me/Desktop/rProgram.R' __ignored__\n\n", b"Fatal error: you must specify '--save', '--no-save' or '--vanilla'\n")
發現這是我上面的目錄中的一些錯誤。 在 Mac 上,Rscript 路徑以/Library/Frameworks/R.framework/Versions/Current/Resources/Rscript
的形式給出。 看起來我在上面的原始代碼中的路徑不會運行 R 的實例。
command = "/Library/Frameworks/R.framework/Versions/Current/Resources/Rscript"
script = "/Users/me/Desktop/rProgram.R"
args = [1,2,3] # some arguments to pass to R
cmd = [command, script] + args
x = subprocess.check_output(cmd, universal_newlines = True, stderr = subprocess.STDOUT)
x
是從 rProgram.R 腳本傳回的必需參數。
# Retrieve arguments from Python
args <- commandArgs(trailingOnly = TRUE)
# Perform some functions
some.value <- some.function(args)
# Passing the value back to Python
cat(some.value)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.