[R] Escaping " ' " character

Marc Schwartz (via MN) mschwartz at mn.rr.com
Tue Aug 22 17:19:09 CEST 2006


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



More information about the R-help mailing list