简体   繁体   中英

Using gsub to replace \ to create an URL string

I am using gsub to replace \\, but I receive an error. I have already tried with

First idea (Wrong!)

root <- "http://maps.google.com/maps/api/geocode/json\""
u <- gsub("\", "", u)

Second idea (Wrong!)

root <- "http://maps.google.com/maps/api/geocode/json\""
u <- gsub("[\]", "", u)

Any other idea?

I am using the root variable for this:

 parameters <- "&key=my-key"
 root <- "http://maps.google.com/maps/api/geocode/json\""
 apiRequests <- paste(root, "?address=", "paris, france", 
 "\"",parameters,sep = "")

 conn <- httr::GET(URLencode(apiRequests))
 apiResponse <- jsonlite::fromJSON(httr::content(conn, "text"))

I think what you want is this

root <- "http://maps.google.com/maps/api/geocode/json\""
root <- gsub('\"', "", root)

The back slash is escaping the first " and so then you want to replace the escaped " which is \\" and use single quotes to keep the quoting straight.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM