Btibert3
Btibert3

Reputation: 40146

Encode a query string in R

Here is a query string that I use to plug into a form:

team,site,week,day,date,o:team,line,points,o:points@season=2011

and here is the resulting string that is passed to the website:

team%2Csite%2Cweek%2Cday%2Cdate%2Co%3Ateam%2Cline%2Cpoints%2Co%3Apoints%40season%3D2011

I know that R is a very powerful language. Are there any functions that would encode this for me? I figure I could write a function to mimic this output, but I didn't want to reinvent the wheel.

Any help will be greatly appreciated.

Upvotes: 12

Views: 4382

Answers (2)

Keith Hughitt
Keith Hughitt

Reputation: 4960

Another option is the URLencode() function which is part of the base utils package:

> URLencode('team,site,week,day,date,o:team,line,points,o:points@season=2011', reserved=TRUE)
[1] "team%2Csite%2Cweek%2Cday%2Cdate%2Co%3Ateam%2Cline%2Cpoints%2Co%3Apoints%40season%3D2011"

Just be sure to set reserved=TRUE if you want all of the punctuation to be encoded as well.

Upvotes: 3

themel
themel

Reputation: 8895

curlEscape in package RCurl does what you want:

> library(RCurl)
Loading required package: bitops
> curlEscape("team,site,week,day,date,o:team,line,points,o:points@season=2011")
[1] "team%2Csite%2Cweek%2Cday%2Cdate%2Co%3Ateam%2Cline%2Cpoints%2Co%3Apoints%40season%3D2011"

Upvotes: 15

Related Questions