RAMS logo
Introduction | Species lists | Search taxa | Taxon tree | Literature | Distributions | Statistics | Editors | Match taxa | Webservice | Log in
[Back]

Calling the RAMS 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 (See http://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 http://www.marinespecies.org/rams/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("http://www.marinespecies.org/rams/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 rOpenSci library 'worrms', available here: https://github.com/ropensci/worrms


Website and databases developed and hosted by VLIZ · Page generated 2018-10-22 GMT · contact: Anton Van de Putte