View on GitHub

rfdc

An R interface to FoodData Central

Nelson Gonzabato 2021-11-10

rfdc: An R Interface to FoodData Central

R-CMD-check license lifecycle Maintenance Project
Status GitHub last
commit GitHub
issues GitHub
issues-closed PRs
Welcome

Background

This repository is intended to serve as a replacement to the R package usdar. This was necessary because the former API is slated for End of Life as explained in issue #2.

It is also based on pyfdc

Installation

To install the package, one requires to have devtools installed. The package is not on CRAN and the author does not intend to push it to CRAN unless specific need arises.

install.packages("devtools")
devtools::install_github("Nelson-Gon/rfdc")

Loading the package

library(rfdc)
#> Please run set_api_key first.Run key_signup to get an API key.

Initial Setup

As a first step, we need to set an API key for the session via set_api_key as shown below.

set_api_key()
#> AN API key already exists, enjoy!

To signup for an API key:

key_signup()

Initialise an object

To intialise an object, we can use make_object. See details about each class below. For instance, to initialise an object of class FoodSearch with the search term indomie:

my_object <- make_object("FoodSearch",search_phrase="indomie")

Classes

  1. FoodSearch class

This class allows access to the food search endpoint i.e to search/query the database.

Available methods

  1. get_food_info

To get details about our object above:

get_food_info(my_object,"description")
#> [1] "CHICKEN INSTANT NOODLES, CHICKEN"                    
#> [2] "CHICKEN CURRY FLAVOUR INSTANT NOODLES, CHICKEN CURRY"
#> [3] "SPECIAL CHICKEN FLAVOUR INSTANT NOODLES, CHICKEN"

To get multiple details:

get_food_info(my_object,c("description",'fdcId','score'))
#>                                            description   fdcId     score
#> 1                     CHICKEN INSTANT NOODLES, CHICKEN 1816702 -537.7092
#> 2 CHICKEN CURRY FLAVOUR INSTANT NOODLES, CHICKEN CURRY 1987917 -537.7092
#> 3     SPECIAL CHICKEN FLAVOUR INSTANT NOODLES, CHICKEN 1987671 -537.7092

Another example:

head(get_food_info(my_object,c('fdcId','description','score')))
#>     fdcId                                          description     score
#> 1 1816702                     CHICKEN INSTANT NOODLES, CHICKEN -537.7092
#> 2 1987917 CHICKEN CURRY FLAVOUR INSTANT NOODLES, CHICKEN CURRY -537.7092
#> 3 1987671     SPECIAL CHICKEN FLAVOUR INSTANT NOODLES, CHICKEN -537.7092
  1. get_food_details

For the FoodDetails class, this returns details about a food data central id. You can get an fdcId using get_food_info from FoodSearch

test_object <-make_object("FoodDetails",fdc_id = 504905)

get_food_details(test_object, "ingredients")
#> [1] "Using URL: https://api.nal.usda.gov/fdc/v1/food/504905?api_key=*****&nutrients="
#> $ingredients
#> [1] "MECHANICALLY SEPARATED CHICKEN, CHICKEN BROTH, WATER, CONTAINS LESS THAN 2% OF: SALT, SUGAR, SPICES, SODIUM PHOSPHATE, SODIUM ASCORBATE, SODIUM NITRITE, NATURAL FLAVORS, EXTRACTIVES OF PAPRIKA."
  1. get_nutrients

To get nutrients corresponding to a food id:

head(get_nutrients(test_object))
#> [1] "Using URL: https://api.nal.usda.gov/fdc/v1/food/504905?api_key=*****&nutrients="
#>     id number                                     name rank unitName serving
#> 1 1079    291                     Fiber, total dietary 1200        g    0.00
#> 2 1089    303                                 Iron, Fe 5400       mg    0.83
#> 3 1110    324 Vitamin D (D2 + D3), International Units 8650       IU    0.00
#> 4 1087    301                              Calcium, Ca 5300       mg   62.00
#> 5 1092    306                             Potassium, K 5700       mg   54.00
#> 6 1235    539                            Sugars, added 1540        g    0.80
#>                                                       serving_descr
#> 1 Calculated from a daily value percentage per serving size measure
#> 2 Calculated from a daily value percentage per serving size measure
#> 3 Calculated from a daily value percentage per serving size measure
#> 4 Calculated from a daily value percentage per serving size measure
#> 5 Calculated from a daily value percentage per serving size measure
#> 6                    Calculated from value per serving size measure

A get_label_nutrients method for a given Food Data Central can also be used as follows. Unfortunately, it is unclear what the provided values mean nutrition wise.

get_label_nutrients(test_object)
#> [1] "Using URL: https://api.nal.usda.gov/fdc/v1/food/504905?api_key=*****&nutrients="
#>   fat saturatedFat transFat cholesterol sodium carbohydrates fiber sugars
#> 1  15          4.5        0        84.5   1060             2     0      1
#>   protein calcium iron potassium addedSugar calories
#> 1      10    80.6 1.08      70.2       1.04      179

Further exploration is left to the user.

Please note that the ‘rfdc’ project is released with a Contributor Code of Conduct. By contributing to this project, you agree to abide by its terms.