Note where the error is coming from and step through your code. Its not even getting to the Savitsky-Golay line.
> v = as.vector(r)
> z = na.spline(v)
> s1.ts2 = ts(z, start=1, end=nlayers(z), frequency=12)
Error in (function (classes, fdef, mtable) :
unable to find an inherited method for function ‘nlayers’ for signature ‘"numeric"’
The error ...