knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/"
)

saythanks Donate Rbuildstatus CoverageStatus

sinimR

Chilean Municipalities Information System Wrapper

What can I do with this?

This R package allows easy SINIM (https://sinim.gov.cl) data retrieval what have advantages over the site:

library(dplyr)
library(sinimr)
library(sf)
library(tmap)

varcode <- 882
var <- get_sinim(varcode, 2018, 
                 region = 13, 
                 truevalue = T, 
                 geometry = T, 
                 auc = T, 
                 unit = "limites")

gran_santiago_plot <- tm_shape(var) +
  tm_fill(col = "value",
          palette = "BuPu", 
          border.col = "white", 
          border.alpha = 0.5,
          lwd=1,
          style = "jenks",
          title = get_sinim_var_name(varcode))+
  tm_text("municipality", size = 0.4, style="jenks") +
  tm_legend(legend.position = c("left", "top"), legend.title.size = 1, legend.text.size = 0.6) +
  tm_compass(type = "8star", position = c(.85, .80)) +
  tm_scale_bar(breaks = c(0, 10), text.size = 0.75, position = c("right", "bottom")) +
  tm_credits("Fuente: Sistema Nacional de Información Municipal (SINIM), SUBDERE, Ministerio del Interior.", position=c("left", "bottom"), size=0.55)+
  tm_layout(legend.width=1,
            inner.margins = c(0.1, 0.1, 0.10, 0.1), 
            legend.format = list(text.separator = "a", 
                                 fun = mm)) +
  tm_borders(col = 'black')


gran_santiago_plot

Support

FONDECYT Regular 2016 Nº 1161417, ¿Quién es responsable del desarrollo local? Una geografía política del neoestructuralismo en "comunas de exportación" (Comisión Nacional de Investigación Científica y Tecnológica).

A note on usage

When querying the API, please be respectful of the resources required to provide this data. Please retain the results for each request to avoid repeated requests for duplicate information.

Installation

install.packages("devtools")
devtools::install_github("robsalasco/sinimr")

How do I use it?

sinimR comes with a small set of functions to deliver the content of SINIM's webpage. To get a first glance of the categories of information what are available please use the get_sinim_cats() command.

library(sinimr)
get_sinim_cats()

Every category have a bunch of variables associated. Use the CODE number and the get_sinim_vars() function to get them.

get_sinim_vars(517)

Finally, to obtain the data across municipalities use the code column and specify a year.

head(get_sinim(c(4210, 4211), 2015))

By default the values are in miles de millones but it can be disabled using the truevalue = T switch.

head(get_sinim(c(4210, 4211), 2015, truevalue = T))

You can get multiple years too! use the command get_sinim() and add more years as in the example.

head(get_sinim(880, 2015:2017))

The geometries are available in long format using the geometry=T argument. By default it uses the comunal geographies but the limite urbano censal is also available. The switches are unit="comunas" and unit="limites". Note: Using limites not all features are available because some comunas are not related to urban zones. As shown in the example below you can obtain multiple years and variables in long format.

head(get_sinim(882, 2015:2017, geometry=T))

Another interesting feature is the possibility to subset by different contexts. e.g if you want the comunas of Antofagasta region this command is available. The command works with or without the presence of the geometry switch and other switches are avaiblable too region, provincia and comuna all working with codes.

head(get_sinim(882, 2015:2017, geometry=T, region=2))

You can get a subset too

head(get_sinim(882, 2015:2017, geometry=T, region=c(2,3)))

But where obtain the codes? a database is provided and you can filter it using the standard R functions.

head(id_geo_census)

Related to variables if you don't know what are you looking for use search_sinim_vars()to get search results based on variable descriptions, names and groups.

search_sinim_vars("cementerio")

Advanced usage

SINIM (Sistema Nacional de Información Municipal) by default applies a monetary correction to show current values of variables. The original values provided by municipalities are available using the moncorr = F switch. And if you want geographical identifiers like region or provincia you can apply them using idgeo = T switch.

Other example plots

Multiple variable faceted plot

library(tmap)
library(dplyr)
library(stringr)
library(sinimr)
library(sf)

data_sinim <- get_sinim(var = c(3954,4174,880,1226,4251,4173), 
                        year = 2018, 
                        region = 13,
                        geometry = T,
                        truevalue = T,
                        auc = T,
                        unit = "limites")

gran_santiago_plot <- tm_shape(data_sinim) +
  tm_fill(col = "value",
          palette = "BuPu", 
          border.col = "white", 
          border.alpha = 0.5,
          lwd=1,
          style = "jenks",
          title = "variable")+
  tm_text("municipality", size = 0.4) +
  tm_style("white", frame = T, legend.title.size = 1, legend.width=1) +
  tm_layout(inner.margins = c(0.01, 0.1, 0.1, 0.01),
            outer.margins = c(0.01, 0.01, 0.01, 0.01),
            design.mode=F,
            legend.format = list(text.separator = "a",
                                 fun = mm))+
  tm_borders(col = 'black') +
  tm_facets(by="variable", ncol = 2)

gran_santiago_plot

A variable in multiple years using facets

library(dplyr)
library(sinimr)
library(sf)
library(tmap)


var <- get_sinim(c(880, 882, 1226),
                 2016:2018, 
                 region = 13,
                 truevalue = T, 
                 geometry = T,
                 auc = T,
                 unit = "limites")

gran_santiago_plot <- tm_shape(var) +
  tm_fill("value",
          palette="BuPu", 
          border.col = "white", 
          style = "jenks", 
          border.alpha = 0.5,
          lwd=1) +
  tm_text("municipality", size = 0.4) +
  tm_legend(legend.position = c("left", "top")) +
  tm_layout(legend.width=0.09,
            inner.margins = c(0.01, 0.1, 0.1, 0.01),
            outer.margins = c(0.01, 0.01, 0.01, 0.01),
            legend.format = list(text.separator = "a",
                                 fun = mm)) +
  tm_facets(by=c("year","variable"),) +
  tm_borders(col = 'black')

gran_santiago_plot

Multiple variables and years using geofacet

library(sf)
library(dplyr)
library(geofacet)
library(sinimr)
library(ggplot2)
library(zoo)
library(scales)
library(ggpubr)

data <- get_sinim(882, 2002:2018,
                  region = 13,
                  moncorr = F, 
                  truevalue = T,
                  auc = T)

data$year <- as.numeric(as.character(data$year))
data$year <- as.Date(as.yearmon(data$year, "1-%y"))

reg13 <- geogrid::read_polygons("https://raw.githubusercontent.com/robsalasco/precenso_2016_geojson_chile/master/Extras/GRAN_SANTIAGO.geojson")
grd <- grid_auto(reg13, seed = 1, names = "NOM_COMUNA", codes = "COMUNA")

#grid_preview(grd, label = "name_NOM_COMUNA")
#grid_design(grd, label = "name_NOM_COMUNA")

ggplot(data, aes(year, value, group=1)) +
  geom_line(color = "steelblue") +
  facet_geo(~ municipality, grid = grd, scales = "free_y")+
  scale_x_date() +
  scale_y_continuous(labels = dollar_format(suffix = "", prefix = "$", big.mark = ".", decimal.mark=","))+
  theme_bw()

Citation

citation("sinimr")

References



robsalasco/sinimr documentation built on July 16, 2025, 3:55 p.m.