[R] Creating/Reading a complex string in R

Olivier Crouzet olivier.crouzet at univ-nantes.fr
Tue Jul 18 22:16:25 CEST 2017


On Tue, 18 Jul 2017 22:35:17 +0530
Christofer Bogaso <bogaso.christofer at gmail.com> wrote:

> Thanks for your pointer.
> 
> Is there any way in R how to replace " ' " with " /' "
> programmatically?
> 

Hi,

perhaps you should simply read the content of a corresponding text
file? There's a simple way using the readr package. Using this
package, you can store the content of a whole file as a
single string. I haven't used it... but if you do choose this way, you
should investigate how it stores / mangages end-of-lines.

#install.packages(readr)
library(readr)
temp <- read_file('file.txt')

You can then process the string that is contained in the temp variable
as you wish to.

Olivier.

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


-- 
  Olivier Crouzet, PhD
  /Assistant Professor/
  @LLING - Laboratoire de Linguistique de Nantes
    UMR6310 CNRS / Université de Nantes
  /Guest Researcher/
  @UMCG (University Medical Center Groningen)
    ENT department
    Rijksuniversiteit Groningen



More information about the R-help mailing list