RStudio crashes when opening stan scripts

I’ve suddenly encountered a mysterious problem opening stan scripts in RStudio on a linux cluster. Opening scripts or clicking on tabs for scripts already open in RStudio crashes the R session. Note: this is before running anything; the session crashes just by opening the script. Creating a new script works fine, and if I fill it with a bunch of nonsene code (for instance repeating the default example code to about 1000 rows), it still works. But if I paste in the same stan code as in one of the scripts that made the session crash, it crashes again. It works fine to open the script in RStudio on my Windows PC. I am lost. Screen dump of the crash and one example of code that crashes the session below. Any suggestions/insights?

Version info:
R version 3.6.3 (2020-02-29)
Platform: x86_64-pc-linux-gnu (64-bit)
Running under: CentOS Linux 7 (Core)

data {
  int<lower=1> NrOfLans;
  int<lower=1> NrOfKretsar;
  int<lower=1> Reports;
  int<lower=0> leftout;
  int<lower=0> K[Reports];
  real<lower=0> A[Reports];
  real logrelA[Reports];
  int<lower=1> KretsNrSeqID[Reports];
  int<lower=1> LansSeqID[NrOfLans];
  int<lower=1> LanSeqID[Reports];
  int<lower=1> KretsarSeqID[NrOfKretsar];
  int<lower=1> KretsListLanSeqID[NrOfKretsar];
  int<lower=0, upper=1> includealpha;//Boolean declaration
  
  int<lower=0, upper=1> includegamma;//
  int<lower=0, upper=1> includephi;//
  int<lower=0, upper=1> AnalyzeWAICandLOO;
  int<lower=0, upper=1> doexactloo;
  
  
}

parameters {
  
  real lambda_raw[NrOfLans];//County effect on average bagging rate
  real chi_raw[NrOfKretsar];//for reparimeterization of chi
  real omega; //Nationwide effect on average bagging rate
  real upsilon[includealpha ? 1 : 0];//Nationwide effect on alpha
  real phi[includephi ? 1 : 0];//effect of hunting area on er area bagging rate
  real<lower=0> sigma;//standard deviation of county effects on average bagging rate
  real<lower=0> tao;//standard deviation of county level effects on average bagging rate
  real gamma[includegamma ? 1 : 0];////effect of bagging intensity on alpha
}

transformed parameters {
  real<lower=0> alpha[includealpha ? NrOfKretsar : 0];// Shape parameter of the Gamma-Poisson
  
  real<lower=0> mu[NrOfKretsar]; //Mean bagging rate in circuit
  real chi[NrOfKretsar];  //County level effect on average bagging rate
  real lambda[NrOfLans];  //County level effect on average bagging rate
  
  for (ik in 1:NrOfKretsar){
    chi[ik]=chi_raw[ik] * sigma;// reparametrisation of chi for improved computation
  }
  
  for (il in 1:NrOfLans){
    lambda[il]=lambda_raw[il] * tao;// reparametrisation of lambda for improved computation
  }
  
  
  for (ik in 1:NrOfKretsar){
    
    mu[ik] = exp( omega + lambda[KretsListLanSeqID[ik]] + chi[ik] );
    
    
    if (includealpha ){
      if (includegamma) {
        
        
          
        
          
          alpha[ik] = exp(upsilon[1] + gamma[1]*(lambda[KretsListLanSeqID[ik]] + chi[ik]));
          
        
        
        
      }else{
        
        
          
          alpha[ik] = exp(upsilon[1]);
          
        
        
        
        
      }
    }

    
  }

  
  
}
model{
  
  for (r in 1:Reports){
    if (r!=leftout){
    
    // Number of killed animals in report r is negative binomial distributed,
    // but defined from the mean (m[KretsNrSeqID[r]] * A[r]) and shape (c[Lan[r]]) of the gamma distribution of the equivalent gamma-poisson mixture
    if (includealpha ){
      if (includephi){
        
        K[r] ~ neg_binomial_2(mu[KretsNrSeqID[r]] * A[r] * exp(logrelA[r] * phi[1]) ,alpha[KretsNrSeqID[r]]); 
        
      }else{
        K[r] ~ neg_binomial_2(mu[KretsNrSeqID[r]] * A[r] ,alpha[KretsNrSeqID[r]]);
      }
      
    } else {
      if (includephi){
        K[r] ~ poisson(mu[KretsNrSeqID[r]] * A[r] * exp(logrelA[r] * phi[1])); 
      }else{
        K[r] ~ poisson(mu[KretsNrSeqID[r]] * A[r]); 
        
      }
      
    }
    }
    
  }
  
  for(ik in 1:NrOfKretsar){
    
    
    
    chi_raw[ik] ~ normal( 0 , 1 ); //  reparametrisation for improved computation
    
  }
  for (il in 1:NrOfLans){
    lambda_raw[il] ~ normal(0,1);//  reparametrisation for improved computation
    
   
    


    
    
  }
  
  omega ~ normal(-8.7,4.3);
  if (includealpha){
    upsilon[1] ~ normal(0,3.0);//based on 95% coef var between 0.1 and 10

    
  }
  
  if (includegamma){
    
    gamma[1] ~ normal(0,1); //Based on being ~95% sure that shap parameter changes less than four times as high or a quarter as low alpha with twice the intensity. Same as CoV doubling or halfing.
    
  }
  
  if (includephi){
    phi[1] ~ normal(0,0.5);// based on 95% between -0.585 and 0.585, which corresponds to maximum 300% increas in hunting rate with twice the area
    
    
    
  }
  
  tao ~ lognormal(0.20,2.3);
  
  sigma ~ lognormal(-1.3,0.86);
  
}

