The Junar API is the basis for a number of Open Data initiatives in Latin
America and the USA. The
junr package is a wrapper to make it easier to
access data made public through the Junar API. Some examples of implementations
are listed on the Junar website.
As an example we will use the data from the Costa Rican President's Office.The first step is to access the website offering the open data to identify the base URL and to obtain an API Key to get access to the Junar API that hosts the data. You will find both on the developers page of the Open Data Costa Rica site.
Below we use a test API Key so that all the examples will run. You may want to get your own API Key instead to run the examples below. Note that with Junar each URL has its own API key.
library(junr) base_url <- "http://api.datosabiertos.presidencia.go.cr/api/v2/datastreams/" api_key <- "0bd55e858409eefabc629b28b2e7916361ef20ff"
Now that we have the basic information for a connection we can quickly check what data is available behind this URL.
get_indexfunction returns the complete list of available data with all
meta-data included as a data frame.
To get only a list of the global unique identifiers (GUID) of the data sets,
you can use
You can also make a list of the titles of the data sets:
list_titles where set up for convenience only because
the results tend to fit in the console window making it easier to read. They
are meant to help to get a quick overview of the available data.
You need to know the Global Unique Identifier (GUID) of the data set that you are interested in to be able to download them to your R session. You can look for the GUID on the web page that shows the data of interest. For example on the page for public expenditure of the Costa Rican government there is a table called "Public Purchasing of the Ministry of the Presidency". In the menu underneath the table you have an option to "Obtain GUID". This last option opens a pop-up showing the GUID "COMPR-PUBLI-DEL-MINIS" that we are going to use in the example below.
data_guid <- "COMPR-PUBLI-DEL-MINIS" purchasing_data <- get_data(base_url, api_key, data_guid)
View(purchasing_data) you can check whether the data have been
downloaded correctly, and have a quick visual check on the mode of the data
(see below to convert currency data from text to numeric).
You may note that if you do not need to go to the web interface to get the GUID
for any data sets of interest. With the function
list_guid() as we used it
above, we obtained the same information.
pres_list <-list_guid(base_url, api_key) pres_list
We can get the GUID we are interested in by fetching the third entry in the
list of GUID's (see the full list in the example above). And the same index
numbers can be used with a list of full titles created with
On data platforms that run Junar, many data sets are just tables of data that has already been analyzed and summarized. It is not immediately obvious which sets contain many data points, and which sets contain only a few rows.
get_dimensions will download all data sets offered through the
base URL and determine how many rows and columns are available in each one. It
is useful to make a quick assessment of the data available. However, please
note that it may take a while before the function finishes, especially if there
are many GUID's.
In the example data above, and possibly in more Junar implementations, we need to clean up any data related to currency values. In our case we need to found all currency symbols (Costa Rica Colon) and all the comma's separating thousands. As they stand these values are text strings, and cannot be converted directly to numeric without removing the symbols and commas.
There are two utilities to help cleaning the currency data:
get_currency_symbol. For example:
currency_data <- get_data(base_url, api_key, "LICIT-ADJUD-POR-LOS-MINIS") currency_data$`Monto Adjudicado` <- clean_currency(currency_data$`Monto Adjudicado`)
Any scripts or data that you put into this service are public.
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.