Search code examples
rnewlineword-wraprodbcend-of-line

String continuation across multiple lines, no newline characters


Am using the RODBC library to bring data into R. I have a long query that I want to pass a variable to, much like this SO user.

Problem is that R interprets the whitespace/carriage returns in my query as a newline '\n'.

The accepted solution for this question suggests to simply break up the text into chunks and then paste() together - which works, but ideally I'd like to keep the whitespace intact - makes it easier to test/verify the behavior of the query over in the database before pasting into R.

In other languages I'm familiar with there's a simple line continuation character - indeed, several of the comments on the accepted answer are looking for an approach similar to python's \.

I found an aside to a workaround using strwrap deep in the bowels of an R discussion lists, so in the interest of making the internet better I will post it here. However, if someone can point the direction toward a more elegant/straightforward solution, I will happily accept your answer.


Solution

  • query <- gsub(pattern='\\s',replacement="",x=query)