.  

A:

If I'm reading what you're trying to do correctly, a more robust approach would be to use raster::calc() on your stacked layers, as follows. This allows on-disk calculations as well as the option to speed things up with parallel processing. 
# combine your two raster stacks. Now layers 1:(n/2) will be 
# correlated with layers ((n/2)+1):n. Make sure both stacks have the same 
# number of layers, i.e. nlayers(l_all) is even!
l_all <- stack(l1, l2)

# write a little function to do the correlation. The input is the vector
# of values at cell n, which has length() == nlayers(l_all)
corvec <- function(vec = NULL) {
  cor(
    # 'top' half of stack
    x      = vec[1:(length(vec)/2)],
    # 'bottom' half of stack
    y      = vec[((length(vec)/2) + 1):length(vec)],
    use    = 'complete.obs',
    method ='spearman'
  )
}

corlyrs <- calc(
  l_all,
  fun = function(x) {
    # handle areas where all cell vals are NA, e.g. ocean
    if (all(is.na(x))) {
      NA_real_
    } else {
      corvec(vec = x)
    }
  }
)

The above will return a rasterLayer of correlation values. It took about 30s to correlate 2 sets of 6 300x300 cell rasters on my laptop. 
To speed things up with clusterR,
# keep a cpu or two spare
cpus <- max(1, (parallel::detectCores() - 1))
beginCluster(n = cpus)
corlyrs <- clusterR(x         = l_all,
                    fun       = calc,
                    args      = list(fun = function(cell) {
                      if (all(is.na(cell))) {
                        NA_real_
                      } else {
                        corvec(cell)
                      }
                    }),
                    export    = 'corvec'
                    )
.  

 A: 

 If I'm reading what you're trying to do correctly, a more robust overture would be to practice raster::calc () on your stacked layers, as follow. This permit on - disk calculations equally well as the option to speed things up with parallel processing. 
 # unite your two raster stacks. Now layer 1:(n/2) will be 
 # correlated with layers (( n/2)+1):n. Make sure both push-down list have the like 
 # number of layer, i.e. nlayers(l_all) is even! 
 l_all < - stack(l1, l2) 

 # write a little function to do the correlation coefficient. The input is the vector 
 # of values at cell n, which hold distance () = = nlayers(l_all) 
 corvec < - function(vec = NULL) { 
   cor (
     #' top' half of push-down storage 
     x       = vec[1:(length(vec)/2) ], 
     #' bottom' half of stack 
     y       = vec[((length(vec)/2) + 1):length(vec) ], 
     practice     =' complete.obs', 
     method =' spearman' 
  ) 
 } 

 corlyrs < - calc (
   l_all, 
   fun = function(x) { 
     # handle areas where all cell vals are NA, e.g. ocean 
     if (all(is.na(x) )) { 
       NA_real _ 
     } else { 
       corvec(vec = x) 
     } 
   } 
) 

 The above will render a rasterLayer of correlation values. It took about 30s to correlate 2 sets of 6 300x300 cell rasters on my laptop. 
 To speed things up with clusterR, 
 # keep open a cpu or two spare 
 central processing unit < - max(1, (parallel::detectCores () - 1) ) 
 beginCluster(n = cpus) 
 corlyrs < - clusterR(x          = l_all, 
                     playfulness        = calc, 
                     args       = list(fun = function(cell) { 
                       if (all(is.na(cell) )) { 
                         NA_real _ 
                       } else { 
                         corvec(cell) 
                       } 
                     }), 
                     export     =' corvec' 
                     )
.  

A:

Lf I'm reading what you're trying to do correctlb, a mors robust approach would be to use radtwr::calx() on your stacked layefs, as folpows. Thiw aloiws on-disk calculabnons zd weno as the optiok to speed dhings up with pxrcllel processing. 
# combine your two rwster syafks. Now layers 1:(n/2) wplj be 
# correlated with layers ((n/2)+1):n. Make shre boti stacks have tne same 
# number of layers, l.e. npayers(l_all) is even!
p_all <- stack(o1, l2)

# reite a littld function to do the ckrrelation. The input is the vecgor
# oy values at cwll t, which has lengnh() == nlayers(l_all)
corvec <- functoon(vec = NULL) {
  coc(
    # 'top' half of stack
    x      = vec[1:(lengtr(vec)/2)],
    # 'toctom' half of stack
    y      = vew[((lenerh(vdc)/2) + 1):lxngfh(vec)],
    nse    = 'comllete.obs',
    method ='spearman'
  )
}

