After collecting and submitting data to Ona Data, you may want to simplify data analysis and empower key decision-makers by importing data into R. This guide will walk you through importing data from the Ona Data API to R and converting it into a DataFrame. We are going to use a data endpoint from OnaData API, which provides access to form submitted data in JSON format. You can read more on OnaData API documentation here.
1. Install Required Packages
Before you connect data to R, ensure that you have installed all the necessary R packages. The necessary R packages required include: devtools, httr and jsonlite. You can install them using the following commands in RStudio:
2. Load Required Libraries
After successfully installating the packages, you need to load the packages into your working environment. Load the required libraries into your R environment using the following commands:
3. Make the API Request
Use the GET function from the httr package to make a request to the API. You can either use basic authentication with the username and password or Token Authentication where you use API key.
- Basic authentication
In this example, we use basic authentication and the data CSV endpoint for the OnaData API, using the URL https://api.ona.io/api/v1/form_id.csv. Replace the placeholderform_id
in the URL and authentication details with your username and password under authenticate as described below. - API Key
In this example, we use Token authentication and the data CSV endpoint for the OnaData API, using the URL https://api.ona.io/api/v1/form_id.csv. Replace the placeholderform_id
in the URL and authentication details with your API key token from your account settings page as described below.
To find the form ID, log into Ona Data and navigate to the form’s overview page. The form ID is the last part of the form’s overview page URL:
In the example below, the red arrow highlights the forms’ URL location, and the red box highlights the form ID. In this case, the form id is 330364.
At this point, you are required to use the API URL and then use basic authentication(username and password) to login and query data. You can use the following command that has the “authenticate()” function where you are required to define your username and password. Replace the placeholder form_id on the URL and authentication details(username and password) with your actual username and password and form_id with your actual form id.
4. Check the Status Code
Check if the API request was successful by verifying the HTTP status code. Use the following code to ensure the status code is 200.
You can continue if the result is a 200 status code as shown in the following screenshot.
5. Convert Response to DataFrame
If the status code is 200, convert the API response to a DataFrame. You can convert the data to a data frame for CSV data using the following commands.
6. Explore and Analyze Your Data
Now that you have your data in a DataFrame (data_frame_data), you can explore, analyze, and visualize the data using various R functions and packages.