From murdoch at stats.uwo.ca  Wed Jul 23 21:38:21 2008
From: murdoch at stats.uwo.ca (Duncan Murdoch)
Date: Wed, 23 Jul 2008 15:38:21 -0400
Subject: [Rd] [R] shQuote and cat
In-Reply-To: <22D850BC39A25742977325ADDE208E770266956392@chiexchange02.w2k.jumptrading.com>
References: <22D850BC39A25742977325ADDE208E770266956391@chiexchange02.w2k.jumptrading.com>,
	<4887741D.9030804@stats.uwo.ca>
	<22D850BC39A25742977325ADDE208E770266956392@chiexchange02.w2k.jumptrading.com>
Message-ID: <488788AD.1050605@stats.uwo.ca>

On 7/23/2008 2:53 PM, Vadim Organovich wrote:
> I feel like it now belongs to r-devel more than to r-help.
> 
> My output was garbled because I sent the original message as HTML, sorry about that.
> 
> Your output, "\"\\"a\\"\"", is what I get too. That is
>  > cat(shQuote(shQuote(shQuote("a"))), '\n')
> "\"\\"a\\"\""
> , which I think should be "\"\\\"a\\\"\"".
> 
> Actually, the R's output, "\"\\"a\\"\"", is not even a valid string literal, that is
>> x <- "\"\\"a\\"\""
> fails.

It's not intended to be a string literal in R, it's intended to be input 
to the Windows CMD shell.  If you want a string literal in R, don't use 
cat().  cat() shows you the naked contents of the string without any 
quoting to make it a valid string literal.

> 
> 
> Now, by cat() being the inverse of shQuote() I mean printing the same literal as it goes into shQuote, quotes included:
>> cat(shQuote("a"), '\n')
> "a"
> 
> whereas
>> cat("a", '\n')
> a  ## no quotes
> 
> If cat() is not the inverse of shQuote() in the above sense, what is?

On Unix-like systems I think asking the shell to echo the output, i.e.

system(paste("echo", shQuote(input)), intern=TRUE)

is intended to reproduce the input.  However, the Windows CMD shell is 
different.  I don't know how to strip quotes off a string in it, e.g. I see

C:\WINDOWS\system32 echo "a"
"a"

Nevertheless, the quotes *are* necessary when passing filenames to 
commands, and they'll typically be stripped off when doing that, e.g.

echo hi >"a"

will create a file named a, not named "a", and

echo hi >"a b"

will create a file with a space in the name.

Duncan Murdoch



[Rd] [R] shQuote and cat
Duncan Murdoch