Re: [R] Escaping " ' " character

From: Marc Schwartz (via MN) <mschwartz_at_mn.rr.com>
Date: Wed 23 Aug 2006 - 01:19:09 EST

On Tue, 2006-08-22 at 07:32 -0700, Srinivas Iyyer wrote:
> Dear marc: thank you for your tip.
>
> > > cat(x.new, "\n")
> > 3\',5\'-cyclic-nucleotide phosphodiesterase activity
> >
>
> here cat is printing on screen.
>
> how can I direct the output to an object.
>
> I cannot do:
>
> y <- cat(x.new, "\n")
>
> is there any other way.
>
> thanks
> srini

Srini,

Going back to your initial post, try something like the following using paste():

> x <- "3',5'-cyclic-nucleotide phosphodiesterase activity"

> x.new <- gsub("'", "\\\\'", x)

#Note the escaping of the single quotes here:
> sql.cmd <- paste("fetch_count_fterm_sql(\'", (x.new), "\');",

                   sep = "")

# Beware any line wrapping here
> sql.cmd

[1] "fetch_count_fterm_sql('3\\',5\\'-cyclic-nucleotide phosphodiesterase activity');"

This way the character vector 'sql.cmd' has the full sql query, which you can then pass to your statement processing code.

I'm not sure how you are passing the code, but if in a text file as input, you can do something like:

> sink("sqlfile.txt")
> cat(sql.cmd, "\n")
> sink()

Where the text file 'sqlfile.txt' will contain the single line:

fetch_count_fterm_sql('3\',5\'-cyclic-nucleotide phosphodiesterase activity');

See ?sink for more information.

HTH, Marc



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 and provide commented, minimal, self-contained, reproducible code. Received on Wed Aug 23 01:46:50 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Wed 23 Aug 2006 - 04:23:01 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.