代码之家  ›  专栏  ›  技术社区  ›  tommy chheng

如何禁止使用r cmd批处理输出行号?

  •  12
  • tommy chheng  · 技术社区  · 14 年前

    如果我有一个R脚本:

    print("hi")
    commandArgs()
    

    我用以下方法运行它:

    r CMD BATCH --slave --no-timing test.r output.txt
    

    输出将包含:

    [1] "hi"
    [1] "/Library/Frameworks/R.framework/Resources/bin/exec/x86_64/R"
    [2] "-f"                                                         
    [3] "test.r"                                                     
    [4] "--restore"                                                  
    [5] "--save"                                                     
    [6] "--no-readline"                                              
    [7] "--slave"                                                    
    

    如何抑制输出中的行号[1]。[7],以便只显示脚本的输出?

    3 回复  |  直到 11 年前
        1
  •  5
  •   Dirk is no longer here    14 年前

    是的,MBQ是正确的——使用 Rscript 或者,如果它漂浮在你的船上, littler :

    $ cat /tmp/tommy.r 
    #!/usr/bin/r
    
    cat("hello world\n")
    print(argv[])
    $ /tmp/tommy.r a b c
    hello world
    [1] "a" "b" "c"
    $
    

    你可能想看看克兰的包裹 getopt optparse 像在其他脚本语言中那样进行参数解析/

        2
  •  13
  •   David J. user3890638    13 年前

    使用 cat 而不是 print 如果要取消行号( [1] , [2] ,…)在输出中。

    我认为您还需要传递命令行参数。我认为最简单的方法是用rscript创建一个文件 shebang :

    例如,创建一个名为 args.r :

    #!/usr/bin/env Rscript
    args <- commandArgs(TRUE)
    cat(args, sep = "\n")
    

    使其可执行 chmod +x args.r 然后你可以用 ./args.r ARG1 ARG2

    fwiw,传递命令行参数 R CMD BATCH ... 语法是一种痛苦。您可以这样做: R CMD BATCH "--args ARG1 ARG2" args.r 注意引号。更多讨论 here

    更新:更改了上方的shebang行 #!/usr/bin/Rscript #!/usr/bin/env Rscript 回应@mbq的评论(谢谢!)

        3
  •  2
  •   mbq    13 年前

    使用 commandArgs(TRUE) 运行脚本时 Rscript .

    编辑:好吧,我误解了你的问题。大卫说得对。