Queer European MD passionate about IT

Mission571Solutions.Rmd 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. ---
  2. title: 'Guided Project: New York Solar Resource Data'
  3. author: "Dataquest"
  4. date: "11/26/2020"
  5. output: html_document
  6. ---
  7. # Finding the Suitable Endpoint and Parameters to Query the API
  8. ```{r}
  9. # Storing my api key in a variable
  10. the_key = "" #TODO Store your API key here
  11. # Identifying the API URL
  12. url <- "https://developer.nrel.gov/api/solar/solar_resource/v1.json"
  13. # Specifying the necessary parameters to request the New York City solar data
  14. parameters_list <- list(api_key = the_key, lat = 41, lon = -75)
  15. ```
  16. # Extracting the New York Solar Resource Data
  17. ```{r}
  18. # Loading the `httr` package
  19. library(httr)
  20. # Using the `GET()` function to request the data from the API with `url` and `parameters_list`
  21. response <- GET(url, query = parameters_list)
  22. # Tracking errors
  23. ## Displaying the status code with the `status_code()` function
  24. print(status_code(response))
  25. ## Displaying the API response format
  26. print(http_type(response))
  27. # Extracting the API response content as text
  28. json_text <- content(response, "text")
  29. # Displaying this content to check how it looks visually.
  30. print(json_text)
  31. ```
  32. # Parsing the JSON into R Object
  33. ```{r}
  34. # Parsing the `json_text` to a R object using the `jsonlite::fromJSON()` function
  35. json_lists <- jsonlite::fromJSON(json_text)
  36. # Displaying the structure of the R object using the `str()` function
  37. str(json_lists)
  38. ```
  39. # How to Create a Datarame from a Complex List
  40. # Building Datarame from a Complex List
  41. ```{r}
  42. # Extracting the outputs data
  43. outputs_list <- json_lists$outputs
  44. # Extracting the monthly vector (`monthly`) from the (`avg_dni`) list in the outputs data
  45. avg_dni <- outputs_list$avg_dni$monthly
  46. # Extracting the monthly vector (`monthly`) from the (`avg_ghi`) list in the outputs data
  47. avg_ghi <- outputs_list$avg_ghi$monthly
  48. # Extracting the monthly vector (`monthly`) from the (`avg_lat_tilt`) list in the outputs data
  49. avg_lat_tilt <- outputs_list$avg_lat_tilt$monthly
  50. # Combining the monthly vectors into a dataframe using the `tibble::tibble()` function
  51. ## Adding the `month` column containing month abbreviations: `Jan`, `Fev`,...,`Dec`
  52. dataframe <- tibble::tibble("avg_dni" = avg_dni,
  53. "avg_ghi" = avg_ghi,
  54. "avg_lat_tilt" = avg_lat_tilt,
  55. "month" = month.abb)
  56. # Displaying the dataframe
  57. dataframe
  58. ```
  59. We can see that all the columns are still lists containing one item. For future use of this dataframe, it would probably be necessary to convert these columns to numeric.
  60. # Extracting Datarame from a Complex List:
  61. ```{r}
  62. # Extracting the outputs list
  63. outputs_list <- json_lists$outputs
  64. # Simplifying the outputs list
  65. simplified_outputs_list <- unlist(outputs_list)
  66. # Restructuring the simplified list into a matrix of 13 rows (the annual value and 12 months values)
  67. data_matrix <- matrix(data = simplified_outputs_list, nrow = 13)
  68. # Removing the annual values from the data matrix
  69. data_matrix <- data_matrix[-1, ]
  70. # Converting the matrix into a dataframe using the `as.data.frame()` function
  71. another_dataframe <- as.data.frame(data_matrix)
  72. # Displaying the dataframe
  73. another_dataframe
  74. ```
  75. # Putting all together
  76. ```{r}
  77. library(httr)
  78. library(dplyr)
  79. the_key = "" #TODO Store your API key here
  80. # Creating the custom `nrel_api_json_get_df()` function inspiring from what we did in the previous missions
  81. ## The function has two parameters
  82. ### The `endpoint` parameter represents the endpoint we need
  83. ### The `queries` parameter represents the list of API request parameters.
  84. nrel_api_json_get_df <- function(endpoint, queries = list()) {
  85. ## Preparing the URL
  86. url <- modify_url("https://developer.nrel.gov", path = endpoint)
  87. ## Querying the API
  88. response <- GET(url, query = queries)
  89. ## Tracking errors
  90. if ( http_error(response) ){
  91. print(status_code(response))
  92. print(http_status(response))
  93. stop("Something went wrong.", call. = FALSE)
  94. }
  95. if (http_type(response) != "application/json") {
  96. stop("API did not return json", call. = FALSE)
  97. }
  98. ## Extracting content
  99. json_text <- content(response, "text")
  100. ## Converting content into Dataframe
  101. table_lst <- jsonlite::fromJSON(json_text)
  102. dataframe <- tibble::tibble("avg_dni" = as.numeric(table_lst$outputs$avg_dni$monthly),
  103. "avg_ghi" = as.numeric(table_lst$outputs$avg_ghi$monthly),
  104. "avg_lat_tilt" = as.numeric(table_lst$outputs$avg_lat_tilt$monthly),
  105. "month" = month.abb)
  106. ## Returning the dataframe
  107. dataframe
  108. }
  109. # Using the custom `nrel_api_json_get_df()` function to extract the solar resource as a dataframe
  110. ## Providing the `"api/solar/solar_resource/v1.json"` as the `endpoint` parameter
  111. ## Providing the `parameters_list` variable as `queries` parameter
  112. solar_resource_df <- nrel_api_json_get_df("api/solar/solar_resource/v1.json", parameters_list)
  113. # Printing the output dataframe
  114. solar_resource_df
  115. ```
  116. # Visualizing New York City Solar Resource Data
  117. ```{r}
  118. # Loading the `ggplot2` and `dplyr` packages
  119. library(ggplot2)
  120. library(dplyr)
  121. # Using the `ggplot()` function to plot the `avg_dni` value for each month
  122. ggplot(data = solar_resource_df,
  123. aes(x = month, y = avg_dni, group = 1)) +
  124. geom_line() +
  125. geom_point() +
  126. theme_bw()
  127. ggsave("plot_avg_dni_before_factor.svg")
  128. # Converting the `month` column into factor using the following command
  129. solar_resource_df <- solar_resource_df %>%
  130. mutate(month = factor(month, levels = month.abb))
  131. # Replotting the `avg_dni` value for each month
  132. ggplot(data = solar_resource_df,
  133. aes(x = month, y = avg_dni, group = 1)) +
  134. geom_line() +
  135. geom_point() +
  136. theme_bw()
  137. ggsave("plot_avg_dni_after_factor.svg")
  138. ```
  139. The first plot x-axis is ordered alphabetically, while the second is in the natural order of months, from January to December.
  140. This operation allows ordering the labels in the plot as we wish.