Hi community, I am trying to complete the World Bank API Challange in R and I am getting an error:
Screen Link:
My Code:
wb_api_json_get_df<- function(endpoint, queries = list()) {
# Preparing the URL
url <- modify_url("http://api.worldbank.org", path = endpoint)
# API requests
response <- GET(url, query = queries)
# Tracking errors
if ( http_error(response) ){
print(status_code(response))
stop("Something went wrong.", call. = FALSE)
}
if (http_type(response) != "application/json") {
stop("API did not return json", call. = FALSE)
}
# Extracting content
json_text <- content(response, "text")
# Converting content into Dataframe
dataframe <- jsonlite::fromJSON(json_text)
# Return the dataframe
dataframe
}
wb_poverty_level <- wb_api_json_get_df("v2/country/afr/indicator/NV.AGR.PCAP.KD.ZG", list(format = "json", date="1989:2000"))
str(wb_poverty_level)
library(ggplot2)
ggplot(data = wb_poverty_level, aes(x = date, y = value, group = 1)) + geom_line() + ylab("Real agricultural GDP per capita growth rate (%)")
What I expected to happen:
I was expected to complete the challange and be able to move on, but i am getting an error
What actually happened:
Error: `data` must be a data frame, or other object coercible by `fortify()`, not a list
Traceback:
1. ggplot(data = wb_poverty_level, aes(x = date, y = value, group = 1))
2. as.environment("package:ggplot2")$ggplot(...)
3. ggplot.default(...)
4. fortify(data, ...)
5. fortify.default(data, ...)
6. abort(msg)
7. signal_abort(cnd)
the challange answer is:
# We use the existing `wb_api_json_get_df()` function as is.
wb_poverty_level <- wb_api_json_get_df("v2/country/afr/indicator/NV.AGR.PCAP.KD.ZG", list(format = "json",
date="1989:2000"))
str(wb_poverty_level)
library(ggplot2)
ggplot(data = wb_poverty_level, aes(x = date, y = value, group = 1)) + geom_line() + ylab("Real agricultural GDP per capita growth rate (%)")
Wondering if someone could help me with this?
Thank you in advance