how to retrieve data from API in R

hi there,

how can retrieve data in R using API?

thanks,
rao

It probably depends on the API and how that is set up.

The API returns the data in json format.
from get method
what code should i write in R

start here, https://cran.r-project.org/web/packages/httr/vignettes/quickstart.html

hi nirgrahmuk,

res <- curl_fetch_memory("http://httpbin.org/cookies/set?foo=123&bar=ftw")
myjsondata <- jsonlite::prettify(rawToChar(req$content))
print(myjsondata)
json_data_frame1 <- as.data.frame(myjsondata)

I am getting this erro

Error in as.data.frame.default(myjsondata) :
cannot coerce class ‘"json"’ to a data.frame

library(curl)
library(jsonlite)
library(dplyr)
res <- curl_fetch_memory("http://httpbin.org/cookies/set?foo=123&bar=ftw")
(myjsondata <- jsonlite::prettify(rawToChar(res$content)))
(myjsondata_df <- jsonlite::fromJSON(myjsondata) %>% 
    as.data.frame())
1 Like

hi nirgrahmuk,

thank you very much.
i am making mistake on "as.data.frame())" only this line?

can you please tell me what is the meaning of this?

does it mean to casting, as we do in oop programming?
var a = (String) someclass.methodname(objectofanyclass)

does it mean same?

thank you
rao

as.data.frame is a base method it only has methods for transforming certain types of things to dayaframes, and a JSON is not one of them. Therefore the jsonlite package itself provides an alternative

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.