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

William Dunlap wdunlap at tibco.com
Sat Oct 27 22:17:24 CEST 2012


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


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of siddu479
> Sent: Saturday, October 27, 2012 4:42 AM
> To: r-help at r-project.org
> Subject: [R] Getting error while running unix commands within R using system() function
> 
> Hello All,
> 
>    I use Cygwin ( unix on windows) heavily for all my text data processing.
> Also use Cygwin inbuilt *R* to do numerical processing.
> *My aim is to integrate R and unix commands to avoid heavy memory usage that
> R takes normally.*
> 
> I can run many unix commands using system("some unix command or sh script.sh
> or even R file itself ") inside R.
> 
> But, When I tried to run the command *awk '{print "Hello"$1}' infile.txt* to
> prefix "Hello" to my first column of file infile.txt, like below in *R *
> *system(" awk '{print "Hello"$1}' infile.txt") *, I am getting the below
> error,
> *Error: unexpected symbol in "system("awk '{print "Hello"*
> I tried even like *system('awk '{print "Hello"$1}' infile.txt')*, but still
> the same error.
> 
> This particular error is hampering my entire work, other wise I found this
> is excellent way processing the text files at the same time reading back and
> forth file outputs from R to unix and vice versa.
> 
> Can some one give me provide some solution to this problem ??
> 
> 
> 
> 
> 
> 
> 
> 
> -----
> Sidda
> Business Analyst Lead
> Applied Materials Inc.
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Getting-error-while-
> running-unix-commands-within-R-using-system-function-tp4647656.html
> Sent from the R help mailing list archive at Nabble.com.
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list