Alphacast R Package

Alphacast has an API for downloading and uploading data, creating and viewing repositories, and obtaining general information about datasets and other aspects of the site. The Alphacast R package provides an easy way to interact with it from the R programming language.

Installation

To install the package, you can choose the Github method (soon in CRAN!) using the following line of code:

devtools::install_github(“alphacastio/alphacast-r”)

Additionally, if you do not have the "dplyr", "httr" and "reshape2" packages installed, it would be desirable to install them at this time for the correct functioning of the Alphacast library.

Install_packages(c(“dplyr”, “httr”, “reshape2”))

After that, you must load the Alphacast package in your R Environment:

library(Alphacast)

Main features for getting data

Within the main functions of the library, the most important feature is to download datasets. The package creates a quick interaction with the API through a "request" function to obtain the dataset (which can be saved in data frame format to analyze it, graph it, save it or whatever you want). To do this, you need to know the dataset id and your Alphacast API Key (which you can get from the "Settings" menu on the site's frontend). For example, if you want to get the dataset of mean remunerations from the Ministry of Labor:

alphacast_api_key <- “YOUR_ALPHACAST_API_KEY”
remunerations <- get_dataframe(6228, alphacast_api_key)

This is very useful for automating data download processes from R, but it can be tricky if the dataset id is not known. Therefore, the library incorporates a function to obtain an index with all the datasets available in Alphacast with your access level recognized with the API key. All that is needed is a valid API key.

datasets <- get_datasets(alphacast_api_key)

It is also possible that you want to know the dataset through the repository in which it is loaded. With a simple function, you can get the name, id and description of all the repositories to which you have access.

repos <- get_repos(alphacast_api_key)

To see the datasets of a particular repo (in this case, the "Argentina Macro Basics" repo with repo_id = 20), proceed with the following function.

arg_macro_basics <- get_repos_datasets(20, alphacast_api_key)

Finally, it is possible to incorporate a simple way to obtain a dataset without knowing its ID or its repo, we call it "get_lucky_dataset". In this way, with a vector of words (any quantity), a dataset can be obtained in which there is the most precise match between these words and the name of the dataset. In this example, we are going to get the "Construya Index" in LONG format.

lucky_words <- c("argentina", "construya", "Long")
construya <- get_lucky_dataset(lucky_words,
                               alphacast_api_key = alphacast_api_key)

Creating repos and datasets

To upload a dataset in .csv format to Alphacast, a repository must first be created using the create_repo function. It is also possible to use a repo created from the frontend, taking the repo_id from its URL.

create_repo("Name of the Repo", "Description of the Repo", slug = "short-name-of-repo",
            alphacast_api_key = alphacast_api_key)

The above function returns the state of the newly created repo. It is necessary to save the repo_id from this summary to later upload the dataset. For example, if the repo_id is "999":

repo_id <- 999

Once the repo has been created, the .csv file is uploaded to the platform using the function created for this purpose. The file must meet certain requirements. First, there must be two columns called "country" and "Date" (case sensitive), with country or geographic location information and dates in YYYY-MM-DD format, respectively. The information must be presented in WIDE format, so there should not be duplicate dates. However, all these requirements are dynamic and we are working to accept other formats, which will be informed in due course.

create_dataset("PATH_TO_THE_FILE.csv", "Name of the Dataset”, repo_id, alphacast_api_key)

If the process was successful, the function will notify you once the data upload to Alphacast is complete.

Luciano Cohan

Written by

Luciano Cohan

Co-Fundador de Alphacast. Ex Subsecretario de Programación Macroeconómica. Data Science. Creando una plataforma para el trabajo colaborativo en economías

Discover the new "Exploring Alphacast" Repository here https://www.alphacast.io/repositories/1862/insights

Alphacast

Part of

Alphacast

Related insights