Thank you for your help in advance. I am trying to pull some data back from a web service library(httr) sample2 <- GET("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":40.7,\"lon\":-76.5}]}&api_key=mycode") result2 <- content(sample2) height <- result2$height[[1]] I would like to put by own latitude and longitude in but alas when I use paste() to combine the double quotes become stuck as literally \" lat <-10 long <20 library(httr) sample2 <- GET(paste("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\", lat , \":40.7,\", long,\":-76.5}]}&api_key=mycode")) result2 <- content(sample2) height <- result2$height[[1]] Can you please suggest a way around this, it has been driving me mad! Thank you so much. Oliver [[alternative HTML version deleted]]
a) you need to read the help pages on the paste function... more likely you are looking for the paste0 function because extra spaces will most likely break the GET request format. b) quotes do not become "stuck" as \" ... that is a visual representation intended to clarify that that quote is not terminating the string but is actually part of it. If you want to see the contents of the string without the escaping, use the cat function instead of depending on the default print behavior (which shows the escape characters). c) The format of the GET request string includes parameter identifiers "lat" and "lon"... don't get confused between those strings being sent to the webserver and what your variables are called. Also, following those strings and a colon are numeric values... those are what you need to replace. Your attempt did not remove the literal digits from the surrounding strings. d) You really need to post plain text as the Posting Guide warns you... your code was getting contaminated by the mailing list "fixing" this mistake on your part. This may completely obstruct your meaning in future questions, and only you can avoid this by using your email software properly. ######## lat <- 40.7 lon <- -76.5 tstring1 <- "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":40.7,\"lon\":-76.5}]}&api_key=mycode" cat( tstring1, "\n" ) # output on its own line #> https://elevation.mapzen.com/height?json={"range":false,"shape":[{"lat":40.7,"lon":-76.5}]}&api_key=mycode tstring2 <- paste0( "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":",lat,",\"lon\":",lon,"}]}&api_key=mycode" ) tstring1 == tstring2 # confirm results are the same #> [1] TRUE lat <- 10 lon <- 20 tstring3 <- paste0( "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":",lat,",\"lon\":",lon,"}]}&api_key=mycode" ) cat( tstring3, "\n" ) # output on its own line #> https://elevation.mapzen.com/height?json={"range":false,"shape":[{"lat":10,"lon":20}]}&api_key=mycode ######## On Wed, 24 Jan 2018, Oliver Morris wrote:> Thank you for your help in advance. > > > I am trying to pull some data back from a web service > > > library(httr) > sample2 <- GET("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":40.7,\"lon\":-76.5}]}&api_key=mycode") > result2 <- content(sample2) > height <- result2$height[[1]] > > > I would like to put by own latitude and longitude in but alas when I use > paste() to combine the double quotes become stuck as literally \" > > > lat <-10 > > long <20 > > library(httr) > sample2 <- GET(paste("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\", lat , \":40.7,\", long,\":-76.5}]}&api_key=mycode")) > result2 <- content(sample2) > height <- result2$height[[1]] > > > Can you please suggest a way around this, it has been driving me mad! > > > Thank you so much. > > > Oliver > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k
Thank you the paste0() worked. Really appreciate the help. ________________________________ From: Jeff Newmiller <jdnewmil at dcn.davis.ca.us> Sent: Wednesday, January 24, 2018 8:05:46 PM To: Oliver Morris Cc: r-help at r-project.org Subject: Re: [R] Issue with concatenation of URL losing a) you need to read the help pages on the paste function... more likely you are looking for the paste0 function because extra spaces will most likely break the GET request format. b) quotes do not become "stuck" as \" ... that is a visual representation intended to clarify that that quote is not terminating the string but is actually part of it. If you want to see the contents of the string without the escaping, use the cat function instead of depending on the default print behavior (which shows the escape characters). c) The format of the GET request string includes parameter identifiers "lat" and "lon"... don't get confused between those strings being sent to the webserver and what your variables are called. Also, following those strings and a colon are numeric values... those are what you need to replace. Your attempt did not remove the literal digits from the surrounding strings. d) You really need to post plain text as the Posting Guide warns you... your code was getting contaminated by the mailing list "fixing" this mistake on your part. This may completely obstruct your meaning in future questions, and only you can avoid this by using your email software properly. ######## lat <- 40.7 lon <- -76.5 tstring1 <- "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":40.7,\"lon\":-76.5}]}&api_key=mycode" cat( tstring1, "\n" ) # output on its own line #> https://elevation.mapzen.com/height?json={"range":false,"shape":[{"lat":40.7,"lon":-76.5}]}&api_key=mycode tstring2 <- paste0( "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":",lat,",\"lon\":",lon,"}]}&api_key=mycode" ) tstring1 == tstring2 # confirm results are the same #> [1] TRUE lat <- 10 lon <- 20 tstring3 <- paste0( "https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":",lat,",\"lon\":",lon,"}]}&api_key=mycode" ) cat( tstring3, "\n" ) # output on its own line #> https://elevation.mapzen.com/height?json={"range":false,"shape":[{"lat":10,"lon":20}]}&api_key=mycode ######## On Wed, 24 Jan 2018, Oliver Morris wrote:> Thank you for your help in advance. > > > I am trying to pull some data back from a web service > > > library(httr) > sample2 <- GET("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\"lat\":40.7,\"lon\":-76.5}]}&api_key=mycode") > result2 <- content(sample2) > height <- result2$height[[1]] > > > I would like to put by own latitude and longitude in but alas when I use > paste() to combine the double quotes become stuck as literally \" > > > lat <-10 > > long <20 > > library(httr) > sample2 <- GET(paste("https://elevation.mapzen.com/height?json={\"range\":false,\"shape\":[{\", lat , \":40.7,\", long,\":-76.5}]}&api_key=mycode")) > result2 <- content(sample2) > height <- result2$height[[1]] > >[[elided Hotmail spam]]> > > Thank you so much. > > > Oliver > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. >--------------------------------------------------------------------------- Jeff Newmiller The ..... ..... Go Live... DCN:<jdnewmil at dcn.davis.ca.us> Basics: ##.#. ##.#. Live Go... Live: OO#.. Dead: OO#.. Playing Research Engineer (Solar/Batteries O.O#. #.O#. with /Software/Embedded Controllers) .OO#. .OO#. rocks...1k --------------------------------------------------------------------------- [[alternative HTML version deleted]]