Thanks for your pointer. Is there any way in R how to replace " ' " with " /' " programmatically? 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
On Tue, Jul 18, 2017 at 12:05 PM, Christofer Bogaso < bogaso.christofer at gmail.com> wrote:> Thanks for your pointer. > > Is there any way in R how to replace " ' " with " /' " programmatically? > > 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, > >?I guess your string value is in a file? And you'd like to update the file contents. You didn't say which OS you're on. I'm a Linux person, and the simplest for me would be: sed -i -r "s/'/\\'/g" myfile.txt? #note the \\ becomes a single \ in the output. weird. Or just edit the file in your favorite editor. In mine, vim, it would be the command ":g/'/s/\\'/g" (the command is within the " marks). You are correct that you could use gsub(). But you can't just paste your string into an R program as is, as you have already seen. You'd need to put it into a file. Then read that file into a program (perhaps an R program using gsub()) to write the updated string to another file. Which contents you could then cut'n'paste into your original R file. something like: #!/usr/bin/Rscript # files <- commandArgs(trailingOnly=TRUE); # for (infile in files) { x=readLines(infile); y=gsub("'","\\\\'",x); ofile=paste0(infile,".new"); writeLines(y,ofile); } -- Veni, Vidi, VISA: I came, I saw, I did a little shopping. Maranatha! <>< John McKown [[alternative HTML version deleted]]
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
It was suggested to quote your string with *backticks* (` ... `) rather than single quotes. String <- `<html> <head> ... ` 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? > > 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 >
> 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