How to escape the percent symbol "%" in r?

In the url, I am dealing with the special character % ', which I must pass as a string. The url contains some arguments, so I use sprintf. How to escape the % 'character in r?

 start <- 1 #%s is my variable url<-(sprintf('https://www.amazon.com/s/ref=sr_pg_%s?rh=n%3A172282%2Cn%3A%21493964%2Cn%3A502394%2Cn%3A281052%2Cn%3A12556502011%2Cn%3A3017941&page=%s&ie=UTF8', start, start)) 

invalid format '% 2Cn% 3A'; use% s format for character objects

+6
source share
1 answer

The reference file for sprintf :

Wrapper for C function sprintf, ...

This way you avoid it in R same way you do for C , using the double warning signs %% to create one % , according

How to avoid sprintf ()% errors so that they are not recognized as variables?

In your code, we create a URL supposedly retrieving the first page in this search for amazon.com:

 url<-(sprintf('https://www.amazon.com/s/ref=sr_pg_%s?rh=n%%3ā€Œā€‹A172282%%2Cn%%3A%%21ā€Œā€‹493964%%2Cn%%3A50239ā€Œā€‹4%%2Cn%%3A281052%%2Cā€Œā€‹n%%3A12556502011%%2Cā€Œā€‹n%%3A3017941&page=%sā€Œā€‹&ie=UTF8', start, start)) 

produces

 > url [1] "https://www.amazon.com/s/ref=sr_pg_1?rh=n%3A172282%2Cn%3A%21493964%2Cn%3A502394%2Cn%3A281052%2Cn%3A12556502011%2Cn%3A3017941&page=1&ie=UTF8" 
+6
source

Source: https://habr.com/ru/post/1011636/


All Articles