Search code examples
ruser-inputreadline

R: read in a line from user input with newline character


I am trying to use readline() for reading in a user input. My input has a newline character, which gets escaped, unfortunately.

> readline(prompt = "Tell me: ")
Tell me: first\nsecond
[1] "first\\nsecond"

How do I get

[1] "first\nsecond"

Solution

  • Do you simply want to remove the extra \? If so:

    x
    #> [1] "first\\nsecond"
    
    gsub("\\n", "\n", x, fixed = TRUE)
    #> [1] "first\nsecond"
    

    Created on 2022-01-04 by the reprex package (v2.0.1)