[R] How do I paste double quotes arround a character string?
Prof Brian Ripley
ripley at stats.ox.ac.uk
Thu Jul 3 13:13:42 CEST 2008
On Thu, 3 Jul 2008, Philip James Smith wrote:
> He Ted:
>
> My command will look like:
>
> gnu.dat <- read.csv( pipe( "cut -d,
> -f9,2669,2676,2684,506,467,423,6,16,622,474,2,403,19,436,400,444,430,390,2671,437,428,2686,23,21,2246,2326,2288,2282,2272,2360,2318,2262,476,502,605,519,523,527,531,535
> /home/philipsmith/file_name.csv" ) )
>
> This code uses pipe() and the unix command "cut."
>
> What I'm trying to do is to read certain fields in a .csv file..... my
> original .csv file has THOUSANDS of columns, so I just want to read certain
> ones...The unix command "cut" uses -d to specify that the comma "," is the
> delimiter in the file, and the -f to identify the field numbers. After the
> field numbers "cut" wants the file name.
>
> Evidently, the unix command called by pipe must be within double quotes...
> and that is my problem...
That is not 'evident', not even true in your example. You passed pipe() a
length-one character vector containing a command, and the quotes were
something the parser used to recognize the element of the character
vector.
>
> In my actual problem, I've got THOUSANDS of csv files that contain some
> common variables and other variables that differ from file to file. I've
> specified the variables I want as a character vector called namz, and read
> the first line of each file using scan(). I'm then able to find out using
> match() the positions of the variables I want in each file. the code listed
> above gives only 1 of THOUSANDS of possible configurations of those desired
> variables.
>
> In order to get this task done, for each file I get the position numbers in a
> variable called "col.pos" and then create the unix command using paste:
>
>
> unix.cmd <- paste( "cut -d -f" , col.pos , " " , fn , sep = '' )
and then you can use
gnu.dat <- read.csv(pipe(unix.cmd))
Much simpler!
> Then what I want to do is to create the R command that will read the file...
> something like:
>
>
> cmd0 <- paste( "gnu.dat <- read.csv( pipe ( \"" , unix.cmd ,
> "\" ) ) " , sep='')
>
>
> Then, I'll use
>
> eval( parse (text = cmd0) )
>
> to read a file... and embed this in more code to read all of the THOUSANDS of
> files, each 1 at a time.
>
> However, in the "cmd0" variable, I've used "\"" ... this does not work... it
> yields a character string that looks like...
>
>
>> cmd0
> [1] "gnu.dat <- read.csv( pipe ( \"cut -d
> -f9,2669,2676,2684,506,467,423,6,16,622,474,2,403,19,436,400,444,430,390,2671,437,428,2686,23,21,2246,2326,2288,2282,2272,2360,2318,2262,476,502,605,519,523,527,531,535
> /home/philipsmith/Bubba/NIS/2007_claf/y2007.csv\" ) ) "
>>
>
> You'll see in that line the slash that precedes the double quote. the pipe()
> command is giving unix a unix command that has a slash in it: unix is
> expecting only a double quote and not a slash and gives and error.
>
> So, those are all of the details. I'll be quite grateful if you can tell me
> how to paste a double quote (") in front of and at the end of a string so
> that unix will recognize the string as a valide unix command.
Actually, this is a shell command line, which the shell parses and passes
to Unix *provided* the parsing is not switched off by quoting.
If you want to quote a command string, that is what shQuote() is for.
>
> Very gratefully,
> Phil Smith
> Duluth, GA
>
>
>
>
>
>
> (Ted Harding) wrote:
>> Hi Philip,
>> I think a bit more clarification may be useful yet!
>>
>> 1: How are you sending the command from R to Linux?
>> 2: What is the command intended to be (as seen by Linux)?
>> And from what source (quasi-command line; script file; ...)
>> would it be read by Linux?
>>
>> For example, on my Linux machine, I just did (in a shell
>> command line):
>>
>> ted at deb:~$ touch "New Mexico"
>> ted at deb:~$ ls -l "New Mexico"
>> -rw-r--r-- 1 ted ted 0 2008-07-03 10:58 New Mexico
>>
>> and then, in R, I did:
>>
>>
>>> system("ls -l \"New Mexico\"")
>>>
>> -rw-r--r-- 1 ted ted 0 2008-07-03 10:58 New Mexico
>>
>> so I don't seem to have had the problem you describe below.
>>
>> On the other hand, back in Linux, if I do:
>>
>> ted at deb:~$ ls -l \"New Mexico\"
>> ls: "New: No such file or directory
>> ls: Mexico": No such file or directory
>>
>> which looks like the problem you describe -- so clearly the
>> command did not get through in that form from R using the
>> command system("ls -l \"New Mexico\"").
>>
>> Best wishes,
>> Ted.
>>
>> On 03-Jul-08 09:18:14, Philip James Smith wrote:
>>
>>> Thanks for your reply, Ted... I am very grateful for it.
>>>
>>> Using your notation, what I need is a character string Y that looks
>>> like:
>>>
>>> >
>>> > Y
>>> >[1] ""New Mexico""
>>>
>>> rather than
>>>
>>> > Y
>>> >[1] "\"New Mexico\""
>>>
>>> i.e., Y must have the string 'New Mexico' surrounded by double quotes,
>>> rather than double quotes preceded by slashes.
>>>
>>> The reason why I need it that way is that I've over simplified my request
>>> and the character string is actually a unix command that needs to be
>>> surrounded by double quotes when embeded in the unix (linux) executes it.
>>> When unix sees that slash before the quotes, it gives an
>>> error.
>>>
>>> I'd be grateful if you can provide a solution to this!!
>>>
>>> Thank you, Ted!
>>>
>>> Gratefully,
>>> Phil Smith
>>> Duluth, GA
>>>
>>>
>>> (Ted Harding) wrote:
>>>
>>>> On 03-Jul-08 01:25:55, Philip James Smith wrote:
>>>>
>>>>> Hi R Community:
>>>>> I've got a character string that looks like: New Mexico
>>>>>
>>>>> How to I create the new character string that looks like: "New
>>>>> Mexico" That is, it is the original string (New Mexico) with double
>>>>> quotes infront and behind it?
>>>>>
>>>>> Thanks,
>>>>> Phil Smith
>>>>>
>>>> I tried the following. Is that the sort of thing you want to achieve?
>>>>
>>>> X<-"New Mexico"
>>>> Y<-"\"New Mexico\""
>>>> X
>>>> # [1] "New Mexico"
>>>> Y
>>>> # [1] "\"New Mexico\""
>>>> plot((1:10),xlab=X,ylab=Y)
>>>>
>>>> Best wishes,
>>>> Ted.
>>>>
>>>> --------------------------------------------------------------------
>>>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>>>> Fax-to-email: +44 (0)870 094 0861
>>>> Date: 03-Jul-08 Time: 09:22:10
>>>> ------------------------------ XFMail ------------------------------
>>>>
>>>>
>>>>
>>> ______________________________________________
>>> 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.
>>>
>>
>> --------------------------------------------------------------------
>> E-Mail: (Ted Harding) <Ted.Harding at manchester.ac.uk>
>> Fax-to-email: +44 (0)870 094 0861
>> Date: 03-Jul-08 Time: 11:08:36
>> ------------------------------ XFMail ------------------------------
>>
>>
>
> ______________________________________________
> 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.
>
--
Brian D. Ripley, ripley at stats.ox.ac.uk
Professor of Applied Statistics, http://www.stats.ox.ac.uk/~ripley/
University of Oxford, Tel: +44 1865 272861 (self)
1 South Parks Road, +44 1865 272866 (PA)
Oxford OX1 3TG, UK Fax: +44 1865 272595
More information about the R-help
mailing list