1

I have a character vector which includes regex special characters such as \n. I want to save this vector to a txt-file which shows the \n.

The code below creates a txt file, but when opening the file e.g. in notepad, the \n are translated into linebreaks. I don't want to see the linebreaks, I want to see \n.

library(tidyverse)
my_text <- "hello\n, nice to meet you.\n\n. how are you?"
my_text
readr::write_file(my_text, "my_text.txt")

this is what notepad shows:

hello
, nice to meet you.

. how are you?

but I want to see

"hello\n, nice to meet you.\n\n. how are you?"

I thought there was some kind of 'raw option', but seems like I am wrong. Thx!

zoowalk
  • 2,018
  • 20
  • 33
  • So, `my_text <- "hello\\n, nice to meet you.\\n\\n. how are you?"`? – Wiktor Stribiżew May 18 '20 at 09:02
  • @WiktorStribiżew many thanks. sorry, should have been clearer in my question. I am aware of escaping backlashes etc. when eg. searching the text with stringr/stringi etc. I was wondering whether there is some kind of save/write command which takes care of the need to escape e.g. backlashes when actually saving the vector to a file. – zoowalk May 18 '20 at 09:09
  • Do you mean something like `stringi::stri_escape_unicode(my_text)`? – Wiktor Stribiżew May 18 '20 at 09:27

0 Answers0