Skip to content

Printing a single "\" character

7 messages · Thomas Lumley, Duncan Murdoch, Firas Swidan +3 more

#
Hi,
I have a small R question: how to print a single "\" character? I have the
following results:
[1] "\\"

I need to make the following substitution as well, but it does not work
either:
[1] "g_g"

Thanks in advance,
Firas.
#
On Tue, 19 Apr 2005, Firas Swidan wrote:

            
Use cat("\\").
Use
   sub("_","\\\\_","g_g")

If you print() the result it will look as though it has a double \, but 
that's an optical illusion. cat() will show it correctly with a single \ 
(and nchar() will confirm that it has 4 characters).

 	-thomas
#
Firas Swidan wrote:
You can use the lower level function cat() to print exactly what you want:

 > cat("\\")
\>

Notice that not even a newline was printed after the backslash, so the 
prompt showed up on the same line.  To get what you were hoping for you 
could use

 > cat("[1] \"\\\"\n")
[1] "\"

but it's pretty hard to read, because of all the escapes.
I'm not sure what you want to get, but it might be

 > cat( sub("_", "\\\\_", "g_g") ); cat("\n")
g\_g

The extra escapes are necessary because you need to send \\ to sub so 
that it outputs a \.

Duncan Murdoch
#
Hi Thomas,
thanks for the help, it does work.
Regards,
Firas.
On Tue, 19 Apr 2005, Thomas Lumley wrote:

            
1 day later
#
Hi all --

    I have a matrix of doubles (roughly 30x80) which I'd like to copy to 
the clipboard.  However, as the following shows:

 > dm = matrix(runif(30 * 80), nrow = 80)
 > write.table(dm, "clipboard", sep = "\t")
Warning message:
clipboard buffer is full and output lost

    Is there any way to increase the buffer?  Obviously, other programs 
don't have the same limitations (i.e., I can copy the same volume of 
data from Excel or my text editor and paste it into R without a problem)

-- Chris
#
Hi Chris,
Clipboard:
...
     When writing to the clipboard, the output is copied to the
     clipboard only when the connection is closed or flushed. There is
     a 32Kb limit on the text to be written to the clipboard. This can
     be raised by using e.g. 'file("clipboard-128")' on NT-based
     versions of Windows, to give 128Kb.

So,
Warning message: 
clipboard buffer is full and output lost
Cheers,
Rich
On 4/21/05, Chris Bergstresser <chris at subtlety.com> wrote:

  
    
#
On Thu, 21 Apr 2005, Rich FitzJohn wrote:

            
Just to add: this is not the same as copying in Excel or a text editor.
The equivalent of that is writeClipboard(), and that is not limited by R
(although it is by Windows).  Here you are treating the clipboard as a 
sequential file.

The reason for the choice of 32Kb is that is the clipboard limit on 16-bit 
versions of Windows, perpetuated in Win95 (at least).