代码之家  ›  专栏  ›  技术社区  ›  colin

让R打印包含“,”和转义字符的系统调用\

  •  0
  • colin  · 技术社区  · 6 年前

    system(paste0('my command'))
    

    但是,我要粘贴的命令包含单引号和双引号以及转义字符。具体来说,我想粘贴这个命令:

    perl -pe '/^>/ ? print "\n" : chomp' in.fasta | tail -n +2 > out.fasta
    

    system() 功能?

    1 回复  |  直到 6 年前
        1
  •  2
  •   Suhas Hegde    6 年前

    嘿,我还没测试过你的 perl this 还有更多的问题。 我的方法,

    # shouldnt have any text expect for an empty string
    my_text <- try(system(" perl -e 'print \"\n\"' ", intern = TRUE))
    my_text
    
    [1] ""
    
    
    # should contain the string - Hello perl from R!
    my_text2 <- try(system(" perl -e 'print \"Hello perl from R!\"' ", intern = TRUE))
    my_text2
    
    [1] "Hello perl from R!"
    

    所以根据以上的试验,我认为这对你有用-

    try(system(command = "perl -pe '/^>/ ? print \"\n\" : chomp' in.fasta | tail -n +2 > out.fasta", intern = TRUE))
    

    intern = TRUE 只需将输出捕获为R中的字符向量。