Skip to content
Prev 309194 / 398506 Next

Getting error while running unix commands within R using system() function

To diagnose a problem in R it pays to pull apart your expression
into a sequence of simpler ones.  E.g., replace your expression
  system(" awk '{print "Hello"$1}' infile.txt")
with the sequence
  sysCmd <- " awk '{print "Hello"$1}' infile.txt"
  system(sysCmd)
and you will see an error when running the first one, showing that
this has nothing to do with the system() function:
  > sysCmd <- " awk '{print "Hello"$1}' infile.txt"
  Error: unexpected symbol in "sysCmd <- " awk '{print "Hello"
Note where the error message ends, at a double quote, suggesting
that quoting may be the problem.

To put a double quote inside a double-quoted string you must
put a backslash in front of it
  > sysCmd <- " awk '{print \"Hello\"$1}' infile.txt"

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com