KnitR/Fetching Data from an API

Retrieve Data from an API for R Analysis edit

This learning resource has an additional repository with scripts and emlated API calls for scripts that show how to use a remote API for data retrieval within R. The sources are mainly R but also some small scripts are available to show the basic principles from the Linux shell.

Introduction edit

This repository was establish during the COVID-19 pandemic as a learning resource in Wikiversity about Dynamic Report or Report Generation with KnitR.

Test Usage edit

If you want to perform a test with your R-script to fetch a JSON record from a database API is makes sense to test the programming code if it parses a JSON correctly. * Demo Link for Emulation of Database Query over Web for testing.

The tests encapsule debugging to the processing of a JSON in R. Otherwise you must distinguish if the API call was incorrect and the provided JSON could processed due to the query syntax or the parsing and processing in the R script was not working. The repository provides a correct JSON via a HTTP request and therefore an error can clearly allocated on the scripting of the parser.

R-Script to Fetch Data form API edit

This is the example for R code with the example URL to a JSON data in this repository. You can use this code also as KnitR code chunk.

url <- ""
raw_data_json <- scan(url, "", sep="\n")
my_dataframe <- fromJSON(paste(raw_data_json, collapse = ""))

The code runs out of the box in your R-code, Shiny-WebApp or KnitR code chunk. After testing adapt the URL to your API with the appropriate parameters. The current URL is just for testing the basic functionality of the R code. The JSON is static and just for demo purpose.

Folders in the Repository edit

The following folders are providing tools for data retrieval on the Operating System level or on the application level (e.g. in Open Source R, GNU Octave, Shiny Web-Apps with R Code, KnitR, …

Linux - Shell Scripts edit

The folder linux/ contains Linux scripts that can be used for automatisation of data updates. These scripts are usually added in the crontab for daily or weekly update of data.

R-Code edit

The folder r_code/ contains R scripts that can be used within R/RStudio, in KnitR or in a Shiny Web-App.

Data - Demo Files edit

The data contains demo data with different files types for testing.