Dear R-Help, I'm using R version 1.6.0 on a Windows computer. I am trying to create a function that, among other things, constructs strings that refer to Windows files, e.g., I might want to construct a string like 'c:\work\part1.txt'. I have found that the following does not work.> paste("c:", "\", "work", "\", "part1.txt", sep="")Error: syntax error I'm guessing that R interprets "\" as some kind of special control character, and that there is some way to show that one wants a literal interpretation of "\" and not a control interpretation, but I haven't been able to find an explanation of this issue. I understand that I must use 'c:\\work\\part1.txt' or 'c:/work/part1.txt' to refer to the file that Windows knows as 'c:\work\part.txt', but what I'm trying to do is to write an R function that writes references to Windows files into a text file, where a different Windows programs will later read these references in the standard Windows syntax. Can someone tell me how to create the character string 'c:\work\part1.txt' from the parts, "c:", "work", an "part1.txt"? John Miyamoto -------------------------------------------------------------------- John Miyamoto, Dept. of Psychology, Box 351525 University of Washington, Seattle, WA 98195-1525 Phone 206-543-0805, Fax 206-685-3157, Email jmiyamot at u.washington.edu Homepage http://faculty.washington.edu/jmiyamot/ --------------------------------------------------------------------
Why guess? It's in the R Language definition and in all good books on S/R. `String constants are delimited by a pair of single (') or double (") quotes and can contain all other printable characters. Quotes and other special characters within strings are specified using escape sequences:' paste("c:", "work", "part1.txt", sep="\\") On Sun, 22 Dec 2002, John Miyamoto wrote:> Dear R-Help, > I'm using R version 1.6.0 on a Windows computer. I am trying to create > a function that, among other things, constructs strings that refer to > Windows files, e.g., I might want to construct a string like > 'c:\work\part1.txt'. I have found that the following does not work. > > > paste("c:", "\", "work", "\", "part1.txt", sep="") > Error: syntax error > > I'm guessing that R interprets "\" as some kind of special control > character, and that there is some way to show that one wants a literal > interpretation of "\" and not a control interpretation, but I haven't been > able to find an explanation of this issue. I understand that I must use > 'c:\\work\\part1.txt' or 'c:/work/part1.txt' to refer to the file that > Windows knows as 'c:\work\part.txt', but what I'm trying to do is to > write an R function that writes references to Windows files into a text > file, where a different Windows programs will later read these references > in the standard Windows syntax.c:/work/part1.txt *is* standard Windows syntax, too!> Can someone tell me how to create the character string > 'c:\work\part1.txt' from the parts, "c:", "work", an "part1.txt"?-- 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
> write an R function that writes references to Windows files into a text > file, where a different Windows programs will later read these references > in the standard Windows syntax. > Can someone tell me how to create the character string > 'c:\work\part1.txt' from the parts, "c:", "work", an "part1.txt"?I don't have access to a Windows machine right now to test this, but might file.path() help here? -J
R is interpreting \" as the ASCII character for ", not "\". This means that your second argument to paste() is the string "\", " and there there follows a 'work', which is why you get a syntax error. Yes, you should use \\ for backslash, e.g.> pathname <- paste("c:", "\\", "work", "\\", "part1.txt", sep="") > print(pathname)[1] "c:\\work\\part1.txt"> cat(pathname, "\n")c:\work\part1.txt Note that the "\\" is not two characters but one, i.e. nchar("\\") == 1. Henrik Bengtsson> -----Original Message----- > From: r-help-admin at stat.math.ethz.ch > [mailto:r-help-admin at stat.math.ethz.ch] On Behalf Of John Miyamoto > Sent: den 22 december 2002 19:01 > To: R discussion group > Subject: [R] pasting "\" into character strings > > > Dear R-Help, > I'm using R version 1.6.0 on a Windows computer. I am > trying to create a function that, among other things, > constructs strings that refer to Windows files, e.g., I might > want to construct a string like 'c:\work\part1.txt'. I have > found that the following does not work. > > > paste("c:", "\", "work", "\", "part1.txt", sep="") > Error: syntax error > > I'm guessing that R interprets "\" as some kind of special > control character, and that there is some way to show that > one wants a literal interpretation of "\" and not a control > interpretation, but I haven't been able to find an > explanation of this issue. I understand that I must use > 'c:\\work\\part1.txt' or 'c:/work/part1.txt' to refer to the > file that Windows knows as 'c:\work\part.txt', but what I'm > trying to do is to write an R function that writes references > to Windows files into a text file, where a different Windows > programs will later read these references in the standard > Windows syntax. > Can someone tell me how to create the character string > 'c:\work\part1.txt' from the parts, "c:", "work", an "part1.txt"? > > John Miyamoto > > -------------------------------------------------------------------- > John Miyamoto, Dept. of Psychology, Box 351525 > University of Washington, Seattle, WA 98195-1525 > Phone 206-543-0805, Fax 206-685-3157, Email > jmiyamot at u.washington.edu Homepage > http://faculty.washington.edu/jmiyamot/ > > -------------------------------------------------------------------- > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/> r-help > >
Is it really necessary to use "\"? On my Windows 2000 system here at work, it appears that the "dos" shell interprets "/" as "\", even though "\" is the "official" Windows folder delimiter. For instance, I can type "C:/Program Files/R/rw1061/bin/Rgui.exe" (quotes included) and R opens. Using "/" would simplify matters, but are there pitfalls? Do all Windows systems exhibit this behavior? Just curious. Jim Garrett Becton Dickinson Diagnostic Systems Baltimore, Maryland, USA ********************************************************************************* This message is intended only for the designated recipient(s). It may contain confidential or proprietary information and may be subject to the attorney-client privilege or other confidentiality protections. If you are not a designated recipient, you may not review, use, copy or distribute this message. If you receive this in error, please notify the sender by reply e-mail and delete this message. Thank you. **********************************************************************************