![](/img/trans.png)
[英]How import python script that requires command line arguments before importing?
[英]How to call a python script that requires command line arguments (that are not static) from Rstudio?
I am trying to create a Shiny app where a user will choose a short string from a drop-down menu, that string will then be passed to a python script which will output some values which will be used in the shiny app.
使用 reticulate 的py_run_file
function 和硬编码所需的值效果很好。 但是,使用这个:
py_run_file('test_script.py arg1')
给出了这个:
py_run_file_impl(file, local, convert)
中的错误:
无法打开文件“test_script.py arg1”(它存在吗?)
几个线程建议使用system()
调用来运行带有命令行 arguments 的.py
脚本,但我认为这对于目标来说是不可行的,因为参数需要能够改变。 其他线程建议创建一个 python 文件,该文件使用os.system()
和 arguments 调用原始 python 文件,但这也不适用于我的情况。
有没有人有任何想法?
谢谢
如果其他人正在努力解决这个问题:我找到了一种解决方法。
我没有向 python 脚本提供参数,而是创建一个 R 全局环境变量,然后在 python 脚本中调用它。
Had no idea you could reference R environment variables by calls such as r.RVar
in the python script, similar to py$PythonVar
when calling python variables in R scripts.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.