To query the DSID API using the R programming language, you can construct a POST request to send data to the API and return results back. This page is intended for users who are already familiar with using R.
There are multiple options for R packages that can send API requests. This example shows how the httr and jsonlite packages can be used to reach the DSID API. To download the full R Code shown in this page, use this link: (dsid_api_R_example_code.R)
Create a new R Script (File → New File → R Script) to type and enter the code for this API Request. First, install and load the httr and jsonlite packages.
Example goal: Estimate calcium levels in adult MVMs using a label amount of 200mg. In your script, create a data frame object that contains the input values.
This “input_data” object now contains the required input fields to pass to the API. Type input_data into the console to see its value.
This input list can be converted to JSON format using the toJSON function from the jsonlite package.
Using your JSON input data and the DSID API URL, you can now send a request with the POST function from the httr package. Be sure to include the additional header functions accept_json() and content_type_json() – these allow the API to know what type of data to expect.
If the request was formatted and went through properly, you should see a successful status code of 200.
To view the result of the API request, use the httr content function and specify to return it as text. Use cat to print the JSON results; to save the result to a JSON file, use the file argument to provide the path to a .json file.
To convert the JSON data back to a data frame, you can use the fromJSON function.
You can continue to work with the results in R or import them into other software. See the Converting JSON Data page for an Excel example.