Skip to content
This repository was archived by the owner on Mar 25, 2020. It is now read-only.

Basic Usage

Tom Wilson edited this page Apr 20, 2017 · 5 revisions

When the fatsecretR library is loaded, a fatsecret (S4) object will be assigned into the global environment. This object helps to manage essential parameters when making API requests.

> library(fatsecretR)
> ls()
[1] "fatsecret"

> class(fatsecret)
[1] "fatsecret"
attr(,"package")
[1] "fatsecretR"

> slotNames(fatsecret)
[1] "ConsumerKey"     "SharedSecret"    "oauthConsumer"   "RESTURL"        
[5] "httpMethod"      "signatureMethod" "oauthVersion"    "timestamp"      
[9] "nonce"

The object show method will inform you if API keys have been added and when the object was last 'Timestamped' and thus; used in an API request.

> fatsecret

fatsecret S4 object 
 
For more details, please visit https://github.com/wilsontom/fatsecretR/wiki 
 
No API ConsumerKey has been added yet
 
No API SharedSecret has been added yet
 
Last timestamp: 
no timestamp available

Your API keys can be then be added to the object;

> APIkeys(fatsecret, token = "api_token", secret = "api_secret")

> fatsecret
fatsecret S4 object 
 
For more details, please visit https://github.com/wilsontom/fatsecretR/wiki 
 
API ConsumerKey available
 
API SharedSecret available
 
Last timestamp: 
no timestamp available

The main method is 'fatsecretR'. This method will allow you to execute the implemented API requests. The fatsecret developers website; gives an API method reference. The names of API methods are not like-for-like in fatsecretR.

The methods slot in fatsecret gives a description of currently available methods;

> fatsecret@methods
       RESTful                 method       oauth_param signature
1      getFood           foods.search search_expression         1
2    getFoodID               food.get           food_id         1
3 getFoodMonth food_entries.get_month              date         2
4 getFoodEntry       food_entries.get              date         2
5   getProfile            profile.get                           1
6  makeProfile         profile.create           user_id         1

method is the API method as described in the fatsecret API method reference and RESTful is the name of it's implementation in fatsecretR. oauth_param is the name of the parameter which needs specifying for the method. All oauth_param's must be character strings.

For example; if we want to search beer in the fatsecret database this is done by;

> fatsecretR(fatsecret, "getFood", "beer")
    food_id                 name    type                  brand
1     7682                 Beer Generic                     NA
2    37716         Regular Beer Generic                     NA
3   500175             IPA Beer   Brand           Goose Island
4    37718           Light Beer Generic                     NA
5   304375       Brown Ale Beer   Brand           Samuel Adams
6   718178             Pale Ale   Brand          Sierra Nevada
7    54560     Ultra Light Beer   Brand               Michelob
8  1063977   Fat Tire Amber Ale   Brand    New Belgium Brewing
9   251811       Blue Moon Beer   Brand                  Coors
10   82480        Heineken Beer   Brand               Heineken
11  594512                Lager   Brand          Stella Artois
12 7844155         Belgian Beer   Brand       Delirium Tremens
13  304373      Hefeweizen Beer   Brand           Samuel Adams
14  551829         Corona Extra   Brand                 Corona
15 1660947       Obsidian Stout   Brand      Deschutes Brewery
16 1883549                 Beer   Brand              Yuengling
17   82500   Lite Beer (Bottle)   Brand Miller Brewing Company
18 1020320               Porter   Brand          Sierra Nevada
19   82489 Budweiser Light Beer   Brand         Anheuser-Busch
20  927522      Dos Equis Amber   Brand              Dos Equis

Queries with more than word, should be separated with a + sign;

For example;

> fatsecretR(fatsecret, "getFood", "baked+beans")
    food_id                                   name    type             brand
1    38218                            Baked Beans Generic                NA
2    78908              Country Style Baked Beans   Brand       Bush's Best
3   268251                  Homestyle Baked Beans   Brand       Bush's Best
4    55686                   Original Baked Beans   Brand     Bush Brothers
5  2556949                            Baked Beans   Brand             Heinz
6   969766 Baked Beans with Brown Sugar and Bacon   Brand Cattle Drive Gold
7  3271808                   Original Baked Beans   Brand       Bush's Best
8     3213                 Vegetarian Baked Beans Generic                NA
9     3212                            Baked Beans Generic                NA
10 1600212                   BBQ Baked Beans Side   Brand      Smokey Bones
11   82428          Maple Cured Bacon Baked Beans   Brand       Bush's Best
12   68791                   Original Baked Beans   Brand        Van Camp's
13   65828                 Vegetarian Baked Beans   Brand       Bush's Best
14   68371                        BBQ Baked Beans   Brand               KFC
15 4432679        Hickory Brown Sugar Baked Beans   Brand    KC Masterpiece
16   47675                            Baked Beans   Brand       Great Value
17 4945274              Boston's Best Baked Beans   Brand               B&M
18   67655                            Baked Beans   Brand           Hooters
19   68789                  Homestyle Baked Beans   Brand        Van Camp's
20   83982              Boston Recipe Baked Beans   Brand       Bush's Best

The food_id code can then be used to retrieve the nutritional information of a particular item;

fatsecretR(fatsecret, "getFoodID", "38218")
                        id=37545 id=45194 id=60949
serving_description        1 cup     1 oz    100 g
metric_serving_amount    253.000   28.350  100.000
metric_serving_unit            g        g        g
number_of_units            1.000    1.000  100.000
measurement_description      cup       oz        g
calories                     382       43      151
carbohydrate               54.12     6.06    21.39
protein                    14.02     1.57     5.54
fat                        13.03     1.46     5.15
saturated_fat              4.928    0.552    1.948
polyunsaturated_fat        1.872    0.210    0.740
monounsaturated_fat        5.396    0.605    2.133
cholesterol                   13        1        5
sodium                      1068      120      422
potassium                    906      101      358
fiber                       13.9      1.6      5.5
vitamin_a                      0        0        0
vitamin_c                      5        0        2
calcium                       15        2        6
iron                          28        3       11

Clone this wiki locally