generated quantities {
  vector[doexactloo ? 1 : 0] log_lik_exact;
  vector[AnalyzeWAICandLOO ? Reports : 0] log_lik; // can't be computed if doexactloo
  
   if (doexactloo){
  
    
    
    
    
    if (includealpha){
      if (includephi){
        log_lik_exact[1] = neg_binomial_2_lpmf(K[leftout] | mu[KretsNrSeqID[leftout]] * A[leftout] * exp(logrelA[leftout] * phi[1]), alpha[KretsNrSeqID[leftout]]);
      }else{
        log_lik_exact[1] = neg_binomial_2_lpmf(K[leftout] | mu[KretsNrSeqID[leftout]] * A[leftout], alpha[KretsNrSeqID[leftout]]);
        
      }
    } else {
      if (includephi){
        
        log_lik_exact[1] = poisson_lpmf(K[leftout] | mu[KretsNrSeqID[leftout]] * A[leftout] * exp(logrelA[leftout] * phi[1]));
      }else{
        
        log_lik_exact[1] = poisson_lpmf(K[leftout] | mu[KretsNrSeqID[leftout]] * A[leftout]);
      }
      
    }
    
    
  }
  
  
  
  if (AnalyzeWAICandLOO){
  for (r in 1:Reports){
    
    
    
    
    if (includealpha){
      if (includephi){
        log_lik[r] = neg_binomial_2_lpmf(K[r] | mu[KretsNrSeqID[r]] * A[r] * exp(logrelA[r] * phi[1]), alpha[KretsNrSeqID[r]]);
      }else{
        log_lik[r] = neg_binomial_2_lpmf(K[r] | mu[KretsNrSeqID[r]] * A[r], alpha[KretsNrSeqID[r]]);
        
      }
    } else {
      if (includephi){
        
        log_lik[r] = poisson_lpmf(K[r] | mu[KretsNrSeqID[r]] * A[r] * exp(logrelA[r] * phi[1]));
      }else{
        
        log_lik[r] = poisson_lpmf(K[r] | mu[KretsNrSeqID[r]] * A[r]);
      }
      
    }
    
    
  }
  
  }
}

I think you can workaround this temporarily by calling

library(rstan)
rstan_options(javascript = FALSE)

Thanks @bgoodri! That seems to work.
Is it safe to include this in files that also will also run ®stan on nodes, or is it expected to affect execution of stan scripts?

It won’t affect anything for the moment.

I encountered the same problem on Ubuntu 20.04. I did not even have to load rstan for this to happen, it was enough for it to be installed. It was driving me crazy!

Thank you for your workaround. I can also confirm it does the trick. I ended up putting the following in my .Rprofile:

rstan::rstan_options(javascript = FALSE)

1 Like