Eflores89 / inegiR

R package to interact with INEGI API's :bar_chart: :chart_with_upwards_trend:
http://enelmargen.org/inegiR/
53 stars 12 forks source link

'd_dates' no encontrado. #25

Closed SymmetricSymplectic closed 10 months ago

SymmetricSymplectic commented 10 months ago

inegi_series(), con series generadas usando el constructor de consultas genera el siguiente output:

Error in data.frame(date = d_dates, date_shortcut = d_aux, values = as.numeric(d$OBS_VALUE), : objeto 'd_dates' no encontrado

CarlosGlezDlt commented 10 months ago

INEGI change the response data structure of the periodicity information; instead of retrieving the code with only a number ("n"), they add "0_" and then the number ("0_n"), so you need to modify the inegi_series function so it can recognize the periodicity of the series and can generate the dates to paste in the dataframe.

The next is the corrected function:

inegi_series <- function (series_id, token, geography = "00", database = "BIE", metadata = FALSE, lastonly = FALSE, as_tt = FALSE, as_compact = FALSE) { if (grepl(pattern = "[0-9]", x = series_id, ignore.case = TRUE)) { } else { stop("Series id must only be numbers. Adding the entire URL is deprecated in v3. See documentation.") } u <- paste0("https://www.inegi.org.mx/app/api/indicadores/desarrolladores/jsonxml/INDICATOR/", series_id, "/en/", geography, "/", ifelse(lastonly, "true", "false"), "/", database, "/2.0/", token, "?type=json") s <- jsonlite::fromJSON(u) if (!is.null(s$ErrorInfo)) { warning(paste0("INEGI error: ", s$ErrorInfo)) return(NULL) } d <- as.data.frame(s$Series$OBSERVATIONS) if (grepl("1", s$Series$FREQ, fixed=TRUE) || grepl("2", s$Series$FREQ, fixed=TRUE) || grepl("3", s$Series$FREQ, fixed=TRUE)) { d_dates <- gsub(pattern = "/[0-9]{2,5}", replacement = "", x = d$TIME_PERIOD) d_aux <- rep(x = "Year", times = length(d$TIME_PERIOD)) } else { if (grepl("6", s$Series$FREQ, fixed=TRUE)) { d_dates <- gsub(pattern = "/04", replacement = "/10", x = d$TIME_PERIOD) d_dates <- gsub(pattern = "/03", replacement = "/07", x = d_dates) d_dates <- gsub(pattern = "/02", replacement = "/04", x = d_dates) d_aux <- ifelse(grepl(pattern = "/10", x = d_dates), "Q4", ifelse(grepl(pattern = "/07", x = d_dates), "Q3", ifelse(grepl(pattern = "/04", x = d_dates), "Q2", "Q1"))) d_dates <- as.Date(zoo::as.yearmon(d_dates, "%Y/%m")) } else { if (grepl("8", s$Series$FREQ, fixed=TRUE)) { d_dates <- as.Date(zoo::as.yearmon(d$TIME_PERIOD, "%Y/%m")) d_aux <- paste0("M", lubridate::month(d_dates)) } else { if (grepl("9", s$Series$FREQ, fixed=TRUE)) { d_dates <- as.Date(zoo::as.yearmon(d$TIME_PERIOD, "%Y/%m")) d_aux <- 1:length(d$TIME_PERIOD) warning("Biweekly data is NOT coerced or forced to a date. Instead, the date_shortcut column is an index of the order of observations (larger N with same date is second half of month).") } else { if (grepl("4", s$Series$FREQ, fixed=TRUE) || grepl("5", s$Series$FREQ, fixed=TRUE) || grepl("7", s$Series$FREQ, fixed=TRUE) || grepl("10", s$Series$FREQ, fixed=TRUE) || grepl("11", s$Series$FREQ, fixed=TRUE) || grepl("12", s$Series$FREQ, fixed=TRUE) || grepl("13", s$Series$FREQ, fixed=TRUE)) { warning("Time format is not supported. Only Anual, Trimestral, Monthly or Biweekly indicators are supported by this package. Data was downloaded but will be passed on as-is.") } } } } } data <- data.frame(date = d_dates, date_shortcut = d_aux, values = as.numeric(d$OBS_VALUE), notes = d$OBS_NOTE) if (as_tt) { data <- as_tbltime(x = data, index = "date") } if (metadata) { warning("Metadata will be passed as-is. Use incat functions to download catalogs.") metadata <- data.frame(source = s$Series$SOURCE, topic = s$Series$TOPIC, notes = s$Series$NOTE, last_update = s$Series$LASTUPDATE, region = geography, units = s$Series$UNIT, indicator_ID = s$Series$INDICADOR, frequency = s$Series$FREQ, call_local_time = Sys.time(), call_unmasked = u) if (as_compact) { data$meta_source <- metadata$source[1] data$meta_topic <- metadata$topic[1] data$meta_notes <- metadata$notes[1] data$meta_lastupdate <- metadata$last_update[1] data$meta_region <- metadata$region[1] data$meta_units <- metadata$units[1] data$meta_indicatorid <- metadata$indicator_ID[1] data$meta_frequency <- metadata$frequency[1] data$meta_calltime <- metadata$call_local_time[1] data$meta_call <- u return(data) } else { l <- list(metadata = metadata, data = data) return(l) } } else { return(data) } }

SymmetricSymplectic commented 10 months ago

Just as a follow-up: Got some date conversion issues using base R as.Date(), so I switched those instances to tidyverse's as_date(). Otherwise, patch works as intended.

Thanks!

Eflores89 commented 10 months ago

Thanks - this package is not in active development so I was surprised when receive emails about it. You saved me a lot of time and effort. Let me work on the patch now!