Re: [R] escape characters in shell commands

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue, 24 May 2011 17:53:48 -0400

On Tue, May 24, 2011 at 1:40 PM, James Rome <jamesrome_at_gmail.com> wrote:
> On a Windows platform I am trying to count the number of lines in a file.
> In a DOS window, the following works:
> C:\Users\jar>findstr /R /N "^" D:\my_dir\my_file | find /C ":"
> 5317
> (it works with double \\ also)
>
> But in R, I need to make this string up with the file name I get from
> file.choose():
> filename = file.choose()
> #get the number of lines in the file
> # first make a command string with the filename in it
> cmd = paste('findstr /R /N "^" ', filename, ' | find /C ":"', sep=""')
> nrec = as.numeric(shell(cmd))
>
> But R puts in escape characters for the ":
>> cmd
> [1] "findstr /R /N \"^\" D:\\my_dir\\my_file | find /C \":\""
>
> and shell(cmd) does not work properly. And
>> nrec=shell('findstr /R /N "^"D:\\my_dir\\my_file | find /C ":"')
>> nrec
> [1] 0
> does not work either.

Just a guess but note that Rtools has a find.exe so if you have Rtools on your path before C:\Windows\System32 then the above won't work. You can check it via:

shell("where find.exe")

If that is the problem then workarounds include using the full path to find, using wc (which is included in Rtools) or doing it entirely in R.

-- 
Statistics & Software Consulting
GKX Group, GKX Associates Inc.
tel: 1-877-GKX-GROUP
email: ggrothendieck at gmail.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.
Received on Tue 24 May 2011 - 21:56:39 GMT

This quarter's messages: by month, or sorted: [ by date ] [ by thread ] [ by subject ] [ by author ]

All messages

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.2.0, at Tue 24 May 2011 - 22:50:09 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.

list of date sections of archive