【问题标题】:store output of system command into a variable in r将系统命令的输出存储到 r 中的变量中
【发布时间】:2012-06-21 08:00:21
【问题描述】:

我正在 R 中执行以下命令:

system("ls ")

我需要将上述命令的输出存储在某个 R 变量中。 有没有办法做同样的事情??

【问题讨论】:

    标签: r variables system


    【解决方案1】:

    使用intern=TRUE:

    a <- system("ls ", intern = TRUE)
    

    【讨论】:

    • 如果对程序进行系统调用,有没有办法存储信息?
    • 有什么方法可以同时获取os程序状态码和程序输出?
    • 如果我要使用返回 100 万行的 SQL 命令怎么办?如何先将输出重定向到文件?
    【解决方案2】:

    为什么不用对应的R函数呢?

    a <- list.files()
    b <- list.files(recursive = TRUE)
    

    更多详情

    ?list.files
    

    【讨论】:

    • 因为list.files 也将目录列为项目,如果您使用recursive=T,它会潜入这些目录。没有简单的 R 方法可以列出目录中的文件。我用file_list = system('ls -p &lt;target dir&gt; | grep -v /')
    猜你喜欢
    • 2011-08-20
    • 2010-12-29
    • 2012-04-03
    • 1970-01-01
    • 2011-01-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-08-10
    相关资源
    最近更新 更多