I couldn't resist adding a more literal answer unback <- function(x) { chars <- unlist(strsplit(deparse(x),"")) chars <- chars[-c(1,length(chars))] paste(gsub("\\\\","/",chars),collapse="") } unback("\n") | David Duffy (MBBS PhD) ,-_|\ | email: davidD at qimr.edu.au ph: INT+61+7+3362-0217 fax: -0101 / * | Epidemiology Unit, Queensland Institute of Medical Research \_,-._/ | 300 Herston Rd, Brisbane, Queensland 4029, Australia GPG 4D0B994A v
Close, but does not work generally with RGui 2.1.1 patched, Windows XP: > unback(x="n\o") [1] "no" I'm also unable to parse "echo", suggested by Ten Harding and Henrik Bengtsson: > echo D:/spencerg/statmtds/R/Rnews> tmp.txt Error: syntax error > echo cat(gsub("\\\\", "/", readLines("tmp.txt"))) | R --slave Error: syntax error Earlier today, Sundar Dorai-Raj helped me with the following: > (File0 <- file.choose()) [1] "D:\\spencerg\\dataPOWER\\stats\\Tukey\\Boxplot_missing_Tukey2.txt" > strsplit(File0, "\\\\") [[1]] [1] "D:" "spencerg" [3] "dataPOWER" "stats" [5] "Tukey" "Boxplot_missing_Tukey2.txt" > fp. <- strsplit(File0, "\\\\")[[1]] > (path <- paste(fp.[-length(fp.)], collapse="/")) [1] "D:/spencerg/dataPOWER/stats/Tukey" > setwd(path) > getwd() [1] "D:/spencerg/dataPOWER/stats/Tukey" > File <- fp.[length(fp.)] > File [1] "Boxplot_missing_Tukey2.txt" Thanks to everyone who has contributed (or even read) this thread. I'm confident that a better method exists. Best Wishes, Spencer Graves David Duffy wrote:> I couldn't resist adding a more literal answer > > unback <- function(x) { > chars <- unlist(strsplit(deparse(x),"")) > chars <- chars[-c(1,length(chars))] > paste(gsub("\\\\","/",chars),collapse="") > } > > unback("\n") > > > | David Duffy (MBBS PhD) ,-_|\ > | email: davidD at qimr.edu.au ph: INT+61+7+3362-0217 fax: -0101 / * > | Epidemiology Unit, Queensland Institute of Medical Research \_,-._/ > | 300 Herston Rd, Brisbane, Queensland 4029, Australia GPG 4D0B994A v > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html-- Spencer Graves, PhD Senior Development Engineer PDF Solutions, Inc. 333 West San Carlos Street Suite 700 San Jose, CA 95110, USA spencer.graves at pdf.com www.pdf.com <http://www.pdf.com> Tel: 408-938-4420 Fax: 408-280-7915
On Wed, 29 Jun 2005, David Duffy wrote:> I couldn't resist adding a more literal answerThis can only work for escapes which are preserved. The parser maps \n to a character (LF) and the deparser maps it back to \n. This happens to be true of \a \b \f \n \r \t \v \\ but no others. For example, \s is mapped to s, and there is no difference between \s and s in the parsed input.> > unback <- function(x) { > chars <- unlist(strsplit(deparse(x),"")) > chars <- chars[-c(1,length(chars))] > paste(gsub("\\\\","/",chars),collapse="") > } > > unback("\n")> unback("\s")[1] "s" Spencer Graves keeps on insisting there is a better way, but all the solutions are to avoid sending the string to the parser, and hence avoiding having the string directly in an R script. This is common in shell scripts, which use 'here' documents to avoid 'quoting hell'. We can do that in R too. Here are two variants I have not seen in the thread test1.R: scan("", "", allowEscapes=FALSE, n=1, quiet=TRUE) D:\spencerg\dataPOWER\stats\Tukey\Boxplot_missing_Tukey2.txt catIx, "\n", sep="") R --slave --vanilla < test1.R D:\spencerg\dataPOWER\stats\Tukey\Boxplot_missing_Tukey2.txt (This one does not allow quoted strings.) test2.R: x <- readLines(stdin(), n=1) "D:\spencerg\dataPOWER\stats\Tukey\Boxplot_missing_Tukey2.txt" x <- gsub('^"(.*)"$', "\\1", x) cat(x, "\n") R --slave --vanilla < test2.R D:\spencerg\dataPOWER\stats\Tukey\Boxplot_missing_Tukey2.txt (This one allows surrounding quotes or not.) -- 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