Problem with escape character in paste-function in R I am trying to paste together a path-string. Like this &quot;C:\testfile.txt&quot;
This doesn't work at all:
filename &lt;- &quot;testfile.txt&quot;

path &lt;- paste(&quot;C:\\\&quot;,filename,sep=&quot;&quot;)

This also doesn't produce the right result:

filename &lt;- &quot;testfile.txt&quot;

path &lt;- paste(&quot;C:\\\\&quot;,filename,sep=&quot;&quot;)

[1] &quot;C:\\\testfile.txt&quot;

Can you help me to prudece a path-string with only one backslash?
This works:
&gt; paste(&quot;C:\\&quot;,filename,sep=&quot;&quot;)
[1] &quot;C:\\testfile.txt&quot;

Although you see TWO backslashes, there's really only one there, because usually when printed a backslash means &quot;the next character is encoding a special character&quot;. In this case the special character is literally a backslash.
You can see this by counting the number of characters in two backslashes:
&gt; nchar(&quot;\\&quot;)
[1] 1

And anyway you should probably be using file.path to construct paths and not fiddling around with backslashes:
&gt; file.path(&quot;C:&quot;,filename)
[1] &quot;C:/testfile.txt&quot;

(Note forward slashes here also work in Windows and don't suffer from having to be backslash-encoded).