Re: [R] How to convert "c:\a\b" to "c:/a/b"

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Thu 30 Jun 2005 - 08:33:47 EST

Note that if you want to source it rather than than run it as a batch job from the command line you will something like this. The way it works is that one puts the file name into comments that are marked with tags and the script rereads itself as data picking out the tagged lines and removing the tags from those lines. script.name() returns the name of the script its running in and my.stdin sets up a connection to the script as data after stripping out all lines that do not match the tag and removing the tag from those lines that do. (This is a variation of some code that I previously posted.)

# source the following from R

script.name <- function()
 showConnections()[as.character(eval.parent(quote(file), n=3)), "description"] my.stdin <- function( tag = "^#>", script.name. = script.name())  textConnection( sub(tag, "", grep(tag,readLines(script.name.),value=TRUE)) )

x <- readLines( my.stdin() )
#>c:\a\b
cat(x, "\n")

On 6/29/05, Spencer Graves <spencer.graves@pdf.com> wrote:
> Thank You, Prof. Ripley!
>
> Both "test1.R" and "test2.R" worked for me just now, as did the
> following minor modification:
>
> (x <- readLines(stdin(), n=1))
> D:\spencerg\dataPOWER\stats\Tukey\Boxplot_missing_Tukey2.txt
>
> Thanks again.
>
> spencer graves
>
> Prof Brian Ripley wrote:
> > On Wed, 29 Jun 2005, David Duffy wrote:
> >
> >
> >>I couldn't resist adding a more literal answer
> >
> >
> > This 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.)
> >
>
> --
> Spencer Graves, PhD
> Senior Development Engineer
> PDF Solutions, Inc.
> 333 West San Carlos Street Suite 700
> San Jose, CA 95110, USA
>
> spencer.graves@pdf.com
> www.pdf.com <http://www.pdf.com>
> Tel: 408-938-4420
> Fax: 408-280-7915
>
> ______________________________________________
> R-help@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
>



R-help@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 Received on Thu Jun 30 08:42:45 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:07 EST