Substitute, eval, bquote, do.call ... some guidelines for replacing expressions

I would like to program a time series class. The idea is that I create an instance of an object with an expression and some other time series objects, for example

(two time series)

x <- ts(rnorm(10), frequency = 4, start = c(1959, 2))  
y <- ts(rnorm(10), frequency = 4, start = c(1959, 2))  

(time series defined as the sum of x and y)

z <- exprTs("x+y", parents=list(x=x, y=y)) 

(get part of the series)

window(z, start=1960, end=1960.75)

The problem is, how can I evaluate the expression? I tried the following:

#(constructor for class)  
exprTs <- function(expr, parents) {  
  res = list(expr=expr, parents=parents)  
  class(res) <- "exprTs"  
  res  
}  

#(window method)  
window.exprTs <- function(z, ...) {  
  eval(substitute(z$expr, lapply(z$parents, window, ...)))  
  #do.call(z$expr, lapply(z$parents, window, ...))  
}  

I cannot get the window method to work.

If you could guide me on how to use substitute, eval, do.call accordingly, that would be very helpful.

+3
source share
3 answers

, , . , parse:

> "x+y"
[1] "x+y"
> parse(text="x+y")
expression(x+y)
attr(,"srcfile")
<text> 

, apply ?

+5

. ts :

R> set.seed(42)
R> x <- ts(rnorm(10), frequency = 4, start = c(1959, 2))
R> y <- ts(rnorm(10), frequency = 4, start = c(1959, 2))
R> z <- x + y
R> cbind(x,y,z)
               x       y       z
1959 Q2  1.37096  1.3049  2.6758
1959 Q3 -0.56470  2.2866  1.7219
1959 Q4  0.36313 -1.3889 -1.0257
1960 Q1  0.63286 -0.2788  0.3541
1960 Q2  0.40427 -0.1333  0.2709
1960 Q3 -0.10612  0.6360  0.5298
1960 Q4  1.51152 -0.2843  1.2273
1961 Q1 -0.09466 -2.6565 -2.7511
1961 Q2  2.01842 -2.4405 -0.4220
1961 Q3 -0.06271  1.3201  1.2574
R> 

R.

+5

:

window.exprTs <- function(z, ...) {  
  names(z$parents)<-NULL  
  do.call(z$expr, lapply(z$parents, window, ...))  
}  

plus <- function(x, ...) if (nargs() == 1) x else x + Recall(...)  
z <- exprTs(plus, parents=list(x=x, y=y))

window(z, start=1960, end=1960.75) 

.

+1

Source: https://habr.com/ru/post/1729484/


All Articles