3

I'm working on crop-climate modelling using Robert Hijmans' package 'dismo' and more specifically the function 'ecocrop'. Ecocrop calls a series of 'hidden' functions including, .doEcocrop (ecocrop and .doEcocrop pasted below), which calculate the final output of ecocrop.

.doEcocrop <‐ function(crop, tmin, tavg, prec, rainfed) {
if (rainfed) 

{
nasum <‐ sum(is.na(c(tmin, tavg, prec)))
} else {
nasum <‐ sum(is.na(c(tmin, tavg)))
}
if (nasum > 0) { return( new('ECOCROP')) }
duration <‐ round((crop@GMIN + crop@GMAX) / 60)
tmp <‐ c(crop@TMIN, crop@TOPMN, crop@TOPMX, crop@TMAX)
temp <‐ .getY(tmp, tavg)
ktmp <‐ c(crop@KTMP, crop@KTMP, Inf, Inf)
tmin <‐ .getY(ktmp, tmin‐5)
if (rainfed) {
pre <‐ c(crop@RMIN, crop@ROPMN, crop@ROPMX, crop@RMAX)
shftprec <‐ c(prec[12], prec[‐12])
cumprec <‐ movingFun(prec, n=duration+1, fun=sum, type='from',     circular=TRUE) + shftprec
prec <‐ .getY(pre, cumprec)
allv <‐ cbind(temp, tmin, prec)
} else {
allv <‐ cbind(temp, tmin)
}
minv <‐ apply(allv, 1, min)
obj <‐ new('ECOCROP')
obj@crop <‐ crop
obj@suitability <‐ movingFun(minv, n=duration, fun=min, type='from',    circular=TRUE)
obj@maxsuit <‐ max(obj@suitability)
if (obj@maxsuit > 0) {
obj@maxper <‐ which(obj@suitability==max(obj@suitability))
} else {
obj@maxper <‐ 0
}
return(obj)
}

ecocrop <‐ function(crop, tmin, tavg, prec, rainfed=TRUE, ...) {
if (class(crop) == 'character') {
crop <‐ getCrop(crop)
}
if (missing(prec) & rainfed) {
stop('prec missing while rainfed=TRUE' )
}
if (inherits(tmin, 'Raster')) {
if (nlayers(tmin) != 12) {
stop()
}
.ecoSpat(crop, tmin, tavg, prec, rainfed)
} else {
.doEcocrop(crop=crop, tmin=tmin, tavg=tavg, prec=prec, rainfed=rainfed, ...)
}
}

I was just wondering whether there was a simple method for extracting for example 'allv' and 'minv' from the function, or whether I'd need to redefine them as objects and re-write the function? I've tried this repeatedly, defining both allv and minv as objects, but come up against an error, which I think I've traced back to issues of the environment and/or namespace.

I'm certain there must be a simpler way of doing this, if anyone has any ideas, or Robert if you're out there and could shine a light on which way to go I'd very much appreciate it.

Thanks very much and apologies if I've not not been sufficiently specific.

Arferion
  • 53
  • 4
  • 1
    You could re-write the functions and assign the objects you want to extract with a double arrow `<<-` to make them available globally. However, this is often frowned upon. You could alternatively try returning a list object (see [here](http://stackoverflow.com/questions/1826519/how-to-assign-from-a-function-which-returns-more-than-one-value)). – conrad-mac Feb 14 '17 at 08:06
  • 2
    My vote would be to returning a list. For safety, I would make this into a new function not to break any behavior. – Roman Luštrik Feb 14 '17 at 08:28
  • If that makes sense to have those objects at disposal, why not first ask package owner/maintainer to extend its object definition to include them in additional slots? – Eric Lecoutre Feb 14 '17 at 13:23
  • Thanks very much for the responses. I got this working by re-writing the function as suggested conrad-mac and Roman. – Arferion Feb 26 '17 at 20:29

0 Answers0