Skip to content
Advertisement

Is there any way to avoid double quotes around an URL in R?

I am trying to pull data using an API call with GET from httr. The API call has SQL statements that require double quotes around table and field IDs, and single quotes around string values.
This means I get some issues when I use the URL in R, because it needs quotes around it there as well.

The API call looks like this:
resp <- GET("https://api.energidataservice.dk/datastore_search_sql?sql=SELECT * from "co2emis" WHERE "Minutes5UTC" > ((current_timestamp at time zone 'UTC') - INTERVAL '1 day') AND "PriceArea" = 'DK1'")

I have tried using ‘ or “, but that doesn’t work.

Is it possible to somehow avoid using quotes around the URL? Or an alternative way of doing it?

Advertisement

Answer

The issue isn’t to do with escaping the quotes (although you do need to do that), but rather you need to use the query parameter with GET():

library(httr)

result <- GET("https://api.energidataservice.dk/datastore_search_sql",
          query = list(sql = "SELECT * from "co2emis" WHERE "Minutes5UTC" > ((current_timestamp at time zone 'UTC') - INTERVAL '1 day') AND "PriceArea" = 'DK1'"))

# Parse the result:
dat <- content(result, as = "parsed")

Or you can read it in directly using the entire url string with jsonlite::read_json():

library(jsonlite)

read_json("https://api.energidataservice.dk/datastore_search_sql?sql=SELECT * from "co2emis" WHERE "Minutes5UTC" > ((current_timestamp at time zone 'UTC') - INTERVAL '1 day') AND "PriceArea" = 'DK1'")

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement