0

Obviously one can find and set working directories with getwd() and setwd(). This question is a bit more complicated. I'm running two files on a super computer (one is a regular R file which calls the other file (a .stan file). The way I submit work to the super computer is that I have to zip a folder containing the data, the .R file, and the .stan file. I upload this folder, and I pull this folder by setting it as one of the parameters in the super computer. I call the data using the standard read.csv() command and everything is hunky dory.

However, when I call the .stan file from the .R file, it can't access it because it needs to know the working directory. This is the error I get:

> fit <- stan(file="ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
  path[1]="ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
  cannot open file 'ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) : 
  cannot open model file "ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted

When I tried setting the working directory to the unzipped NSG_stan folder (which is what I assumed the wd to be, I received this error:

fit <- stan(file="NSG_stan/ace_thresholds.stan", data=stanData, cores = 4)
Error in file(fname, "rt") : cannot open the connection
In addition: Warning messages:
1: In normalizePath(file) :
  path[1]="NSG_stan/ace_thresholds.stan": No such file or directory
2: In file(fname, "rt") :
  cannot open file 'NSG_stan/ace_thresholds.stan': No such file or directory
Error in get_model_strcode(file, model_code) : 
  cannot open model file "NSG_stan/ace_thresholds.stan"
Calls: stan -> stan_model -> stanc -> get_model_strcode
Execution halted

So I tried running print(getwd()) within the script and in the printout I see that the wd is

"/projects/ps-nsg/home/nsguser/ngbw/workspace/NGBW-JOB-RTOOL_TG-EBE9CDBF28BF42AF8CB6EC9355006B3E/NSG_stan"

which means that the working directory will shift with every job. So to accurately set the working directory, I'd need to set it to the current folder within the script. I looked for various posts on how to do this like the following

# install.packages("rstudioapi") # run this if it's your first time using it to install
library(rstudioapi) # load it
# the following line is for getting the path of your current open file
current_path <- getActiveDocumentContext()$path 
# The next line set the working directory to the relevant one:
setwd(dirname(current_path ))
# you can make sure you are in the right directory
print( getwd() )

The issue with this is that it's a super computer, so it's difficult to install packages, because every time I want to install something, I have to email the folks associated with the super comp and that all takes time.

I've looked over this thread as well: R command for setting working directory to source file location in Rstudio. Appears to be a lot of dissent over what works. I tried a couple of them, and they didn't work.

setwd(getSrcDirectory()[1])
this.dir <- dirname(parent.frame(2)$ofile)
setwd(this.dir)

I've included the .R file below, in the event that it helps, but I think this is probably a pretty easy answer for someone with a decent amount of coding experience.

.R file (so it's the "ace_thresholds.stan" file that I either need to link to the current wd or to include the code that would set the wd, such that this "ace_thresholds.stan" call would work. Does that make sense?

Thanks much!

dat <- ace.threshold.t2.samp
dat <- subset(dat, !is.na(rw))
dat$condition <- factor(dat$condition)
dat$pid <- factor(dat$pid)


nTotal <- dim(dat)[1]
nCond <- length(unique(dat$condition))
nSubj <- length(unique(dat$pid))
intensity <- dat$rw
condition <- as.numeric(dat$condition)
pid <- as.numeric(dat$pid)
correct <- dat$correct_button == "correct"
chancePerformance <- 1/2
stanData <- list(nTotal=nTotal, nLevels=nCond, nSubj = nSubj, subject = pid, intensity=intensity, level=condition, correct=correct, chancePerformance=chancePerformance)

fit.rw <- stan(file="ace_thresholds.stan", data=stanData, cores = 4, control=list(max_treedepth=15, adapt_delta=0.90))
James
  • 459
  • 2
  • 14
  • "However, when I call the .stan file from the .R file, it can't access it because it needs to know the working directory." Can you post the error message when you do that? I would have thought that your server would start R from the unzipped directory, making that the working directory. – Ben Goodrich Apr 05 '20 at 03:50
  • Thanks, just posted. The warning I get is the first grey box. – James Apr 05 '20 at 05:15
  • I would think the server would have some environmental variable that users could use to know where their files had been unzipped. Can you contact a system administrator or point to some public-facing documentation of the server? – Ben Goodrich Apr 06 '20 at 22:19
  • Yeah, I contacted them. I converted the file to a .stan, but apparently on their end it was appearing as a .stan.R. So it was a long-time figuring it out, but a quick fix. The wd was apparently set to the current directory. Odd because when I used it with Matlab, I needed to add code to the script telling it to grab the function from the current folder. – James Apr 09 '20 at 22:06
  • You wrote this in 2016: "Not much has changed. For RStan, it is easier in May 2016 in the sense that you can specify the cores argument to stan or sampling or just set options(mc.cores = parallel::detectCores()) prior to calling stan or sampling. But that only reduces typing. It still does the same thing (parallelizes the chains) and does not parallelize anything within chains." Googled but didn't find much. Has anything changed, or is there still nothing that will parallelize within chains? Thanks! – James Apr 09 '20 at 22:10
  • Look at `map_rect` and soon `reduce_sum` – Ben Goodrich Apr 10 '20 at 03:19
  • Awesome. Thanks! – James Apr 10 '20 at 18:43

0 Answers0