简体   繁体   English

通过 R 中的 system2() 对终端提示响应是

[英]responding yes to terminal prompt via system2() in R

tl;dr : How can I invoke the system command y | conda create --name gee_interface tl;dr :如何调用系统命令y | conda create --name gee_interface y | conda create --name gee_interface from an R console, eg via system2()? y | conda create --name gee_interface从 R 控制台,例如通过 system2()? I'm comfortable enough with system2('conda', c('create', '--name', 'gee_interface')) , but I don't know how to handle piping in the 'y' via system2() .我对system2('conda', c('create', '--name', 'gee_interface')) ,但我不知道如何通过system2()处理 'y' 中的管道。

Details I am trying to use an R console to run the bash command conda create --name gee_interface (OSX Mojave with Anaconda installed).详细信息我正在尝试使用 R 控制台运行 bash 命令conda create --name gee_interface (安装了 Anaconda 的 OSX Mojave)。

In terminal, that command executes just fine, but prompts me to answer with Proceed ([y]/n)?在终端中,该命令执行得很好,但提示我用Proceed ([y]/n)?回答Proceed ([y]/n)? (I answer 'y' and everything works smoothly). (我回答'y',一切顺利)。

In R, I run在 R 中,我运行

Sys.setenv(PATH = paste(c("/Applications/anaconda3/bin", Sys.getenv("PATH")), collapse = .Platform$path.sep)) # ensures that system2() finds conda

system2('conda', c('create', '--name', 'gee_interface')) # This is the key line for the purposes of this question

When running the second line [ie system2('conda', c('create', '--name', 'gee_interface')) ], the process never finishes, but quickly falls to zero CPU usage.当运行第二行 [即system2('conda', c('create', '--name', 'gee_interface')) ] 时,该进程永远不会完成,但很快就会下降到零 CPU 使用率。 Presumably the system is waiting for my response to the prompt, but I don't know how to provide it.大概系统正在等待我对提示的响应,但我不知道如何提供它。 How does one do this via an R script?如何通过 R 脚本做到这一点? Note also that in my particular case, the number of times that I need to respond 'y' is variable, depending on whether an environment of the name gee_interface already exists or not.另请注意,在我的特定情况下,我需要响应 'y' 的次数是可变的,具体取决于名称为gee_interface的环境是否已经存在。

The fix to your first problem is to tell conda not to ask for confirmation using -y :解决第一个问题的方法是告诉conda不要使用-y要求确认:

system2('conda', c('create', '--name', 'gee_interface', '-y'))

As to the second part (variable times that your input is required), I'm guessing it's to overwrite the environment if it exists?至于第二部分(需要输入的可变时间),我猜它是覆盖环境(如果存在)? In that case, you could check for its existence first with conda info --envs , and run conda remove --name gee_interface --all if necessary before creating it.在这种情况下,您可以首先使用conda info --envs检查它的存在,然后在创建它之前运行conda remove --name gee_interface --all

See:看:

https://docs.conda.io/projects/conda/en/latest/commands/create.html https://docs.conda.io/projects/conda/en/latest/commands/create.html

https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#removing-an-environment https://docs.conda.io/projects/conda/en/latest/user-guide/tasks/manage-environments.html#removing-an-environment


You could also try your system2 call, with the argument input = "y" , but that doesn't fix your second problem of needing to affirm multiple times.您也可以尝试使用system2调用,参数为input = "y" ,但这并不能解决您需要多次确认的第二个问题。

See: Invoke a system command and pipe a variable as an argument请参阅: 调用系统命令并将变量作为参数通过管道传输

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM