Function Repository Resource:

NutritionLabelData

Source Notebook

Retrieve the nutrient content and daily value percentages of foods

Contributed by: Gay Wilson

ResourceFunction["NutritionLabelData"][amt, food]

yields nutrient data as associations for a given amount of food.

Details

ResourceFunction["NutritionLabelData"] provides data from a nutrition label in a computable format.
The first argument can be a text string or an EntityInstance of an implicit food entity from the Wolfram Language.
ResourceFunction["NutritionLabelData"] supports the following options:
"ExcludeZeroValues"Falseexclude properties with value of 0
"OutputType"Associationkeyvalue
"Rounding"Truerounds to the nearest integer
The output type "Dataset" also is supported.

Examples

Basic Examples (1) 

Enter a text string with a food and amount, then easily retrieve the nutrient data as associations:

In[1]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["3oz beef"]
Out[1]=

Scope (1) 

Instead of a text string, use EntityInstance from the Wolfram Language:

In[2]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"][
 EntityInstance[Entity[
  "Food", {EntityProperty["Food", "FoodType"] -> ContainsExactly[{
Entity["FoodType", "Beef"]}], EntityProperty["Food", "AddedFoodTypes"] -> ContainsExactly[{}]}],
   Quantity[3, "Ounces"]]]
Out[2]=

Options (3) 

Specify the Rounding option as False for more precise values:

In[3]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["2 cups spinach", "Rounding" -> False]
Out[3]=

Also specify the ExcludeZeroValues option as True to exclude properties with a value of 0:

In[4]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["2 cups spinach", "Rounding" -> False, "ExcludeZeroValues" -> True]
Out[4]=

Retrieve a dataset instead of associations:

In[5]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["1/2 cup ricotta cheese", "OutputType" -> "Dataset"]
Out[5]=

Possible Issues (3) 

A message will be generated when a food or non-food item is not found in the database:

In[6]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["3oz penguin"]

There is limited data via NutritionLabelData for some fast-food branded items:

In[7]:=
ResourceFunction[
 "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["hamburger happy meal"]
Out[7]=

The nutrition data per typical serving size can be retrieved for these fast-food items via the EntityValue function:

In[8]:=
Interpreter["Food"]["hamburger happy meal"]
Out[8]=
In[9]:=
entityData = DeleteMissing[
   EntityValue[Entity["Food", "McDonaldsHamburgerHappyMeal::181qzm"], "Association"]];
KeySelect[entityData, StringContainsQ[ToString[#], "Serving", IgnoreCase -> True] &] // N
Out[10]=

Neat Examples (2) 

Create a bar chart to visualize the Daily Value percentages:

In[11]:=
tofuData = ResourceFunction[
   "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["5oz tofu"];
tofuValuePercents = KeySelect[tofuData, StringContainsQ[ToString[#], "Percent", IgnoreCase -> True] &];
chartLabels = StringTrim /@ Map[StringDelete["DailyValuePercent"], Keys[tofuValuePercents]];
BarChart[tofuValuePercents, PlotLabel -> "Percent of Daily Value Provided by 5oz Tofu", ChartLabels -> Placed[chartLabels, Below, Rotate[#, Pi/2] &], AxesLabel -> "%"]
Out[12]=

Reverse sort and customize the style of the chart:

In[13]:=
tofuData = ResourceFunction[
   "NutritionLabelData", ResourceSystemBase -> "https://www.wolframcloud.com/obj/resourcesystem/api/1.0"]["5oz tofu"];
tofuValuePercents = KeySelect[tofuData, StringContainsQ[ToString[#], "Percent", IgnoreCase -> True] &];
sortedTofuValuePercents = ReverseSortBy[Normal[tofuValuePercents], Last];
chartLabels = StringTrim /@ Map[StringDelete["DailyValuePercent"], sortedTofuValuePercents[[All, 1]]];
chartValues = sortedTofuValuePercents[[All, 2]];
BarChart[chartValues,
 PlotLabel -> "Percent of Daily Value Provided by 5oz Tofu",
 ChartLabels -> Placed[chartLabels, Below, Rotate[#, Pi/2] &],
 ChartStyle -> "FallColors",
 AxesLabel -> "%"]
Out[14]=

Requirements

Wolfram Language 13.0 (December 2021) or above

Version History

  • 1.0.0 – 15 April 2024

Related Resources

License Information