[R] apparent problems with the textConnection command
Mario Valle
mvalle at cscs.ch
Wed May 19 10:48:02 CEST 2010
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.
--
Ing. Mario Valle
Data Analysis and Visualization Group | http://www.cscs.ch/~mvalle
Swiss National Supercomputing Centre (CSCS) | Tel: +41 (91) 610.82.60
v. Cantonale Galleria 2, 6928 Manno, Switzerland | Fax: +41 (91) 610.82.82
More information about the R-help
mailing list