[R] Creating/Reading a complex string in R

Jeff Newmiller jdnewmil at dcn.davis.ca.us
Wed Jul 19 17:41:22 CEST 2017


I think the whole premise of this question is flawed. If you want to work with this string as data,  then read it in from a separate file using readLines. If it is fixed data that you want to be part of your program, then invest the effort to escape the odd characters and be done with it. But using backticks is a bad idea because they ate not just another kind of quote. 
-- 
Sent from my phone. Please excuse my brevity.

On July 19, 2017 8:17:22 AM PDT, David Winsemius <dwinsemius at comcast.net> wrote:
>
>> On Jul 19, 2017, at 5:38 AM, Michael Friendly <friendly at yorku.ca>
>wrote:
>> 
>> It was suggested to quote your string with *backticks* (` ... `)
>rather than single quotes.
>> 
>> String <- `<html>
>>     <head>
>>     ...
>> `
>
>That failed for me. The parser considered it a language object, an R
>name. Here's what I needed to do:
>
>> string <- `"this string has both "'" and '"'`
>Error: object '"this string has both "'" and '"'' not found
>> string <- deparse(`"this string has both "'" and '"'`)
>Error in deparse(`"this string has both "'" and '"'`) : 
>  object '"this string has both "'" and '"'' not found
>
>But success with:
>
>
>> string <- deparse(substitute(`"this string has both "'" and '"'`))
>
>> string
>[1] "\"this string has both \"'\" and '\"'"
>
>With linefeed:
>
>> string <- deparse(substitute(`"this string ...
>+  has both "'" and '"'`))
>> string
>[1] "\"this string ...\n has both \"'\" and '\"'"
>
>
>
>> On 7/18/2017 1:05 PM, Christofer Bogaso wrote:
>>> Thanks for your pointer.
>>> Is there any way in R how to replace " ' " with " /' "
>programmatically?
>
>Forward-slashes are not going to have any desirable effect. They are
>not the R escape-character.
>
>Best of luck. I do not think we yet know in what form this "string"
>exists at the moment. And it's not clear whether your use case requires
>keeping these lines all in one element of a character vector. Generally
>`readLines` and `scan` (and by extension all the read.* variants) wiill
>interpret the line-feeds as end of line.
>
>> cat(string, file="test.txt")
>
>> readLines("test.txt")     # will retrieve a two-element character
>vector.
>[1] "\"this string ..."        " has both \"'\" and '\"'"
>Warning message:
>In readLines("test.txt") : incomplete final line found on 'test.txt'



More information about the R-help mailing list