PageRenderTime 17ms CodeModel.GetById 12ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

/R/fb_spawning.R

http://fb-scraping-project.googlecode.com/
R | 58 lines | 58 code | 0 blank | 0 comment | 5 complexity | 12bfeb845ef094ed57079e0ac9ddf2bd MD5 | raw file
 1fb_spawning <- function(idFB = NA, StockCode=NA,  Genus = NA, Species = NA, server = 'http://www.fishbase.tw/')
 2  {
 3require("XML")
 4require("stringr")
 5require('RCurl')
 6  if (is.na(idFB) | is.na(StockCode)) 
 7		{
 8		ids <- fb_ids(Genus=Genus, Species=Species, server = server)
 9		idFB <- ids$idFB
10		StockCode <- ids$StockCode
11		}
12  if ((is.na(idFB) | is.na(StockCode)) & (is.na(Genus) | is.na(Species)))
13    {
14    warning("idFB and StockCode or Genus and Specie are required!")
15    }
16	if(!is.na(idFB) & !is.na(StockCode))
17		{
18		url <- paste(server, "Reproduction/SpawningList.php?ID=", idFB, "&StockCode=", 
19                 StockCode, sep = "")
20		s <- readHTMLTable(url, colnames=c('Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 
21                                       'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dic', 
22                                       'Country', 'Locality'))
23		if (is.null(s$dataTable))
24			{
25			diet <- c(as.character(idFB), as.character(StockCode), as.character(Genus), 
26                as.character(Species), t(rep(NA, 14)))
27			names(diet) <-  c('idFB', 'StockCode', 'Genus', 'Species', 'Jan', 'Feb', 'Mar', 
28                        'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dic', 
29                        'Country', 'Locality')
30			}
31		if (!is.null(s$dataTable))
32			{
33			diet <- cbind(as.character(idFB), as.character(StockCode), as.character(Genus), as.character(Species), s$dataTable)
34			names(diet) <-    c('idFB', 'StockCode', 'Genus', 'Species', 'Jan', 'Feb', 'Mar', 
35                        'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dic', 
36                        'Country', 'Locality')
37			diet$idFB <- as.character(diet$idFB)
38    	diet$StockCode <- as.character(diet$StockCode)
39			diet$Genus <- as.character(diet$Genus)
40			diet$Species <- as.character(diet$Species)
41			diet$Jan <- as.character(diet$Jan)
42			diet$Feb <- as.character(diet$Feb)
43			diet$Mar <- as.character(diet$Mar)
44			diet$Apr <- as.character(diet$Apr)
45      diet$May <- as.character(diet$May)
46      diet$Jun <- as.character(diet$Jun)
47      diet$Jul <- as.character(diet$Jul)
48      diet$Aug <- as.character(diet$Aug)
49      diet$Sep <- as.character(diet$Sep)
50      diet$Oct <- as.character(diet$Oct)
51      diet$Nov <- as.character(diet$Nov)
52      diet$Dic <- as.character(diet$Dic)
53      diet$Country <- as.character(diet$Country)
54      diet$Locality <- as.character(diet$Locality)
55			}
56		}
57	return(diet)
58	}