[R] apparent problems with the textConnection command
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="")
Read 1 item
> 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,
> ----- 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
> 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;
>> ####### the above code gives exactly the results that I want
>> # next, attempt to read the same text in via textConnection
>> The results that I obtained (with an echo of the commands) were :
>>  "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,
>> R-help at r-project.org mailing list
>> 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