Vignette for the nutrientr Package"

knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)
#install.packages("ggplot2")
library(nutrientr)
library(ggplot2)
library(dplyr)
Sys.setenv("CN_API" = "4ed46e31519f0bb5b7a66a5d350a4f1c")

Description

The nutrientr package is an R wrapper for the Canadian Nutrient File (CNF) API. It contains information about commonly consumed foods in Canada. There are eight different endpoints that give access to different types of information.

Getting Started - API Access Key

In order to use the nutrientr package, you will need to get an API key from the Canada Open Data API Store. Once you have this, you can either set is as a system environment variable using Sys.setenv(CN_API = 'your_API_key_here') or pass it as a parameter to the functions.

Function Usage

The nutrientr package contains several function that wrap the CNF API endpoints listed above in the description. The following documentation will give examples of how to use them.

get_food()

The get_food() function retrieves food information from the CNF using the following arguments:

An example of using get_food() to search for the food with food_code as 5 would be as follows:

get_food(foodCode = 5)

Nutrient Information: get_nutrientamount(), get_nutrientgroup(), get_nutrientname() and get_nutrientsource()

The get_nutrientamount(), get_nutrientgroup() , get_nutrientname() and get_nutrientsource() functions returns nutrient amount information from the CNF using the following parameters.

If a user wanted to search and return the all nutrient amount information, get_nutrientamount() could be used, though there is a lot of data (500K lines), so this will take a long time. Instead, we will look for food #2314 (Spinach Souffle):

head(get_nutrientamount(foodCode = 2314))

If you want to find the nutrient group information for nutrient_group_id 7 call get_nutrientgroup():

get_nutrientgroup(nutrientGroupId = 7)

To see all of the nutrient groups, don't provide a nutrient_group_id:

get_nutrientgroup()

If you want to find the nutrient name information for nutrientNameId 550 call get_nutrientname(nutrientNameId = 550):

get_nutrientname(nutrientNameId = 550)

If you want to find the nutrient source information for all nutrient sources, call get_nutrientsource():

head(get_nutrientsource())

If you want to find the nutrient source information for nutrient source number 108 call get_nutrientsource(nutrientSourceId = 108):

get_nutrientsource(nutrientSourceId = 108)

get_refuseamount()

The get_refuseamount() function retrieves refuse amount information from the CNF using the following parameters:

If you want to find the refuse amount information for all foods where this information is available, call get_refuseamount():

head(get_refuseamount())

Serving Size

The get_servingsize() functions all retrieve serving sizes information from the CNF using the following parameters:

If you want to find the serving size information for all foods where this information is available, call get_servingsize():

get_servingsize()

Yield Amount

The get_yieldamount()function retrieves yield information from the CNF using the following arguments:

If you want to find the yield amount information for all foods where this information is available, call get_yieldamount():

head(get_yieldamount())

A Fully Worked Example

Let's say we're super interested in comparing Roquefort Cheese (food code = 46), with Romano Cheese (food code = 45). I know these codes from looking through the full list (calling get_food()), to demonstrate, I'll get the names for these two cheeses using their codes.

#The API doesn't accept multiple IDs at once, so we have to make two queries. 
rbind(get_food(foodCode = 45), get_food(foodCode = 46))

Now that we've confirmed that we have two types of cheese, let's see the serving sizes for both.

rbind(get_servingsize(foodCode = 45), get_servingsize(foodCode = 46))

Cool, now we can convert between different ways to serve these two types of cheese using the base measurement of 50g. Next, we'll check if either have any refuse amounts or corresponding yields. Since they are both cheeses and don't require any preparation, they shouldn't. This means we should get 0 for total refuse, and errors for the yields saying the queries are invalid since there's no info in the database.

get_refuseamount(foodCode = 45)
get_refuseamount(foodCode = 46)
try({
  get_yieldamount(foodCode = 45)
  get_yieldamount(foodCode = 46)
})

Now let's get the nutrition information for both cheeses and compare it.

#The API doesn't accept multiple IDs at once, so we have to make two queries. 
nutrition <- rbind(get_all_nutrient_info(foodCode = 45), get_all_nutrient_info(foodCode = 46))
head(nutrition)

Got it! Now we have ample information to plot. As a simple example of something to look at, let's compare the amounts of the different amino acids in the two cheeses. These are what make up proteins, so they should present in relatively high levels in cheese.

filtered <- nutrition |> dplyr::filter(nutrient_group_name == "Amino Acids")
ggplot(data = filtered, aes(x=nutrient_name, y=nutrient_value, fill = food_description))+
  geom_bar(stat = 'summary', fun = sum, position = "dodge") +
  theme_classic() + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  labs(x = "Amino Acid", y = "Amount (g)", fill = "Cheese")

Awesome! We can see that romano cheese has higher levels of almost all amino acids, particularly glutamic acid.



Try the nutrientr package in your browser

Any scripts or data that you put into this service are public.

nutrientr documentation built on March 18, 2022, 6:27 p.m.