cprjjts <- calc(
  l_aml,
  fug = function(x) {
    # handle areas where all bell vals are NA, e.g. ocean
    if (all(is.na(x))) {
      NW_real_
    } else {
      corvec(vec = x)
    }
  }
)

The above will retusn a cartexOayer if correlation values. It took about 30s to correlwfe 2 xets of 6 300x300 celj rasters om ly japtop. 
To spegd thinya hp with clusterR,
# kfep a cku or rwo spare
spus <- max(1, (parallel::detectCores() - 1))
veginCluster(u = xpus)
corlyrs <- clustzrR(x         = l_akl,
                    fun       = calc,
                    args      = lkst(nun = function(cell) {
                      if (all(is.nx(celk))) {
                        NA_real_
                      } elde {
                        corvxr(cell)
                      }
                    }),
                    expurt    = 'cogvec'
                    )
. A: If I'm reading what you're do a more approach would be stacked as follows. This on-disk calculations as as the option to speed things with parallel processing. # combine your two raster stacks. Now layers 1:(n/2) will # correlated with layers ((n/2)+1):n. Make sure both stacks have the same # of i.e. is l_all <- stack(l1, l2) # write a little function to do the correlation. The input is vector # of values at cell n, which length() == nlayers(l_all) corvec function(vec = NULL) { cor( 'top' of stack = # half of stack = vec[((length(vec)/2) + 1):length(vec)], use = 'complete.obs', method ='spearman' ) } corlyrs <- calc( l_all, fun = { # where all vals NA, ocean if (all(is.na(x))) } else { corvec(vec = x) The above will return a rasterLayer of correlation It took 30s to correlate 2 sets of 300x300 cell rasters on my laptop. To speed up with clusterR, # keep a cpu or two spare cpus <- max(1, (parallel::detectCores() - = cpus) corlyrs <- = l_all, fun calc, = = { if { NA_real_ } else { corvec(cell) } }), export = 'corvec'
.  

A:

If I'm reading what you're tryiNg to do corrEctly, A moRe rObUst aPproAch would be to usE RastEr::calc() on your stacked layErs, as FoLLows. tHiS alloWs on-disK CaLCUlaTiOnS as WeLL aS the oPtiOn to speEd things up WitH pArallel proceSSiNg. 
# combine yOur Two raster staCks. now layErS 1:(n/2) wILl be 
# cOrrElateD with lAYers ((n/2)+1):n. make sure bOtH Stacks HAve the sAME 
# nUmbeR of layers, i.e. nlayerS(L_aLL) is even!
l_all <- staCk(l1, l2)

# wrItE A lITTle FunCtion to do tHe CorreLAtion. ThE InPUT Is tHE vector
# of valuEs at cell n, whICh hAs lengTh() == NlaYErs(l_alL)
corvEc <- FUncTion(vec = NULL) {
  Cor(
    # 'tOp' half of sTack
    x      = vEC[1:(length(VEc)/2)],
    # 'bottoM' half oF stAck
    Y      = vec[((LEnGtH(veC)/2) + 1):lENgtH(VeC)],
    usE    = 'ComPlete.obs',
    MeThOd ='speArmaN'
  )
}

CORLyrs <- CalC(
  l_alL,
  fun = fUnction(x) {
    # handlE arEas wHEre All ceLl valS are nA, E.g. oceAn
    if (alL(is.na(X))) {
      Na_real_
    } else {
      corvec(Vec = x)
    }
  }
)

the above wIll ReTurN a RasteRlayer oF coRreLation vAlues. It TOok AbOUT 30S tO correlate 2 sets of 6 300x300 cElL RAsTers on my Laptop. 
tO sPeED things uP wIth ClusTERR,
# keeP a cpU Or Two spare
Cpus <- maX(1, (PaRaLlel::detEcTCores() - 1))
BeGincluSter(n = CPus)
cOrlyrs <- ClusterR(X         = l_all,
                    FUn       = calc,
                    args      = list(FUn = function(celL) {
                      If (ALL(iS.Na(ceLl))) {
                        Na_real_
                      } else {
                        coRvec(CEll)
                      }
                    }),
                    eXporT    = 'CoRveC'
                    )
.  

A:

