Error in sink(type = "output") : invalid connection

Here I come again!
The stan code could run perfectly last night, but reported error since today after a small adjustment was made in my model parameter. I am confused what’s wrong with the code. By the way, the code was run via our public server so it’s not ok for me to restart the server!
Here below is my main_code:
run_rl_sp <- function(multiSubj = FALSE) {
# =============================================================================
#### Construct Data ####
# =============================================================================
# clear workspace
library(rstan)
library(ggplot2)
# library(R.matlab)
subID=105
individual_RL<-subset(IGT_data_individual,subNo==subID)
if (multiSubj==FALSE) {
load(‘I:/Xiehanbo/IGT_Data/R/data_individual_RL_dataset.RData’)
sz <- dim(individual_RL)
nTrials <- sz[1]
dataList <- list(nTrials=nTrials,
choice=individual_RL[,13]-10,
reward=individual_RL[,11])
} else {
load(‘I:/Xiehanbo/IGT_Data/R/data_individual_RL_dataset.RData’)
individual_multi_RL<-IGT_data_individual[,c(1,4,11,13)]
a<-matrix(data=individual_multi_RL[,3],nrow=39,ncol=100)
b<-matrix(data=individual_multi_RL[,4],nrow=39,ncol=100)
sz <- dim(a)
nSubjects <- sz[1]
nTrials <- sz[2]

    dataList <- list(nSubjects=nSubjects,
                     nTrials=nTrials, 
                     choice=b-10, 
                     reward=a)
}   


# =============================================================================
#### Running Stan #### 
# =============================================================================
rstan_options(auto_write = TRUE)
options(mc.cores = 4)

if (multiSubj==FALSE) {
    modelFile <- 'I:/Xiehanbo/IGT_Data/R/individual_single_RL_model.stan'
} else {
    modelFile <- 'I:/Xiehanbo/IGT_Data/R/PVL_Delta_learning_Model.stan'
} 

nIter     <- 4000
nChains   <- 4
nWarmup   <- floor(nIter/2)
nThin     <- 1

cat("Estimating", modelFile, "model... \n")
startTime = Sys.time(); print(startTime)
cat("Calling", nChains, "simulations in Stan... \n")

fit_rl4 <- stan(modelFile, 
               data    = dataList, 
               chains  = nChains,
               iter    = nIter,
               warmup  = nWarmup,
               thin    = nThin,
               init    = "Random",
               seed    = 1450154626)
               control = list(adapt_delta=0.99,
                          stepszie=1.0,
                          max_treedepth=20)

cat("Finishing", modelFile, "model simulation ... \n")
endTime = Sys.time(); print(endTime)  
cat("It took",as.character.Date(endTime - startTime), "\n")

# =============================================================================
#### Model Summary and Diagnostics #### 
# =============================================================================
print(fit_rl4)

plot_trace_excl_warm_up <- stan_trace(fit_rl4, pars = c('lr','c','A','loss_aversion'), inc_warmup = F)
plot_dens <- stan_plot(fit_rl4, pars=c('lr','c','A','loss_aversion'), show_density=T, fill_color = 'skyblue')

return(fit_rl4)

}
It’s like the R told me to add new tools for required source, and I pressed ‘YES’ and it happened like below:

Error in setwd(dirname(libCFile)) : cannot change working directory!
Error in sink(type = “output”) : invalid connection!

Can someone help with that? Thank you in advance!

If you already have RTools, then I would click No if it asks you whether you want to install them again. Otherwise, you have to specify verbose = TRUE when you call stan or sampling in order to see the real error message.

yes! but I have tried clicking no when it informs me to install. Unluckily, the same error reports. So I am confused about it!

We need to see the line that starts with error: (including the colon), when you run it with verbose = TRUE.

Version:1.0 StartHTML:0000000107 EndHTML:0000005896 StartFragment:0000000127 EndFragment:0000005878

  1. sink(type = “output”)

  2. cxxfunctionplus(signature(), body = paste(" return Rcpp::wrap("", model_name, “”);", sep = “”), includes = inc, plugin = “rstan”, save_dso = save_dso auto_write, module_name = paste(“stan_fit4”, model_cppname, “_mod”, sep = “”), verbose = verbose)

  3. stan_model(file, model_name = model_name, model_code = model_code, stanc_ret = NULL, boost_lib = boost_lib, eigen_lib = eigen_lib, save_dso = save_dso, verbose = verbose)

  4. stan(modelFile, data = dataList, chains = nChains, iter = nIter, warmup = nWarmup, thin = nThin, init = “Random”, seed = 1450154626) at

IGT_individual_parm_main.R#64

  1. run_rl_sp(multiSubj = TRUE)

this is exactly what the error is like when using trace back .

Version:1.0 StartHTML:0000000107 EndHTML:0000000943 StartFragment:0000000127 EndFragment:0000000925

697 : // declarations
698 : extern “C” {
699 : SEXP file682044137263( ) ;
700 : }
701 :
702 : // definition
703 :
704 : SEXP file682044137263( ){
705 : return Rcpp::wrap(“PVL_Delta_learning_Model”);
706 : }

this is when I use verbose=TRUE.

Below all that, there will be a line that has the string error: in it. Tell us what that line is.

hi! Thanks for your advice! But I run the code on the server in our lab. It’s because my account was not authorized to write files in server so the stan code couldn’t run. After getting authorization, I worked out the problem! Thanks for your help!