【问题标题】:responding yes to terminal prompt via system2() in R通过 R 中的 system2() 对终端提示响应是
【发布时间】:2020-03-30 02:09:38
【问题描述】:

tl;dr:如何从 R 控制台调用系统命令 y | conda create --name gee_interface,例如通过system2()?我对system2('conda', c('create', '--name', 'gee_interface')) 很满意,但我不知道如何通过system2() 处理'y' 中的管道。

详情 我正在尝试使用 R 控制台运行 bash 命令conda create --name gee_interface(安装了 Anaconda 的 OSX Mojave)。

在终端中,该命令执行得很好,但提示我回答 Proceed ([y]/n)?(我回答“y”,一切正常)。

在 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

当运行第二行时 [i.e. system2('conda', c('create', '--name', 'gee_interface'))],该过程永远不会完成,但会迅速降至零 CPU 使用率。大概系统正在等待我对提示的响应,但我不知道如何提供它。如何通过 R 脚本做到这一点?另请注意,在我的特定情况下,我需要响应“y”的次数是可变的,具体取决于名称为 gee_interface 的环境是否已经存在。

【问题讨论】:

    标签: r


    【解决方案1】:

    解决第一个问题的方法是告诉conda 不要使用-y 要求确认:

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

    至于第二部分(需要您输入的可变时间),我猜它是覆盖环境(如果存在)?在这种情况下,您可以先使用conda info --envs 检查它是否存在,如有必要,在创建它之前运行conda remove --name gee_interface --all。

    见:

    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


    您也可以尝试使用input = "y" 参数调用system2,但这并不能解决您需要多次确认的第二个问题。

    见:Invoke a system command and pipe a variable as an argument

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-02
      • 2019-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-20
      • 2017-10-17
      • 2021-12-14
      相关资源
      最近更新 更多