Status code error in World Bank API Challange in R

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

Inspect wb_poverty_level. What is it precisely? Do you see what you need in there? How do you adjust the definition of wb_api_json_get_df to get what you want?

1 Like

Thank you Bruno.

I managed to fix the previous problem adding data.frame() before I called wb_api_json_get_df function:

wb_poverty_level <- data.frame(wb_api_json_get_df("v2/country/afr/indicator/NV.AGR.PCAP.KD.ZG", list(format = "json", date="1989:2000")))

And now it is producing the plot just as the example, but it is still giving me an error message:

“One of your variables doesn’t seem to have the correct value. Please re-check the instructions and your code.”

So, I am not sure if I am understanding it

Take a look at the dataframe in your answer and take a look at the dataframe is Dataquest’s answer to investigate the difference.

If you want to know how to fix your answer, take a look inside.

The code below only has one significant difference relative to yours. Can you spot it?

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 <- data.frame(
    wb_api_json_get_df(
        "v2/country/afr/indicator/NV.AGR.PCAP.KD.ZG",
        list(format = "json",
        date="1989:2000")
        )
    )

# Select the relevant columns
wb_poverty_level <- wb_poverty_level[,8:15]

y_label <- "Real agricultural GDP per capita growth rate (%)"
library(ggplot2)

ggplot(
    data = wb_poverty_level, aes(x = date, y = value, group = 1)
) + geom_line() + ylab(y_label)
2 Likes

Fantastic Bruno! thank you so much for the tip! that is why my plot was working but they were not accepting my data frame. All working now, thank you

1 Like