[R] Creating/Reading a complex string in R

David Winsemius dwinsemius at comcast.net
Wed Jul 19 17:17:22 CEST 2017


> 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'

-- 
David.

>> My actual string is quite lengthy, so changing it manually may not be
>> possible. I am aware of gsub() function, however not sure I can apply
>> it directly on my original string.
>> Regards,
>> On Tue, Jul 18, 2017 at 10:27 PM, John McKown
>> <john.archie.mckown at gmail.com> wrote:
>>> Try:
>>> 
>>> String = '<html>
>>>   <head>
>>>     <script type="text/javascript"     <script type="text/javascript">
>>>       mystatement(\'current\', {\'pac\':[\'\']});
>>>       mystatement;'
>>> 
>>> 
>>> To embed a single ' mark in a string delimited by ' marks, you must "escape"
>>> them by prefixing them with a back-slash \.
>>> 
>>> 
>>> 
>>> R version 3.4.0 (2017-04-21) -- "You Stupid Darkness"
>>> Copyright (C) 2017 The R Foundation for Statistical Computing
>>> Platform: x86_64-redhat-linux-gnu (64-bit)
>>> 
>>> R is free software and comes with ABSOLUTELY NO WARRANTY.
>>> You are welcome to redistribute it under certain conditions.
>>> Type 'license()' or 'licence()' for distribution details.
>>> 
>>>   Natural language support but running in an English locale
>>> 
>>> R is a collaborative project with many contributors.
>>> Type 'contributors()' for more information and
>>> 'citation()' on how to cite R or R packages in publications.
>>> 
>>> Type 'demo()' for some demos, 'help()' for on-line help, or
>>> 'help.start()' for an HTML browser interface to help.
>>> Type 'q()' to quit R.
>>> 
>>>> 
>>>> String = '<html>
>>> +   <head>
>>> +     <script type="text/javascript"     <script type="text/javascript">
>>> +       mystatement(\'current\', {\'pac\':[\'\']});
>>> +       mystatement;'
>>>> String
>>> [1] "<html>\n  <head>\n    <script type=\"text/javascript\"     <script
>>> type=\"text/javascript\">\n      mystatement('current', {'pac':['']});\n
>>> mystatement;"
>>> 
>>> 
>>> 
>>> On Tue, Jul 18, 2017 at 11:48 AM, Christofer Bogaso
>>> <bogaso.christofer at gmail.com> wrote:
>>>> 
>>>> Hi again,
>>>> 
>>>> Let say I have below string (arbitrary)
>>>> 
>>>> <html>
>>>>   <head>
>>>>     <script type="text/javascript"     <script type="text/javascript">
>>>>       mystatement('current', {'pac':['']});
>>>>       mystatement;
>>>> 
>>>> 
>>>> I want to pass above string to some R variable for further analysis.
>>>> So I have tried below :
>>>> 
>>>> String = '<html>
>>>>   <head>
>>>>     <script type="text/javascript"     <script type="text/javascript">
>>>>       mystatement('current', {'pac':['']});
>>>>       mystatement;'
>>>> 
>>>> It is not clearly working as I am getting below error :
>>>> 
>>>>> String = '<html>
>>>> 
>>>> +   <head>
>>>> 
>>>> +     <script type="text/javascript"     <script type="text/javascript">
>>>> 
>>>> +       mystatement('current', {'pac':['']});
>>>> 
>>>> Error: unexpected symbol in:
>>>> 
>>>> "    <script type="text/javascript"     <script type="text/javascript">
>>>> 
>>>>       mystatement('current"
>>>> 
>>>>>       mystatement;'
>>>> 
>>>> Error: object 'mystatement' not found
>>>> 
>>>>> 
>>>> 
>>>> 
>>>> Any pointer how to create my string 'String' based on above would be
>>>> highly appreciated.
>>>> 
>>>> Thanks for your time.
>>>> 
>>>> ______________________________________________
>>>> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>> PLEASE do read the posting guide
>>>> http://www.R-project.org/posting-guide.html
>>>> and provide commented, minimal, self-contained, reproducible code.
>>> 
>>> 
>>> 
>>> 
>>> --
>>> Veni, Vidi, VISA: I came, I saw, I did a little shopping.
>>> 
>>> Maranatha! <><
>>> John McKown
>> 
> 
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.

David Winsemius
Alameda, CA, USA



More information about the R-help mailing list