[R] apparent problems with the textConnection command

Duncan Murdoch murdoch.duncan at gmail.com
Wed May 19 12:56:22 CEST 2010


On 19/05/2010 6:46 AM, ravi wrote:
> Hi Mario,
> Thanks for your reply. I should now explain what it was that got me started with the example that I showed. The path environmental variable in windows is a long string with a number of backslash characters. To study and check the individual components in the path, it helps if it can be obtained as a list with each seperate component on a new line. I wanted to check if I could automate this with R.
> So, it is not a case of my entering the string manually. With manual entry, I would of course use \\.
>   

Don't expect cut and paste to work.  Pass the string in correctly using 
Sys.getenv("PATH"), or after cut and paste, go through and manually 
escape all the backslashes, or enter it using scan(what=""), e.g.

 > path <- scan(what="")
1: c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;
2:
Read 1 item
 > path
[1] "c:\\Rtools\\bin;c:\\Rtools\\perl\\bin;c:\\Rtools\\MinGW\\bin;"

Duncan Murdoch
> I could paste the entire path string in a text file, read in the file and then obtain a column of the path components. If you look at the output of c1 in my original mail, you will see that the readLines command recognises the escape character and automatically adds an extra backslash to enable further processing of the string in R. After this, I wanted to go one step further - to skip pasting the string in a text file. Instead, I wanted to directly paste the string in the argument to the textConnection command. As I showed earlier, this step failed. 
> Since I thought that readLines, textConnection and read.table belonged to the same family of commands, I expected them to have the same capability. So, my question is - can these commands be tweaked in some way to have the same flexibility? 
> Thanking you,
> Ravi
>
>
> ----- Original Message ----
> From: Mario Valle <mvalle at cscs.ch>
> To: ravi <rv15i at yahoo.se>
> Cc: "r-help at r-project.org" <r-help at r-project.org>
> Sent: Wed, 19 May, 2010 10:48:02
> Subject: Re: [R] apparent problems with the textConnection command
>
> Is not a problem of textConnection. It it how strings work.
> In them \ is the escape character, so you have to enter \\
> Hope it helps
>         mario
>
> ravi wrote:
>   
>> Dear list,
>> In my experiments in reading in text data, I have obtained some peculiar results. I would appreciate any help in understanding these results. Consider the following code :
>>
>> ## Reading in text data from a text file
>> ### the first line of file f1.txt contains the text :    c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;
>> f1<-file("C:\\Ex\\f1.txt")
>> c1<-readLines(f1,1)
>> c2<-gsub(";",";\n",c1)
>> cat(c2)
>> close(f1)
>> ####### the above code gives exactly the results that I want
>> # next, attempt to read the same text in via textConnection
>> f2<-textConnection("c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;")
>> d1<-readLines(f2)
>> ################ 
>>
>> The results that I obtained (with an echo of the commands) were :
>>     
>>> f1<-file("C:\\Ex\\f1.txt")
>>> c1<-readLines(f1,1)
>>> c1
>>>       
>> [1] "c:\\Rtools\\bin;c:\\Rtools\\perl\\bin;c:\\Rtools\\MinGW\\bin;"
>>     
>>> c2<-gsub(";",";\n",c1)
>>> cat(c2)
>>>       
>> c:\Rtools\bin;
>> c:\Rtools\perl\bin;
>> c:\Rtools\MinGW\bin;
>>     
>>> close(f1)
>>> f2<-textConnection("c:\Rtools\bin;c:\Rtools\perl\bin;c:\Rtools\MinGW\bin;")
>>>       
>> Error: '\R' is an unrecognized escape in character string starting "c:\R"
>> What I would like to know :
>> 1. The textConnection command is, from what I understand, usually used to test typical data in a text file. However, in my example, initial testing with this command fails while the longer step of reading from a text file succeeds. Is there any reason for this? Is there any workaround for the problem with the textConnection command ?
>> 2. There appear to be similiar problem with the read.table command. Using the allowEscapes =TRUE did not seem to help. Is there some other option here? Like, for example, temporarily setting another character like "<" as the escape character? 
>>
>> I would like to explain here that these questions are not directly related to any real world problem solving. They are just my attempts to understand the R language better.
>> Thanking you,
>> Ravi
>>
>> ______________________________________________
>> 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