Setup

Install latest ckanR, dplyr and DT.

install.packages("devtools")
devtools::install_github("rstudio/DT")
devtools::install_github("hadley/dplyr")
devtools::install_github("ropensci/ckanr")

Config

Save yourself specifying the URL with each ckanR call by setting a default url.

library(ckanr)
## Loading required package: DBI
ckanr::ckanr_setup(url="http://datos.imss.gob.mx/")

Reading data

Reading the first dataset and the first resource of IMSS.gov.mx:

packages <- ckanr::package_list()
p1 <- ckanr::package_show(packages[[1]])
r1 <- ckanr::resource_show(p1$resources[[1]]$id)
# did that work? print the resulting objects
p1
## <CKAN Package> 76d40fe0-7077-4c2c-9422-ad2f925d316b 
##   Title: PDA 2017
##   Creator/Modified: 2017-04-03T18:26:13 / 2017-04-11T12:38:05
##   Resources (up to 5): PDA 2017 (31-Enero)
##   Tags (up to 5): 
##   Groups (up to 5): group/poblacion-derechohabiente-adscrita-pda
r1
## <CKAN Resource> 41753dbd-bcf4-4307-9ad4-39a420a05ff5 
##   Name: PDA 2017 (31-Enero)
##   Description: <p>Información al 31 de Enero, población derechohabiente adscrita.</p>
##   Creator/Modified: Lun, 04/03/2017 - 18:27 / Date changed   Lun, 04/03/2017 - 18:29
##   Size: 108.68 MB
##   Format: csv

Using the data retrieved by ckanR, you can now view the first dataset or download its first resource.

Reading a specific dataset:

p <- ckanr::package_show("asegurados-2013")
r <- ckanr::resource_show("57f8e388-7cad-4105-a5b0-6eae49f1e96e")

You can now read data from the package asegurados-2013, e.g. resource Asegurados 2013 (31-enero) into R. Note: As the CSV file is 259.74 MB, this may take a while, so we only read in the first 100 lines.

data <- read.csv(r$url, header=T, as.is=T, nrows = 100, sep="|")
dplyr::glimpse(data)
## Observations: 100
## Variables: 29
## $ cve_delegacion     <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ cve_subdelegacion  <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ cve_entidad        <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ cve_municipio      <chr> "A01", "A01", "A01", "A01", "A01", "A01", "...
## $ sector_economico_1 <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ sector_economico_2 <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ sector_economico_4 <int> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ tamaño_patron      <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ sexo               <int> 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1...
## $ rango_edad         <chr> "E1", "E10", "E11", "E12", "E13", "E14", "E...
## $ rango_salarial     <chr> NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,...
## $ asegurados         <int> 204, 381, 232, 141, 126, 206, 181, 155, 60,...
## $ no_trabajadores    <int> 204, 381, 232, 141, 126, 206, 181, 155, 60,...
## $ ta                 <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ teu                <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tec                <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tpu                <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tpc                <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ ta_sal             <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ teu_sal            <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tec_sal            <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tpu_sal            <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ tpc_sal            <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ masa_sal_ta        <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ masa_sal_teu       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ masa_sal_tec       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ masa_sal_tpu       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ masa_sal_tpc       <dbl> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
## $ patrones           <int> 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0...
DT::datatable(data)