[Back]
Calling the WoRMS webservice from R
This example script was written in R version 3.4.4, for newer/older versions just try and see if it works (
https://www.r-project.org/).
Available for MacOS X, Linux & Windows.
Paste the following code in the R Console (this is just an example)
See all available functions at https://marinespecies.org./rest. Just click on the function you want to explore and "try it out"!
# Install the required packages (comment if needed)
install.packages("jsonlite", repos="http://cran.r-project.org")
install.packages("httr")
# Use the libraries
library(jsonlite) #https://cran.r-project.org/web/packages/jsonlite/
library(httr)
# Fill in the AphiaID you need
AphiaID <- 127160
# Build the URL to get the data from
url <- sprintf("https://marinespecies.org./rest/AphiaClassificationByAphiaID/%d", AphiaID);
# Get the actual data from the URL
classificationTree <- fromJSON(url)
# Walk the classification tree
currentTreeItem = classificationTree
while (!is.null(currentTreeItem )) {
print(sprintf("ID: %d, RANK: %s, ScientificName: %s",
currentTreeItem$AphiaID,
currentTreeItem$rank,
currentTreeItem$scientificname
));
# You can access the variables individually
# print(currentTreeItem$AphiaID);
# print(currentTreeItem$scientificname);
# print(currentTreeItem$rank);
# Get next item in the tree
currentTreeItem <- currentTreeItem$child;
}
Download this
example.
Download
elaborate example (match names).
Note there is a complete R package available that you can use. See the rOpenScilibrary 'worrms', available here: https://github.com/ropensci/worrms