2011年3月22日星期二

invoke unix command in R and using paste() function

(1) run Unix commands which has quotations ("") in it,
# in unix you can run the code here
awk '{print $1 "\t" $2 "\t" $3}' Bak-2.vcf | head -n3

# in R, you need to run like
system("awk '{print $1 \"\t\" $2 \"\t\" $3}' Bak-2.vcf | head -n3")

# here you need to use symbol (\) which can conserve quotations (") doing like the normal quotations

(2) paste()
# a question: how to generate a unix command like "awk '{print $1 "\t" $2 "\t" $3}' Bak-2.vcf | head -n3" with paste()?

# answer is
paste("awk '{print $1 \"\t\" $2 \"\t\" $3}' Bak-2.vcf | head -n3", sep ="")
# it should work, though it looks not exactly same with the command you use in unix terminal. There is " \" in the output of paste().

"awk '{print $1 \"\t\" $2 \"\t\" $3}' Bak-2.vcf | head -n3"

# it just can be used in system() function to invoke unix.

没有评论:

发表评论