I have following two code snippets (the difference is just in names position):
1st:
library(zoo)
vec             <- rep(c(rep(5,10), rep(2,10)), 10)
win.size        <- c(2, 4, 5, 10, 20, 30)
vec.avgs        <- lapply(win.size, function(x) { rollapply(vec, width = x,  by = x,  FUN = mean, align = "left") })
names(vec.avgs) <- win.size
vec.rep         <- lapply(as.character(win.size), function(x) { rep(vec.avgs[[x]], each=x) })
names(vec.rep)  <- win.size
res             <- lapply(as.character(win.size), function(x) { cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) })
2nd:
library(zoo)
vec             <- rep(c(rep(5,10), rep(2,10)), 10)
win.size        <- c(2, 4, 5, 10, 20, 30)
vec.avgs        <- lapply(win.size, function(x) { rollapply(vec, width = x,  by = x,  FUN = mean, align = "left") })
vec.rep         <- lapply(as.character(win.size), function(x) { rep(vec.avgs[[x]], each=x) })
res             <- lapply(as.character(win.size), function(x) { cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) })
names(vec.avgs) <- win.size
names(vec.rep)  <- win.size
1st is working as expected but the 2nd throws:
Error in cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) : 
  supply both 'x' and 'y' or a matrix-like 'x'
when it comes to this line:
res <- lapply(as.character(win.size), function(x) { cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) }) 
The error message is same as if cor were called only with one argument e.g.:
> cor(vec,)
Error in cor(vec, ) : supply both 'x' and 'y' or a matrix-like 'x'
Based on suggestions about debugging apply functions I've set options(error = browser) to see what happens. What is surprising for me is that there is no x variable in function context and I cannot move in code (pressing n quits Browse):
> lapply(as.character(win.size), function(x) { cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) })
Error in cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) : 
  supply both 'x' and 'y' or a matrix-like 'x'
Called from: stop("supply both 'x' and 'y' or a matrix-like 'x'")
Browse[1]> ls()
[1] "vec"      "vec.avgs" "vec.rep"  "win.size"
Browse[1]> x
Error during wrapup: object 'x' not found
Browse[1]> n
> 
Another surprising thing for me is that 1st example requires as.character(win.size) in both lapply functions. So if I rewrite the 1st code without casting win.size to characters:
vec             <- rep(c(rep(5,10), rep(2,10)), 10)
win.size        <- c(2, 4, 5, 10, 20, 30)
vec.avgs        <- lapply(win.size, function(x) { rollapply(vec, width = x,  by = x,  FUN = mean, align = "left") })
names(vec.avgs) <- win.size
vec.rep         <- lapply(win.size, function(x) { rep(vec.avgs[[x]], each=x) })
names(vec.rep)  <- win.size
res             <- lapply(win.size, function(x) { cor(vec[1:length(vec.rep[[x]])], vec.rep[[x]]) })
I get the error:
Error in vec.avgs[[x]] : subscript out of bounds
Called from: FUN(X[[i]], ...)
when it comes to this line line:
vec.rep <- apply(win.size, function(x) { rep(vec.avgs[[x]], each=x) })
Setting options(error = browser) did not helped again and as previously there is no x variable:
> lapply(win.size, function(x) { rep(vec.avgs[[x]], each=x) })
Error in vec.avgs[[x]] : subscript out of bounds
Called from: FUN(X[[i]], ...)
Browse[1]> ls()
[1] "vec"      "vec.avgs" "win.size"
Browse[1]> n
>
Questions:
- Why is 1st example working and 2nd is not?
 - Is 
ls()showing the function context or whole program or where is myxvariable? - Why is 
as.character(win.size)required in 1st example? - How can I debug those errors and why 
nquitsBrowsemode?