# Example workflow of checking taxons affecting Abies alba

    # Load packages
    library("pestr")
    library("dplyr")

# Overview

Assume that you are assessing pest threats to Abies alba – European silver fir. It would be wise to go to EPPO Global Database, type Abies alba in search field and check all the information you need, copy paste them into spreadsheet and make some analyses. On the other hand, you can just use `pestr` package and do everything easily from `R` directly saving time on whole copy-pasting procedures. To make this example easy lets assume that your goal is to check number of taxons and number of particular types (major, minor, etc.) of pests. Therefore steps you would need to make in `R` are presented below.

I assume that you already have eppocodes.sqlite database in your working directory. If not you should download it manually from EPPO Data Services or using `eppo_database_download` function. I also assume that you have basic knowledge of using `dplyr` package and piping `%>%` operator.

## Step I – get valid eppocode for Abies alba

First you need valid eppocode of Abies alba. To do this you will need to use `eppo_names_tables` function and save your results to a variable like in the code example below:

    abies_alba <- eppo_names_tables("Abies alba")

This variable will be our input for next step.

## Step II – query EPPO Data Services

We can now look for all the existing pests of Abies alba that exists in EPPO Data Services. To do this we need to use `eppo_tabeltools_pests` function. There are two options how you can access the information.

### With `eppo_names_tables` output as an argument:

You will need result of `eppo_names_tables` function as first argument and the second one is token – which allows you to connect to EPPO API. You get it by registering to EPPO Data Services which is free of charge. You need to paste your token to `create_eppo_token` function and assign the results to a variable (here we use `eppo_token`) that will be used among all `pestr` functions that connect to EPPO API.

### Querying with valid eppocode (or eppocodes)

`eppo_tabletools_pests` will take 3 arguments with this approach:

- token – a variable that store result of `create_eppo_token`;
- character vector of eppocodes (in this example ABIAL);
- use_raw_codes – logical value `TRUE`

Below code shows this in action:

    ### Firsr create eppo_token variable
    eppo_token <- eppo_create_token("<place your eppo token here>")

    ### For token argument, please use eppo_token
    abies_alba_pests <- eppo_tabletools_pests(token = eppo_token,
                                              raw_eppocodes = "ABIAL",
                                              use_raw_codes = TRUE)

Looking at structure of `abies_alba_pests` variable we see that it is a list containing 2 `data.frame`.

    str(abies_alba_pests)
    #> List of 2
    #>  $ long_table   :'data.frame':   51 obs. of  5 variables:
    #>   ..$ eppocode      : chr [1:51] "ABIAL" "ABIAL" "ABIAL" "ABIAL" ...
    #>   ..$ pests_eppocode: chr [1:51] "MELMME" "MELMMD" "ACLRGL" "ACLRVA" ...
    #>   ..$ idclass       : int [1:51] 10 10 9 9 9 9 9 9 9 9 ...
    #>   ..$ labelclass    : chr [1:51] "Experimental" "Experimental" "Host" "Host" ...
    #>   ..$ fullname      : chr [1:51] "Melampsora medusae (as Abies)" "Melampsora medusae f. sp. deltoidis (as Abies)" "Acleris gloverana (as Abies)" "Acleris variana (as Abies)" ...
    #>  $ compact_table: gropd_df [1 × 2] (S3: grouped_df/tbl_df/tbl/data.frame)
    #>   ..$ eppocode: chr "ABIAL"
    #>   ..$ pests   : chr "Experimental: Melampsora medusae (as Abies), Melampsora medusae f. sp. deltoidis (as Abies); Host: Acleris glov"| __truncated__
    #>   ..- attr(*, "groups")= tibble [1 × 2] (S3: tbl_df/tbl/data.frame)
    #>   .. ..$ eppocode: chr "ABIAL"
    #>   .. ..$ .rows   : list<int> [1:1]
    #>   .. .. ..$ : int 1
    #>   .. .. ..@ ptype: int(0)
    #>   .. ..- attr(*, ".drop")= logi TRUE

The long_table element contains what we actually need for our analyses: pest_eppocode and labelclass columns. Lets start with latter.

## Step III – number of pests type

Pest types are stored in labelclass column. We will use few very basic `R` and `dplyr` package commands to check numbers we are interested in.

    ### First select colums labelclass from long_table element,
    ### and use table to check frequencies
    abies_alba_pests$long_table %>%
      dplyr::select(labelclass) %>%
      table()
    #> labelclass
    #> Experimental         Host   Major host
    #>            2           46            3

    ### Than we can create very simple barplot to visualize number of
    ### species in particular type of pest - experimental, host and major host
    abies_alba_pests$long_table %>%
      dplyr::select(labelclass) %>%
      table() %>%
      barplot(xlab = "Type of pest", ylab = "Number of species",
               col = "#AF0011", ylim = c(0, 30))

## Step IV – Number of taxons

Obtaining number of pests taxons is not very complicated. Since we can use `eppocodes` directly, we can pass whole column to `eppo_tabletools_taxo` function, which will retrieve data on pests taxonomy.

    ### Extract eppocodes of pests
    pests_eppocodes <- abies_alba_pests$long_table %>%
      dplyr::select(pests_eppocode) %>%
      unlist()

Now, we can pass `pest_eppocodes` variable as `raw_eppocodes` argument:

    pests_taxonomy <- eppo_tabletools_taxo(token = eppo_token,
                                           raw_eppocodes = pests_eppocodes,
                                           use_raw_codes = TRUE)

This time we can take a shortcut and use second element of list `compact_table`. We can now check the numbers using `table` function and plot results with simple `barplot`.

    pests_taxonomy$compact_table %>%
      dplyr::select(taxonomy) %>%
      table()
    #> taxonomy
    #> Arthropoda  Chromista      Fungi   Nematoda    Plantae
    #>         34          2         10          1          4

    ### barplot of number of pest species in each major taxonomic group -
    ### in the case of our query: Arthropoda, Chromista, Fungi, Nematoda, Plantae
    pests_taxonomy$compact_table %>%
      dplyr::select(taxonomy) %>%
      table() %>%
       barplot(xlab = "Classification of pest", ylab = "Number of species",
               col = "#AF0011", ylim = c(0, 30))

Now, knowing basics of how functions interacts, you are ready to play with your own workflows and analyses.