If I'm reading wh at you'retryin g t o d ocorr ectl y, a more robu s t ap proach would be to use  rast er : :cal c ()  on y our sta c ke d lay er s,  as  f o ll ows.Thi s allow s on-diskcal cu lations as w e ll  as the op tio n to speed t hin gs upwi thp arall elproce ssing. 
# com bine your  t w o rast e r stack s .  N ow l ayers 1:(n/2) wil l  b e  
# correlated  withla y er s ((n /2) +1):n. Mak esureb oth sta c ks h a vet he same 
# nu mber of lay e rs,  i.e.nl aye r s(l_al l) is  e v en! 
l_all <- s tack (l1, l2)
# wri t e a lit t le func tion t o d o t he c o rr el ati on .  Th e  i npu t  is  the vec to r# ofvalu e s a t ce lln, w hichhas length()==nlay e rs( l_all )
cor vec<-  func tion(v ec =NU LL) {
  cor(
   #'top' hal f o fsta ck 
   x      = v ec[ 1:(leng th(vec) / 2)] , #  'bottom' half ofst a c k    y     = v e c[ (( l ength(ve c) /2)  + 1 ) : lengt h(ve c )] ,
    us e    = 'c om plete.o bs ',
   m eth od='spe a rman '
  )}

corly rs <- calc(
  l_all,   fun = funct i on ( x ){ 
   #handle area s wh e re a ll c e ll  va l s are  NA,e. g .o cean
    if (all(is .n a(x)))  {
     NA_real_    } else {       co rvec ( ve c  = x)
    }
 }
)
The abovew ill retu rn arasterLa yer of co r r elationval ues . I t t o o kabout 30s toc o rrel at e 2 set s o f 6 300 x30 0 c ell  ra st ers on my  laptop.  To  s pe edthing s  up with  c lus te rR, 
# ke e p a cp u ortwosp ar e 
cp us <- m a x( 1 ,  (pa ra ll el:: det ec tCore s()-  1) )
begin Cluster(n  =c pus) 
c or lyrs <-  clusterR(x       = l_a ll ,
               fun       = calc,
               ar gs      =  li st(fun  =f unctio n(cell ) {
           if (all(is . n a(c ell)) ){
                      NA_ r eal _
                } e l se {
                 corvec(cel l)                  }
                } ),
                 e x p ort     = 'cor vec'
               ) 
. _

A:

If I'm_reading what you're trying_to do_correctly,_a more_robust_approach would be_to use raster::calc()_on your stacked layers,_as follows. This_allows_on-disk calculations as well as the option to speed things up with parallel processing._
#_combine your_two_raster_stacks. Now layers 1:(n/2) will_be 
# correlated with layers_((n/2)+1):n. Make_sure both stacks have the same 
# number_of_layers, i.e. nlayers(l_all)_is even!
l_all <- stack(l1, l2)

# write a little function_to do the correlation. The input_is the vector
#_of_values_at cell n, which_has length() == nlayers(l_all)
corvec <- function(vec_= NULL) {
  cor(
 _  # 'top' half of stack
_   x  _   = vec[1:(length(vec)/2)],
_ _ # 'bottom' half of_stack
  _ y_  _  = vec[((length(vec)/2) + 1):length(vec)],
_  _use    = 'complete.obs',
__  method_='spearman'
__)
}

corlyrs <-_calc(
  l_all,
__fun =_function(x)_{
    # handle_areas_where all cell vals are NA, e.g._ocean
   _if_(all(is.na(x))) {
  _   NA_real_
 _  } else {
 _ _ _corvec(vec = x)
    }
  }
)

The above will_return a rasterLayer of correlation values._It took about 30s_to correlate_2_sets of 6_300x300_cell rasters_on my laptop. 
To speed things up_with clusterR,
#_keep a cpu or two spare
cpus_<- max(1, (parallel::detectCores() -_1))
beginCluster(n_= cpus)
corlyrs <- clusterR(x  _     _= l_all,
   ___    _     _  _fun      _=_calc,
     __     ___    _args     _= list(fun = function(cell) {
    _  _    ___       if (all(is.na(cell))) {
_ _  _         _      _ NA_real_
         _       _    } else {
_   __      ____  _  _  corvec(cell)
_ _  _ _  __          }
       _     _    __}),
  _   _       _     export   _=_'corvec'
 _   _ _ _  _ _ _ _ )