Cmdstan_model Include files failed in docker

I want to write a stan file which includes another prior.stan and run it by cmdstan_model function in R. It works well locally, but the #include prior.stan will fail while running in a docker image.

My ~/test_docker/ folder contains

  1. an empty file named prior.stan and
  2. an R script test.R
# test_docker/test.R
library(cmdstanr)

f <- write_stan_file("functions {
    #include prior.stan
}")

model <- cmdstan_model(f, include_path = getwd())

I run it on a docker image I built. The cmdstan_model() function would cause an error of not finding the include path.

docker pull xinranmiao/test_stan:01
docker run -v ~/test_docker/:/test_docker -it --rm=true xinranmiao/test_stan:01 /bin/bash
cd test_docker
Rscript test.R

Error message:

This is cmdstanr version 0.5.3
- CmdStanR documentation and vignettes: mc-stan.org/cmdstanr
- CmdStan path: /root/.cmdstan/cmdstan-2.31.0
- CmdStan version: 2.31.0
Syntax error in '/tmp/Rtmpiln4CL/model-10200ceee1.stan', line 2, column 4, include error:
   -------------------------------------------------
     1:  functions {
     2:      #include prior.stan
             ^
     3:  }
   -------------------------------------------------

Could not find include file prior.stan in specified include paths.

make: *** [make/program:50: /tmp/Rtmpiln4CL/model-10200ceee1.hpp] Error 1

Error: An error occured during compilation! See the message above for more information.
Execution halted

Notes:

  1. Setting the include_path argument to be an absolute path or relative path (e.g., . or ~) won’t solve this problem.
  2. I think the problem relates to the write_stan_file function. Alternatively if we have a test.stan file that includes prior.stan, then the cmdstan_model function will work.
# test_docker/test.stan
functions {
    #include prior.stan
}
# this will work in R
model <- cmdstan_model("./test.stan", include_path = ".", compile = TRUE)
  1. The error only occurs when I test it on docker. A local test will be fine. Here is my Dockerfile.
FROM rocker/tidyverse

RUN apt-get update
RUN apt-get install -y libudunits2-dev vim
RUN Rscript -e "install.packages('cmdstanr', repos = c('https://mc-stan.org/r-packages/', getOption('repos')))"
RUN Rscript -e "cmdstanr::install_cmdstan()"

RUN chmod -R 777 /usr/local/lib/ /root/ /tmp/
ENV HOME /root/
ENV USER root

Any help will be appreciated!! Thank you!

I think this line confirms it’s the use of write_stan_file. It’s putting the file in a temporary folder.

I’m surprised an absolute path (one that starts with /) doesn’t work though. What is the absolute path of the file you want to include?

Thank you for looking into this! In my example, I tried to put the prior.stan file in the working directory /test_docker or the home directory /root/

f <- write_stan_file("functions {
    #include prior.stan
}")
model <- cmdstan_model(f, include_path = "/test_docker")
# or,
# model <- cmdstan_model(f, include_path = "/root/")

and they both give the same error.

Compiling Stan program...
Syntax error in '/tmp/RtmpLEppWi/model-1325f4d1ee.stan', line 2, column 4, include error:
   -------------------------------------------------
     1:  functions {
     2:      #include prior.stan
             ^
     3:  }
   -------------------------------------------------

Could not find include file prior.stan in specified include paths.

make: *** [make/program:50: /tmp/RtmpLEppWi/model-1325f4d1ee.hpp] Error 1

Error: An error occured during compilation! See the message above for more information.

I also tried to copy the prior.stan file into the temporary directory, but it gives the same error.

tempdir() 
# "/tmp/RtmpLEppWi"

file.copy("prior.stan", tempdir())
model <- cmdstan_model(f, include_path = tempdir())

In every case, I’ve checked list.files() and was sure that prior.stan file was indeed in the include_path I was specifying. This error would only appear in a docker test. Otherwise in my own environment of my laptop, everything works well.
Thank you!