[R] apparent problems with the textConnection command

ravi rv15i at yahoo.se
Wed May 19 10:35:12 CEST 2010


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



More information about the R-help mailing list