1. Home
  2. Analyzing Data
  3. Data Visualization
  4. How to: Get Data into R
  1. Home
  2. Analyzing Data
  3. How to: Get Data into R

How to: Get Data into R

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. 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 placeholder URL and authentication details with your actual username and password 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:

https://ona.io/<account_username>/<project_id>/<form_id>

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.

Related Articles

Need Support?
Can't find the answer you're looking for?
Contact Support